-
Notifications
You must be signed in to change notification settings - Fork 29
/
SparseUniLayer.h
422 lines (341 loc) · 10.7 KB
/
SparseUniLayer.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* SparseUniLayer.h
*
* Created on: Mar 18, 2015
* Author: mszhang
*/
#ifndef SRC_SparseUniLayer_H_
#define SRC_SparseUniLayer_H_
#include "tensor.h"
#include "Utiltensor.h"
#include "MyLib.h"
using namespace mshadow;
using namespace mshadow::expr;
using namespace mshadow::utils;
// Weight updating process implemented without theory support,
// but recently find an EMNLP 2015 paper "An Empirical Analysis of Optimization for Max-Margin NLP"
// In all my papers that use adagrad for sparse features, I use it for parameter updating.
template<typename xpu>
class SparseUniLayer {
public:
hash_set<int> _indexers;
Tensor<xpu, 2, dtype> _W;
Tensor<xpu, 2, dtype> _b;
Tensor<xpu, 2, dtype> _gradW;
Tensor<xpu, 2, dtype> _gradb;
Tensor<xpu, 2, dtype> _eg2W;
Tensor<xpu, 2, dtype> _eg2b;
Tensor<xpu, 2, dtype> _ftW;
bool _bUseB;
int _funcType; // 0: tanh, 1: sigmod, 2: f(x)=x, 3: exp
int _max_update;
NRVec<int> _last_update;
public:
SparseUniLayer() {
_indexers.clear();
}
inline void initial(int nOSize, int nISize, bool bUseB = true, int seed = 0, int funcType = 0) {
dtype bound = sqrt(6.0 / (nOSize + nISize + 1));
//dtype bound = 0.01;
_W = NewTensor<xpu>(Shape2(nISize, nOSize), d_zero);
_gradW = NewTensor<xpu>(Shape2(nISize, nOSize), d_zero);
_eg2W = NewTensor<xpu>(Shape2(nISize, nOSize), d_zero);
_ftW = NewTensor<xpu>(Shape2(nISize, nOSize), d_one);
_b = NewTensor<xpu>(Shape2(1, nOSize), d_zero);
_gradb = NewTensor<xpu>(Shape2(1, nOSize), d_zero);
_eg2b = NewTensor<xpu>(Shape2(1, nOSize), d_zero);
random(_W, -1.0 * bound, 1.0 * bound, seed);
random(_b, -1.0 * bound, 1.0 * bound, seed + 1);
_bUseB = bUseB;
_funcType = funcType;
_max_update = 0;
_last_update.resize(nISize);
_last_update = 0;
}
inline void initial(Tensor<xpu, 2, dtype> W, Tensor<xpu, 2, dtype> b, bool bUseB = true, int funcType = 0) {
static int nOSize, nISize;
nISize = W.size(0);
nOSize = W.size(1);
_W = NewTensor<xpu>(Shape2(nOSize, nISize), d_zero);
_gradW = NewTensor<xpu>(Shape2(nOSize, nISize), d_zero);
_eg2W = NewTensor<xpu>(Shape2(nOSize, nISize), d_zero);
_ftW = NewTensor<xpu>(Shape2(nOSize, nISize), d_one);
Copy(_W, W);
_b = NewTensor<xpu>(Shape2(1, nOSize), d_zero);
_gradb = NewTensor<xpu>(Shape2(1, nOSize), d_zero);
_eg2b = NewTensor<xpu>(Shape2(1, nOSize), d_zero);
if (bUseB)
Copy(_b, b);
_bUseB = bUseB;
_funcType = funcType;
_max_update = 0;
_last_update.resize(nISize);
_last_update = 0;
}
inline void release() {
FreeSpace(&_W);
FreeSpace(&_gradW);
FreeSpace(&_eg2W);
FreeSpace(&_ftW);
FreeSpace(&_b);
FreeSpace(&_gradb);
FreeSpace(&_eg2b);
_indexers.clear();
}
virtual ~SparseUniLayer() {
// TODO Auto-generated destructor stub
}
inline dtype squarenormAll() {
dtype result = squarenorm(_gradW);
if (_bUseB) {
result += squarenorm(_gradb);
}
return result;
}
inline void scaleGrad(dtype scale) {
_gradW = _gradW * scale;
if (_bUseB) {
_gradb = _gradb * scale;
}
}
public:
void ComputeForwardScore(const std::vector<int>& x, Tensor<xpu, 2, dtype> y) {
static long long featNum, featId;
featNum = x.size();
y = 0.0;
for (int idx = 0; idx < featNum; idx++) {
featId = x[idx];
updateSparseWeight(featId);
y[0] += _W[featId];
}
if (_bUseB)
y = y + _b;
if (_funcType == 0)
y = F<nl_tanh>(y);
else if (_funcType == 1)
y = F<nl_sigmoid>(y);
else if (_funcType == 3)
y = F<nl_exp>(y);
}
void ComputeForwardScore(const std::vector<std::vector<int> >& x, Tensor<xpu, 3, dtype> y) {
static long long featNum, featId;
int seq_size = y.size(0);
for (int id = 0; id < seq_size; id++) {
featNum = x[id].size();
y[id] = 0.0;
for (int idx = 0; idx < featNum; idx++) {
featId = x[id][idx];
updateSparseWeight(featId);
y[id][0] += _W[featId];
}
if (_bUseB)
y[id] = y[id] + _b;
if (_funcType == 0)
y[id] = F<nl_tanh>(y[id]);
else if (_funcType == 1)
y[id] = F<nl_sigmoid>(y[id]);
else if (_funcType == 3)
y[id] = F<nl_exp>(y[id]);
}
}
void ComputeForwardScore(const std::vector<std::vector<int> >& x, std::vector<Tensor<xpu, 2, dtype> > &y) {
static long long featNum, featId;
int seq_size = y.size();
for (int id = 0; id < seq_size; id++) {
featNum = x[id].size();
y[id] = 0.0;
for (int idx = 0; idx < featNum; idx++) {
featId = x[id][idx];
updateSparseWeight(featId);
y[id][0] += _W[featId];
}
if (_bUseB)
y[id] = y[id] + _b;
if (_funcType == 0)
y[id] = F<nl_tanh>(y[id]);
else if (_funcType == 1)
y[id] = F<nl_sigmoid>(y[id]);
else if (_funcType == 3)
y[id] = F<nl_exp>(y[id]);
}
}
// loss is stopped at this layer, since the input is one-hold alike
void ComputeBackwardLoss(const std::vector<int>& x, Tensor<xpu, 2, dtype> y, Tensor<xpu, 2, dtype> ly) {
Tensor<xpu, 2, dtype> deri_yx(Shape2(y.size(0), y.size(1))), cly(Shape2(y.size(0), y.size(1)));
AllocSpace(&deri_yx);
AllocSpace(&cly);
if (_funcType == 0) {
deri_yx = F<nl_dtanh>(y);
cly = ly * deri_yx;
} else if (_funcType == 1) {
deri_yx = F<nl_dsigmoid>(y);
cly = ly * deri_yx;
} else if (_funcType == 3) {
cly = ly * y;
} else {
//cly = ly;
Copy(cly, ly);
}
//_gradW
static long long featNum, featId;
featNum = x.size();
for (int idx = 0; idx < featNum; idx++) {
featId = x[idx];
_indexers.insert(featId);
_gradW[featId] += cly[0];
}
if (_bUseB)
_gradb = _gradb + cly;
FreeSpace(&deri_yx);
FreeSpace(&cly);
}
void ComputeBackwardLoss(const std::vector<std::vector<int> >& x, Tensor<xpu, 3, dtype> y, Tensor<xpu, 3, dtype> ly) {
int seq_size = y.size(0);
int y_dim1 = y.size(1), y_dim2 = y.size(2);
static long long featNum, featId;
Tensor<xpu, 2, dtype> deri_yx(Shape2(y_dim1, y_dim2)), cly(Shape2(y_dim1, y_dim2));
AllocSpace(&deri_yx);
AllocSpace(&cly);
for (int id = 0; id < seq_size; id++) {
if (_funcType == 0) {
deri_yx = F<nl_dtanh>(y[id]);
cly = ly[id] * deri_yx;
} else if (_funcType == 1) {
deri_yx = F<nl_dsigmoid>(y[id]);
cly = ly[id] * deri_yx;
} else if (_funcType == 3) {
cly = ly[id] * y[id];
} else {
//cly = ly;
Copy(cly, ly[id]);
}
//_gradW
featNum = x[id].size();
for (int idx = 0; idx < featNum; idx++) {
featId = x[id][idx];
_indexers.insert(featId);
_gradW[featId] += cly[0];
}
if (_bUseB)
_gradb = _gradb + cly;
}
FreeSpace(&deri_yx);
FreeSpace(&cly);
}
void ComputeBackwardLoss(const std::vector<std::vector<int> >& x, const std::vector<Tensor<xpu, 2, dtype> > &y,
const std::vector<Tensor<xpu, 2, dtype> > &ly) {
int seq_size = y.size();
assert(seq_size > 0);
int y_dim1 = y[0].size(0), y_dim2 = y[0].size(1);
static long long featNum, featId, startPos;
Tensor<xpu, 2, dtype> deri_yx(Shape2(y_dim1, y_dim2)), cly(Shape2(y_dim1, y_dim2));
AllocSpace(&deri_yx);
AllocSpace(&cly);
for (int id = 0; id < seq_size; id++) {
if (_funcType == 0) {
deri_yx = F<nl_dtanh>(y[id]);
cly = ly[id] * deri_yx;
} else if (_funcType == 1) {
deri_yx = F<nl_dsigmoid>(y[id]);
cly = ly[id] * deri_yx;
} else if (_funcType == 3) {
cly = ly[id] * y[id];
} else {
//cly = ly;
Copy(cly, ly[id]);
}
//_gradW
featNum = x[id].size();
for (int idx = 0; idx < featNum; idx++) {
featId = x[id][idx];
_indexers.insert(featId);
_gradW[featId] += cly[0];
}
if (_bUseB)
_gradb = _gradb + cly;
}
FreeSpace(&deri_yx);
FreeSpace(&cly);
}
void randomprint(int num) {
static int nOSize, nISize;
nISize = _W.size(0);
nOSize = _W.size(1);
int count = 0;
while (count < num) {
int idx = rand() % nOSize;
int idy = rand() % nISize;
std::cout << "_W[" << idx << "," << idy << "]=" << _W[idy][idx] << " ";
if (_bUseB) {
int idz = rand() % nOSize;
std::cout << "_b[0][" << idz << "]=" << _b[0][idz] << " ";
}
count++;
}
std::cout << std::endl;
}
void updateAdaGrad(dtype regularizationWeight, dtype adaAlpha, dtype adaEps) {
static int startPos;
static hash_set<int>::iterator it;
_max_update++;
Tensor<xpu, 1, dtype> sqrt_eg2W = NewTensor<xpu>(Shape1(_W.size(1)), d_zero);
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
int index = *it;
_eg2W[index] = _eg2W[index] + _gradW[index] * _gradW[index];
sqrt_eg2W = F<nl_sqrt>(_eg2W[index] + adaEps);
_W[index] = (_W[index] * sqrt_eg2W - _gradW[index] * adaAlpha) / (adaAlpha * regularizationWeight + sqrt_eg2W);
_ftW[index] = sqrt_eg2W / (adaAlpha * regularizationWeight + sqrt_eg2W);
}
FreeSpace(&sqrt_eg2W);
if (_bUseB) {
_gradb = _gradb + _b * regularizationWeight;
_eg2b = _eg2b + _gradb * _gradb;
_b = _b - _gradb * adaAlpha / F<nl_sqrt>(_eg2b + adaEps);
}
clearGrad();
}
void clearGrad() {
static hash_set<int>::iterator it;
for (it = _indexers.begin(); it != _indexers.end(); ++it) {
int index = *it;
_gradW[index] = 0.0;
}
_indexers.clear();
if (_bUseB)
_gradb = 0.0;
}
void updateSparseWeight(long long featId) {
if (_last_update[featId] < _max_update) {
int times = _max_update - _last_update[featId];
_W[featId] = _W[featId] * F<nl_exp>(times * F<nl_log>(_ftW[featId]));
_last_update[featId] = _max_update;
}
}
void writeModel(LStream &outf) {
SaveBinary(outf, _W);
SaveBinary(outf, _b);
SaveBinary(outf, _gradW);
SaveBinary(outf, _gradb);
SaveBinary(outf, _eg2W);
SaveBinary(outf, _eg2b);
SaveBinary(outf, _ftW);
WriteBinary(outf, _bUseB);
WriteBinary(outf, _funcType);
WriteBinary(outf, _max_update);
WriteVector(outf, _last_update);
}
void loadModel(LStream &inf) {
LoadBinary(inf, &_W, false);
LoadBinary(inf, &_b, false);
LoadBinary(inf, &_gradW, false);
LoadBinary(inf, &_gradb, false);
LoadBinary(inf, &_eg2W, false);
LoadBinary(inf, &_eg2b, false);
LoadBinary(inf, &_ftW, false);
ReadBinary(inf, _bUseB);
ReadBinary(inf, _funcType);
ReadBinary(inf, _max_update);
ReadVector(inf, _last_update);
}
};
#endif /* SRC_SparseUniLayer_H_ */