-
Notifications
You must be signed in to change notification settings - Fork 15
/
svm_sgdx_common.h
194 lines (151 loc) · 3.72 KB
/
svm_sgdx_common.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**********************************************************
*
* Common devices for implementing SGD for SVM
*
**********************************************************/
#include <light_mat/matlab/matlab_port.h>
#include <light_mat/mateval/mat_reduce.h>
using namespace lmat;
using namespace lmat::matlab;
// weight vector classes
typedef cref_matrix<double, 0, 1> cvec_t;
typedef cref_matrix<double, 0, 0> cmat_t;
typedef ref_matrix<double, 0, 1> vec_t;
typedef ref_matrix<double, 0, 0> mat_t;
class WeightVec
{
public:
LMAT_ENSURE_INLINE
WeightVec(marray mW)
: _w(mW.ptr_real(), mW.nrows(), 1) { }
LMAT_ENSURE_INLINE
index_t dim() const
{
return _w.nrows();
}
LMAT_ENSURE_INLINE
double sqnorm() const
{
return lmat::sqsum(_w);
}
LMAT_ENSURE_INLINE
double predict(const cvec_t& x) const
{
return lmat::dot(_w, x);
}
LMAT_ENSURE_INLINE
void operator += (const cvec_t& x)
{
const index_t d = _w.nrows();
for (index_t i = 0; i < d; ++i) _w[i] += x[i];
}
LMAT_ENSURE_INLINE
void add_mul(const cvec_t& x, double c)
{
const index_t d = _w.nrows();
for (index_t i = 0; i < d; ++i) _w[i] += c * x[i];
}
LMAT_ENSURE_INLINE
void operator *= (double c)
{
const index_t d = _w.nrows();
for (index_t i = 0; i < d; ++i) _w[i] *= c;
}
LMAT_ENSURE_INLINE
double operator[] (index_t i) const
{
return _w[i];
}
LMAT_ENSURE_INLINE
double bias() const
{
return 0;
}
private:
vec_t _w;
};
class WeightVecX
{
public:
LMAT_ENSURE_INLINE
WeightVecX(marray mW, marray mW0, double aug)
: _w(mW.ptr_real(), mW.nrows(), 1)
, _w0(*(mW0.ptr_real()))
, _aug(aug)
{ }
LMAT_ENSURE_INLINE
index_t dim() const
{
return _w.nrows();
}
LMAT_ENSURE_INLINE
double sqnorm() const
{
return lmat::sqsum(_w) + _w0 * _w0;
}
LMAT_ENSURE_INLINE
double predict(const cvec_t& x) const
{
return lmat::dot(_w, x) + _w0 * _aug;
}
LMAT_ENSURE_INLINE
void operator += (const cvec_t& x)
{
const index_t d = _w.nrows();
for (index_t i = 0; i < d; ++i) _w[i] += x[i];
_w0 += _aug;
}
LMAT_ENSURE_INLINE
void add_mul(const cvec_t& x, double c)
{
const index_t d = _w.nrows();
for (index_t i = 0; i < d; ++i) _w[i] += c * x[i];
_w0 += c * _aug;
}
LMAT_ENSURE_INLINE
void operator *= (double c)
{
const index_t d = _w.nrows();
for (index_t i = 0; i < d; ++i) _w[i] *= c;
_w0 *= c;
}
LMAT_ENSURE_INLINE
double operator[] (index_t i) const
{
return _w[i];
}
LMAT_ENSURE_INLINE
double bias() const
{
return _w0;
}
private:
vec_t _w;
double& _w0;
const double _aug;
};
// main skeleton
template<class Trainer>
void run_sgdx(
Trainer& trainer,
const cmat_t& Xstream,
const cvec_t& Ystream,
const index_t K)
{
const index_t d = Xstream.nrows();
const index_t n = Xstream.ncolumns();
index_t rn = n;
index_t i = 0;
for ( ;rn >= K; rn -= K, i += K)
{
const double *pX = Xstream.ptr_col(i);
const double *pY = Ystream.ptr_data() + i;
trainer.learn(pX, pY, K);
}
if (rn > 0)
{
const double *pX = Xstream.ptr_col(i);
const double *pY = Ystream.ptr_data() + i;
trainer.learn(pX, pY, rn);
}
}