UFJF - Machine Learning Toolkit  0.51.8
Learner.hpp
1 //
2 // Created by Mateus Coutinho Mari on 9/7/2018.
3 //
4 
5 #pragma once
6 
7 #include "Solution.hpp"
8 #include "Data.hpp"
9 #include "Timer.hpp"
10 
11 namespace mltk{
12  template <typename T>
13  class Learner;
14 
15  template < class T > using LearnerPointer = std::shared_ptr<mltk::Learner< T > >;
16 
17  template < typename T = double >
18  class Learner {
19  protected:
21  std::shared_ptr<Data< T > > samples;
23  double rate = 0.5f;
25  double start_time = 0.0f;
27  double max_time = 110;
29  int steps = 0;
31  int ctot = 0;
33  double EPS = 0.0000001;
35  double MIN_INC = 1.001;
37  int MAX_IT = 1E9;
39  int MAX_UP = 1E9;
40  int MAX_EPOCH = 1E9;
42  int verbose = 1;
46  size_t seed = 0;
47  double pred_prob = 1.0;
48 
49  public:
50  virtual ~Learner() = default;
51 
52  Learner< T >() = default;
53 
54  explicit Learner< T > (DataPointer< T > _samples): samples(_samples) {}
55 
56  Learner< T > (const Learner< T > &learner){
57  this->samples = learner.samples;
58  this->EPS = learner.EPS;
59  this->rate = learner.rate;
60  this->start_time = learner.start_time;
61  this->max_time = learner.max_time;
62  this->steps = learner.steps;
63  this->ctot = learner.ctot;
64  this->MIN_INC = learner.MIN_INC;
65  this->MAX_IT = learner.MAX_IT;
66  this->MAX_UP = learner.MAX_UP;
67  this->MAX_EPOCH = learner.MAX_EPOCH;
68  this->verbose = learner.verbose;
69  this->seed = learner.seed;
70  }
71 
72 
77  virtual bool train () = 0;
83  virtual double evaluate (const Point< T > &p, bool raw_value=false) = 0;
84 
91 
92  /*********************************************
93  * Getters *
94  *********************************************/
95 
100  virtual std::string getFormulationString() = 0;
105  inline auto getSamples() { return this->samples; }
110  [[nodiscard]] inline double getElapsedTime() const { return timer.elapsed(); }
115  [[nodiscard]] inline int getCtot() const { return ctot; }
120  [[nodiscard]] inline int getSteps() const { return steps; }
125  [[nodiscard]] inline int getUpdates() const { return ctot; }
130  [[nodiscard]] inline double getMaxTime() const { return max_time; }
135  [[nodiscard]] double getPredictionProbability() const { return pred_prob; }
136 
137  /*********************************************
138  * Setters *
139  *********************************************/
140 
145  void setSeed(const size_t _seed){ this->seed = _seed; }
150  virtual void setSamples(const Data< T > &data) { this->samples = make_data<T>(data); }
155  virtual void setSamples(DataPointer< T > data) { this->samples = data; }
160  void setTimer(Timer _timer) { this->timer = _timer; }
165  inline void setSteps(int _steps) { this->steps = _steps; }
170  void setCtot(int _ctot) { this->ctot = _ctot;}
175  void setVerbose(int _verbose) { this->verbose = _verbose;}
180  void setStartTime(double stime) { this->start_time = stime;}
185  void setMaxTime(double maxtime) { this->max_time = maxtime;}
190  void setEPS(double eps) { this->EPS = eps;}
195  void setMaxIterations(int max_it) { this->MAX_IT = max_it;}
200  void setMaxEpochs(int MAX_EPOCHS) {this->MAX_EPOCH = MAX_EPOCHS;}
205  void setMaxUpdates(int max_up) { this->MAX_UP = max_up;}
210  void setLearningRate(double learning_rate) { this->rate = learning_rate;}
211  };
212 
213  /*********************************************
214  * Implementation *
215  *********************************************/
216 
217  template<typename T>
219  mltk::Point<double> preds(data.size());
220  std::transform(data.begin(), data.end(), preds.begin(), [this](auto point){
221  return this->evaluate(*point);
222  });
223  return preds;
224  }
225 }
size_t size() const
Returns the size of the dataset.
Definition: Data.hpp:208
Definition: Learner.hpp:18
int MAX_IT
Max number of iterations.
Definition: Learner.hpp:37
void setMaxUpdates(int max_up)
setMaxIterations Set the max number of updates of the Learner.
Definition: Learner.hpp:205
std::shared_ptr< Data< T > > samples
Samples used in the model training.
Definition: Learner.hpp:21
virtual void setSamples(DataPointer< T > data)
setSamples Set the samples used by the Learner.
Definition: Learner.hpp:155
void setLearningRate(double learning_rate)
Set the learning rate of the Learner.
Definition: Learner.hpp:210
void setCtot(int _ctot)
Set the partial number of updates of the Learner.
Definition: Learner.hpp:170
int ctot
Number of updates of the weights.
Definition: Learner.hpp:31
double getPredictionProbability() const
Get the probability of the last prediction.
Definition: Learner.hpp:135
double max_time
Maximum time of training.
Definition: Learner.hpp:27
virtual void setSamples(const Data< T > &data)
setSamples Set the samples used by the Learner.
Definition: Learner.hpp:150
auto getSamples()
Get the Data used by the learner.
Definition: Learner.hpp:105
virtual mltk::Point< double > batchEvaluate(const Data< T > &data)
evaluate a batch of points.
Definition: Learner.hpp:218
double rate
Learning rate.
Definition: Learner.hpp:23
double start_time
Initial time.
Definition: Learner.hpp:25
double EPS
Max precision.
Definition: Learner.hpp:33
int steps
Number of steps in the data.
Definition: Learner.hpp:29
void setStartTime(double stime)
setStartTime Set the initial time of the Learner.
Definition: Learner.hpp:180
void setSeed(const size_t _seed)
Set the seed to be used by the learner.
Definition: Learner.hpp:145
void setMaxIterations(int max_it)
setMaxIterations Set the max number of iterations of the Learner.
Definition: Learner.hpp:195
int MAX_UP
Max number of updates.
Definition: Learner.hpp:39
virtual bool train()=0
Function that execute the training phase of a Learner.
void setSteps(int _steps)
Set the partial number of steps used in the training phase of the Learner.
Definition: Learner.hpp:165
void setTimer(Timer _timer)
setTimer Set the timer used by the Learner.
Definition: Learner.hpp:160
double getMaxTime() const
getMaxTime Returns the maximum running time in the training phase of the Learner.
Definition: Learner.hpp:130
void setMaxEpochs(int MAX_EPOCHS)
Set the max number of epochs for the learner training.
Definition: Learner.hpp:200
int getCtot() const
Get the total number of updates of the Learner.
Definition: Learner.hpp:115
double MIN_INC
Minimun Increment.
Definition: Learner.hpp:35
int getUpdates() const
getUpdates Returns the number of updates needed to get to the the solution.
Definition: Learner.hpp:125
int verbose
Verbose level of the output.
Definition: Learner.hpp:42
virtual std::string getFormulationString()=0
getFormulationString Returns a string that represents the formulation of the learner (Primal or Dual)...
virtual double evaluate(const Point< T > &p, bool raw_value=false)=0
Returns the class of a feature point based on the trained Learner.
void setMaxTime(double maxtime)
Set the max time of execution.
Definition: Learner.hpp:185
size_t seed
seed for random operations.
Definition: Learner.hpp:46
void setVerbose(int _verbose)
Set the level of verbose.
Definition: Learner.hpp:175
int getSteps() const
getSteps Returns the number of steps through the data by the Learner.
Definition: Learner.hpp:120
double getElapsedTime() const
Get the elapsed time in the training phase of the Learner.
Definition: Learner.hpp:110
void setEPS(double eps)
setEPS Set the precision of the Learner.
Definition: Learner.hpp:190
Wrapper for the implementation of a simple timer.
Definition: Timer.hpp:16
A helper class to measure execution time for benchmarking purposes.
Definition: ThreadPool.hpp:503
UFJF-MLTK main namespace for core functionalities.
Definition: classifier/Classifier.hpp:11