Medial Code Documentation
Loading...
Searching...
No Matches
SGD.h
1#ifndef __SGD_H__
2#define __SGD_H__
3
4#include <MedAlgo/MedAlgo/PredictiveModel.h>
5#include <random>
6#include <map>
7
8using namespace std;
9
10class SGD
11{
12public:
13 SGD(PredictiveModel *mdl, double(*loss_funct)(const vector<double> &got, const vector<float> &y, const vector<float> *weights));
14 void Learn(const vector<vector<float>> &xData, const vector<float> &yData, int T_Steps, const vector<float> *weights = NULL, bool print_auc = false);
15 //use one of these techniques to set params for subgradients
16 double(*subGradientI)(int param_number, const vector<double> &param_values, const vector<vector<float>> &x, const vector<float> &y, const vector<float> *weights);
17 void set_gradient_params(int samplePointCnt, float h, int minSampForCat = 0);
18
19 //use one of them to set learning_rate
20 void set_learing_rate(float val);
21 void set_learing(float blockVals, float blockDerivate, int T_steps);
22 void set_special_step_func(double(*function)(const vector<double> &, const vector<float> &, const vector<double> &, const vector<float> *));
23
24 //use to block parameter search, not mandatory: pass val < 0 to force each step projection to abs(val)
25 void set_blocking(float val);
26
27 //use to round model params - act as some regularization
28 void set_model_precision(double val);
29 double get_model_precision();
30
31 float get_learing_rate();
32 float get_learing_eppsilon(float blockVals, float blockDerivate, int T_steps);
33 PredictiveModel *get_model();
34 float get_blocking();
35
36 size_t output_num; //The number of lines before printing output, 0= no printing
37 bool norm_l1;
38private:
39 PredictiveModel * _model;
40 vector<PredictiveModel *> _models_par;
41 double(*loss_function)(const vector<double> &got, const vector<float> &y, const vector<float> *weights);
42 double(*step_loss_function)(const vector<double> &got, const vector<float> &y, const vector<double> &model_params, const vector<float> *weights);
43 float _learning_rate;
44 bool has_learing_rate;
45 int _minSampForCat;
46 double _min_precision;
47 map<float, vector<int>> _categoryIndex;
48
49 float _h;
50 int _sampleSize;
51 mt19937 _gen;
52
53 float _blocking_val;
54
55 //allocate memory for step function once
56 vector<float> _sampleY;
57 vector<float> _sampleW;
58 vector<vector<float>> _sampleX;
59 vector<double> _preds_plus;
60 vector<double> _preds_minus;
61 vector<double> _preds_base;
62
63 vector<double> _step(const vector<vector<float>> &xData, const vector<float> &yData, const vector<float> *weights);
64 void _projection_step(vector<double> &params);
65 void _round_step(vector<double> &params);
66};
67
68void factor(vector<double> &a, double fact);
69void add(vector<double> &a, const vector<double> &b);
70double product(const vector<double> &a, const vector<double> &b);
71
72#endif
Predictive Model is abstract class of predictor model which has parameters for GD or SGD uses it also...
Definition PredictiveModel.h:13
Definition SGD.h:11
Definition StdDeque.h:58