-
Notifications
You must be signed in to change notification settings - Fork 29
/
UniLayer.h
357 lines (296 loc) · 8.96 KB
/
UniLayer.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
/*
* UniLayer.h
*
* Created on: Mar 18, 2015
* Author: mszhang
*/
#ifndef SRC_UniLayer_H_
#define SRC_UniLayer_H_
#include "tensor.h"
#include "MyLib.h"
#include "Utiltensor.h"
using namespace mshadow;
using namespace mshadow::expr;
using namespace mshadow::utils;
template<typename xpu>
class UniLayer {
public:
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;
bool _bUseB;
int _funcType; // 0: tanh, 1: sigmod, 2: f(x)=x, 3: exp
public:
UniLayer() {
}
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(nOSize, nISize), d_zero);
_gradW = NewTensor<xpu>(Shape2(nOSize, nISize), d_zero);
_eg2W = NewTensor<xpu>(Shape2(nOSize, nISize), d_zero);
_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;
}
inline void initial(Tensor<xpu, 2, dtype> W, Tensor<xpu, 2, dtype> b, bool bUseB = true, int funcType = 0) {
static int nOSize, nISize;
nOSize = W.size(0);
nISize = 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);
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;
}
inline void initial(Tensor<xpu, 2, dtype> W, int funcType = 0) {
static int nOSize, nISize;
nOSize = W.size(0);
nISize = 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);
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);
_bUseB = false;
_funcType = funcType;
}
inline void release() {
FreeSpace(&_W);
FreeSpace(&_gradW);
FreeSpace(&_eg2W);
FreeSpace(&_b);
FreeSpace(&_gradb);
FreeSpace(&_eg2b);
}
virtual ~UniLayer() {
// 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:
inline void ComputeForwardScore(Tensor<xpu, 2, dtype> x, Tensor<xpu, 2, dtype> y) {
y = dot(x, _W.T());
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);
}
inline void ComputeForwardScore(Tensor<xpu, 3, dtype> x, Tensor<xpu, 3, dtype> y) {
int seq_size = y.size(0);
for (int id = 0; id < seq_size; id++) {
y[id] = dot(x[id], _W.T());
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]);
}
}
inline void ComputeForwardScore(const std::vector<Tensor<xpu, 2, dtype> > &x, std::vector<Tensor<xpu, 2, dtype> > &y) {
int seq_size = y.size();
for (int id = 0; id < seq_size; id++) {
y[id] = dot(x[id], _W.T());
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]);
}
}
//please allocate the memory outside here
inline void ComputeBackwardLoss(Tensor<xpu, 2, dtype> x, Tensor<xpu, 2, dtype> y, Tensor<xpu, 2, dtype> ly, Tensor<xpu, 2, dtype> lx, bool bclear = false) {
//_gradW
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 (bclear)
lx = 0.0;
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
_gradW += dot(cly.T(), x);
//_gradb
if (_bUseB)
_gradb += cly;
//lx
lx += dot(cly, _W);
FreeSpace(&deri_yx);
FreeSpace(&cly);
}
//please allocate the memory outside here
inline void ComputeBackwardLoss(Tensor<xpu, 3, dtype> x, Tensor<xpu, 3, dtype> y, Tensor<xpu, 3, dtype> ly, Tensor<xpu, 3, dtype> lx, bool bclear = false) {
//_gradW
int seq_size = y.size(0);
int y_dim1 = y.size(1), y_dim2 = y.size(2);
Tensor<xpu, 2, dtype> deri_yx(Shape2(y_dim1, y_dim2)), cly(Shape2(y_dim1, y_dim2));
AllocSpace(&deri_yx);
AllocSpace(&cly);
if (bclear)
lx = 0.0;
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
_gradW += dot(cly.T(), x[id]);
//_gradb
if (_bUseB)
_gradb += cly;
//lx
lx[id] += dot(cly, _W);
}
FreeSpace(&deri_yx);
FreeSpace(&cly);
}
//please allocate the memory outside here
inline void ComputeBackwardLoss(const std::vector<Tensor<xpu, 2, dtype> > &x, const std::vector<Tensor<xpu, 2, dtype> > &y,
const std::vector<Tensor<xpu, 2, dtype> > &ly, std::vector<Tensor<xpu, 2, dtype> > &lx, bool bclear = false) {
//_gradW
int seq_size = y.size();
assert(seq_size > 0);
int y_dim1 = y[0].size(0), y_dim2 = y[0].size(1);
Tensor<xpu, 2, dtype> deri_yx(Shape2(y_dim1, y_dim2)), cly(Shape2(y_dim1, y_dim2));
AllocSpace(&deri_yx);
AllocSpace(&cly);
if(bclear) {
for (int id = 0; id < seq_size; id++) {
lx[id] = 0.0;
}
}
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
_gradW += dot(cly.T(), x[id]);
//_gradb
if (_bUseB)
_gradb += cly;
//lx
lx[id] += dot(cly, _W);
}
FreeSpace(&deri_yx);
FreeSpace(&cly);
}
inline void randomprint(int num) {
static int nOSize, nISize;
nOSize = _W.size(0);
nISize = _W.size(1);
int count = 0;
while (count < num) {
int idx = rand() % nOSize;
int idy = rand() % nISize;
std::cout << "_W[" << idx << "," << idy << "]=" << _W[idx][idy] << " ";
if (_bUseB) {
int idz = rand() % nOSize;
std::cout << "_b[0][" << idz << "]=" << _b[0][idz] << " ";
}
count++;
}
std::cout << std::endl;
}
inline void updateAdaGrad(dtype regularizationWeight, dtype adaAlpha, dtype adaEps) {
_gradW = _gradW + _W * regularizationWeight;
_eg2W = _eg2W + _gradW * _gradW;
_W = _W - _gradW * adaAlpha / F<nl_sqrt>(_eg2W + adaEps);
if (_bUseB) {
_gradb = _gradb + _b * regularizationWeight;
_eg2b = _eg2b + _gradb * _gradb;
_b = _b - _gradb * adaAlpha / F<nl_sqrt>(_eg2b + adaEps);
}
clearGrad();
}
inline void clearGrad() {
_gradW = 0;
if (_bUseB)
_gradb = 0;
}
void writeModel(LStream &outf) {
SaveBinary(outf, _W);
SaveBinary(outf, _b);
SaveBinary(outf, _gradW);
SaveBinary(outf, _gradb);
SaveBinary(outf, _eg2W);
SaveBinary(outf, _eg2b);
WriteBinary(outf, _bUseB);
WriteBinary(outf, _funcType);
// cout << "Unilayer " << _bUseB << _funcType << endl;
}
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);
ReadBinary(inf, _bUseB);
ReadBinary(inf, _funcType);
// cout << "Unilayer " << _bUseB << _funcType << endl;
}
};
#endif /* SRC_UniLayer_H_ */