-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgdSVM.h
108 lines (64 loc) · 2.29 KB
/
sgdSVM.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/////////////////////////////////////////////////////////////////////////////////////////////
// LIBRARY - stochastic gradient descent solver for SVM
// by Michal Hradis 2011
// Based on work of Leon Bottou http://leon.bottou.org/projects/sgd
// Licence: Lesser General Public License http://www.gnu.org/licenses/lgpl.html
/////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __sgdSVM_h__
#define __sgdSVM_h__
#include <vector>
#include <string>
#include <map>
typedef double TFloatRep;
enum TLoss { HINGELOSS = 1, SMOOTHHINGELOSS = 2, SQUAREDHINGELOSS = 3, LOGLOSS = 10, LOGLOSSMARGIN = 11, EXPLOSS = 12};
static std::map< std::string, TLoss> lossTranslation;
static std::map< TLoss, std::string> invLossTranslation;
void prepareLossTranslation();
class TSVMSgd
{
const int dimension;
TFloatRep *w;
double t;
double lambda;
double wscale;
double bias;
TLoss loss;
public:
TSVMSgd( const int dim, const TFloatRep lambda, const std::string &_loss = "HINGELOSS");
void setClassifier( const TFloatRep *_w, const double _bias);
void train( const std::vector< TFloatRep *> &x, const std::vector< TFloatRep> &y);
void eval( const std::vector< TFloatRep *> &x, std::vector< TFloatRep> &y);
double eval( TFloatRep * x);
std::vector< float> getW();
};
class TSparseCodeSGD
{
int dimension;
int step;
std::vector< double> w;
double t;
double lambda;
double wscale;
double bias;
TLoss loss;
public:
TSparseCodeSGD( const int dim, const int step, const double lambda, const std::string &_loss = "HINGELOSS");
void update( const unsigned char *x, const double classID);
double eval( const unsigned char *x);
std::vector< double> getW();
double getBias(){ return bias;}
};
class TTestHistogram
{
static const int binCount = 2000;
double minVal;
double maxVal;
std::vector< double> posHistogram;
std::vector< double> negHistogram;
public:
TTestHistogram( const std::vector< double> &w, const double bias, int dim);
void accumulate( const double val, bool pos);
void compute( double &avgP, double &eer, double &negRejection, double & posRejection, double & threshold, const double alpha = 0.002);
void getHistograms( std::vector< double > & positive, std::vector< double > & negative, std::vector< double > & xAxis);
};
#endif