Medial Code Documentation
Loading...
Searching...
No Matches
MedBooster.h
1//
2// MedBooster.h
3//
4// A general wrapper allowing several boosting strategies for predictors.
5//
6
7#ifndef __MEDBOOSTER__H__
8#define __MEDBOOSTER__H__
9
10#include "MedAlgo.h"
11
12//============================================================================================================
13// currently parameters should be initialized from a string
14// The format for that is :
15// boost#booster_type=...,loss_func=...,nrounds=...,shrinkage=....,internal=...#internal_params#....
16// example:
17// boost@booster_type=gradient,loss_func=square,nrounds=40,shrinkage=0.05,internal_model=linear_model@internal_params@rfactor=0.8
18//
20 public:
21
22 string init_string;
23
24 int nrounds;
25 float shrinkage;
26
27 string booster_type; // adaboost OR gradient
28 string loss_func; // one of: square , softmax , logistic
29
30 string internal_booster_model;
31 string internal_booster_params;
32
33 int print_flag; // weather to print something in each round in training
34
35 void init_from_string(const string &init_str);
36
37};
38
39//============================================================================================================
40class MedBooster : public MedPredictor {
41
42 public:
43
44 MedBoosterParams params;
45
46 vector<MedPredictor *> predictors; // actual predictors for each round.
47 MedMat<float> alpha; // linear coefficient of each predictor (could be multi channel), last one is always the bias
48
49 MedBooster() { classifier_type = MODEL_BOOSTER; init_defaults(); }
50 ~MedBooster() {
51 for (auto &predictor : predictors) if (predictor) { delete predictor; predictor = NULL; }
52 }
53
54 MedBooster(void *_params) { params = *(MedBoosterParams *)_params; }
55 MedBooster(MedBoosterParams& _params) { params = _params; }
56 int init(void *_params) { params = *(MedBoosterParams *)_params; return 0; }
57
60 int init_from_string(string initialization_text) {
61 cerr << "MedBooster init_from_string ! :: " << initialization_text << "\n";
62 params.init_from_string(initialization_text);
63 return 0;
64 }
66 int init(map<string, string>& mapper) {
67 cerr << "MedBooster:: init map :: not supported, only init_from_string supported....\n";
68 return -1;
69 }
70
71 int set_params(map<string, string>& mapper) {
72 cerr << "MedBooster:: init map :: not supported, only init_from_string supported....\n";
73 return -1;
74 }
75
76 // int init(const string &init_str); // allows init of parameters from a string. Format is: param=val,... , for sampsize: 0 is NULL, a list of values is separated by ; (and not ,)
77 void init_defaults() { params.init_from_string("boost@booster_type=gradient,loss_func=square,nrounds=10,shrinkage=0.1,internal_model=linear_model@internal_params@rfactor=0.8"); }
78
79 int Learn(float *x, float *y, const float *w, int nsamples, int nftrs) {
80 cerr << "MedBooster:: Learn :: API's with MedMat are preferred....\n";
81 MedMat<float> xmat; xmat.load(x, nsamples, nftrs);
82 MedMat<float> ymat; ymat.load(y, nsamples, 1);
83 return learn(xmat, ymat);
84 }
85
86 int Predict(float *x, float *&preds, int nsamples, int nftrs) const {
87 cerr << "MedBooster:: Learn :: API's with MedMat are preferred....\n";
88 MedMat<float> xmat; xmat.load(x, nsamples, nftrs);
89 vector<float> vpreds;
90 int rc = predict(xmat, vpreds);
91 if (preds == NULL) preds = new float[nsamples];
92 memcpy(preds, &vpreds[0], sizeof(float)*nsamples);
93 return rc;
94 }
95
96 int learn(MedMat<float> &x, MedMat<float> &y);
97 int learn(MedMat<float> &x, MedMat<float> &y, vector<float> &wgt) { return learn(x, y); }
98 int predict(MedMat<float> &x, vector<float> &preds) const;
99
100 // serializations
101 ADD_CLASS_NAME(MedBooster)
102 size_t get_size();
103 size_t serialize(unsigned char *blob);
104 size_t deserialize(unsigned char *blob);
105
106
107 // internal funcs
108 int learn_gradient_booster(MedMat<float> &x, MedMat<float> &y);
109 MedPredictor *train_internal_model(MedMat<float> &x, MedMat<float> &y, string model_name, string model_params);
110 int gradient_get_round_alphas(MedMat<float> &residual, vector<float> &preds, vector<float> &a);
111 int gradient_get_alphas_square_loss(MedMat<float> &residual, vector<float> &preds, vector<float> &a);
112 int update_round_residuals(MedMat<float> &y, vector<float> &all_preds, MedMat<float> &residual, vector<float> &preds, vector<float> &a);
113 int update_round_residuals_single_linear(MedMat<float> &y, vector<float> &all_preds, MedMat<float> &residual, vector<float> &preds, vector<float> &a);
114};
115
116//=================================================================
117// Joining the MedSerialize Wagon
118//=================================================================
119
121#endif
MedAlgo - APIs to different algorithms: Linear Models, RF, GBM, KNN, and more.
@ MODEL_BOOSTER
to_use:"booster" general booster (meta algorithm) - creates MedBooster
Definition MedAlgo.h:55
#define MEDSERIALIZE_SUPPORT(Type)
Definition SerializableObject.h:108
Definition MedBooster.h:19
void init_from_string(const string &init_str)
Definition MedBooster.cpp:26
Definition MedBooster.h:40
size_t get_size()
Gets bytes sizes for serializations.
Definition MedBooster.cpp:291
size_t deserialize(unsigned char *blob)
Deserialiazing blob to object. returns number of bytes read.
Definition MedBooster.cpp:305
size_t serialize(unsigned char *blob)
Serialiazing object to blob memory. return number ob bytes wrote to memory.
Definition MedBooster.cpp:298
int init(map< string, string > &mapper)
MedBooster:: init map :: not supported, only init_from_string supported.
Definition MedBooster.h:66
int init_from_string(string initialization_text)
The parsed fields from init command.
Definition MedBooster.h:60
int Predict(float *x, float *&preds, int nsamples, int nftrs) const
Predict should be implemented for each model.
Definition MedBooster.h:86
int Learn(float *x, float *y, const float *w, int nsamples, int nftrs)
Learn should be implemented for each model.
Definition MedBooster.h:79
Definition MedMat.h:63
Base Interface for predictor.
Definition MedAlgo.h:78
MedPredictorTypes classifier_type
The Predicotr enum type.
Definition MedAlgo.h:80