-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBiOP.h
More file actions
460 lines (374 loc) · 11.8 KB
/
BiOP.h
File metadata and controls
460 lines (374 loc) · 11.8 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#ifndef BIOP_H_
#define BIOP_H_
/*
* BiOP.h:
* a simple feed forward neural operation, binary input.
*
* Created on: June 11, 2017
* Author: mszhang
*/
#include "Param.h"
#include "MyLib.h"
#include "Node.h"
#include "Graph.h"
#include "ModelUpdate.h"
class BiParams {
public:
Param W1;
Param W2;
Param b;
bool bUseB;
public:
BiParams() {
bUseB = true;
}
inline void exportAdaParams(ModelUpdate& ada) {
ada.addParam(&W1);
ada.addParam(&W2);
if (bUseB) {
ada.addParam(&b);
}
}
inline void initial(int nOSize, int nISize1, int nISize2, bool useB = true) {
W1.initial(nOSize, nISize1);
W2.initial(nOSize, nISize2);
bUseB = useB;
if (bUseB) {
b.initial(nOSize, 1);
}
}
inline void save(std::ofstream &os) const {
os << bUseB << std::endl;
W1.save(os);
W2.save(os);
if (bUseB) {
b.save(os);
}
}
inline void load(std::ifstream &is) {
is >> bUseB;
W1.load(is);
W2.load(is);
if (bUseB) {
b.load(is);
}
}
};
// non-linear feed-forward node
// input nodes should be specified by forward function
// for input variables, we exploit column vector,
// which means a concrete input vector x_i is represented by x(0, i), x(1, i), ..., x(n, i)
class BiNode : public Node {
public:
PNode in1, in2;
BiParams* param;
dtype(*activate)(const dtype&); // activation function
dtype(*derivate)(const dtype&, const dtype&); // derivation function of activation function
public:
BiNode() : Node() {
in1 = in2 = NULL;
activate = ftanh;
derivate = dtanh;
param = NULL;
node_type = "bi";
}
~BiNode() {
in1 = in2 = NULL;
}
inline void setParam(BiParams* paramInit) {
param = paramInit;
}
inline void clearValue() {
Node::clearValue();
in1 = in2 = NULL;
}
// define the activate function and its derivation form
inline void setFunctions(dtype(*f)(const dtype&), dtype(*f_deri)(const dtype&, const dtype&)) {
activate = f;
derivate = f_deri;
}
public:
void forward(Graph *cg, PNode x1, PNode x2) {
in1 = x1;
in2 = x2;
degree = 0;
in1->addParent(this);
in2->addParent(this);
cg->addNode(this);
}
public:
inline void compute(Tensor1D& ty) {
ty.mat() = param->W1.val.mat() * in1->val.mat() + param->W2.val.mat() * in2->val.mat();
if (param->bUseB) {
ty.vec() += param->b.val.vec();
}
val.vec() = ty.vec().unaryExpr(ptr_fun(activate));
}
inline void backward(Tensor1D& ty, Tensor1D& lty) {
lty.vec() = loss.vec() * ty.vec().binaryExpr(val.vec(), ptr_fun(derivate));
param->W1.grad.mat() += lty.mat() * in1->val.tmat();
param->W2.grad.mat() += lty.mat() * in2->val.tmat();
if (param->bUseB) {
param->b.grad.vec() += lty.vec();
}
in1->loss.mat() += param->W1.val.mat().transpose() * lty.mat();
in2->loss.mat() += param->W2.val.mat().transpose() * lty.mat();
}
public:
inline PExecute generate(bool bTrain);
// better to rewrite for deep understanding
inline bool typeEqual(PNode other) {
bool result = Node::typeEqual(other);
if (!result) return false;
BiNode* conv_other = (BiNode*)other;
if (param != conv_other->param) {
return false;
}
if (activate != conv_other->activate || derivate != conv_other->derivate) {
return false;
}
return true;
}
};
// non-linear feed-forward node
// input nodes should be specified by forward function
// for input variables, we exploit column vector,
// which means a concrete input vector x_i is represented by x(0, i), x(1, i), ..., x(n, i)
class LinearBiNode : public Node {
public:
PNode in1, in2;
BiParams* param;
public:
LinearBiNode() : Node() {
in1 = in2 = NULL;
param = NULL;
node_type = "linear_bi";
}
inline void setParam(BiParams* paramInit) {
param = paramInit;
}
inline void clearValue() {
Node::clearValue();
in1 = in2 = NULL;
}
public:
void forward(Graph *cg, PNode x1, PNode x2) {
in1 = x1;
in2 = x2;
degree = 0;
in1->addParent(this);
in2->addParent(this);
cg->addNode(this);
}
public:
inline void compute() {
val.mat() = param->W1.val.mat() * in1->val.mat() + param->W2.val.mat() * in2->val.mat();
if (param->bUseB) {
val.vec() += param->b.val.vec();
}
}
inline void backward() {
param->W1.grad.mat() += loss.mat() * in1->val.tmat();
param->W2.grad.mat() += loss.mat() * in2->val.tmat();
if (param->bUseB) {
param->b.grad.vec() += loss.vec();
}
in1->loss.mat() += param->W1.val.mat().transpose() * loss.mat();
in2->loss.mat() += param->W2.val.mat().transpose() * loss.mat();
}
public:
inline PExecute generate(bool bTrain);
// better to rewrite for deep understanding
inline bool typeEqual(PNode other) {
bool result = Node::typeEqual(other);
if (!result) return false;
LinearBiNode* conv_other = (LinearBiNode*)other;
if (param != conv_other->param) {
return false;
}
return true;
}
};
class BiExecute :public Execute {
public:
Tensor2D x1, x2, ty, y, b;
int inDim1, inDim2, outDim;
BiParams* param;
dtype(*activate)(const dtype&); // activation function
dtype(*derivate)(const dtype&, const dtype&); // derivation function of activation function
bool bTrain;
public:
~BiExecute() {
param = NULL;
activate = NULL;
derivate = NULL;
inDim1 = inDim2 = outDim = 0;
}
public:
inline void forward() {
int count = batch.size();
x1.init(inDim1, count);
x2.init(inDim2, count);
b.init(outDim, count);
ty.init(outDim, count);
y.init(outDim, count);
for (int idx = 0; idx < count; idx++) {
BiNode* ptr = (BiNode*)batch[idx];
for (int idy = 0; idy < inDim1; idy++) {
x1[idx][idy] = ptr->in1->val[idy];
}
for (int idy = 0; idy < inDim2; idy++) {
x2[idx][idy] = ptr->in2->val[idy];
}
if (param->bUseB) {
for (int idy = 0; idy < outDim; idy++) {
b[idx][idy] = param->b.val.v[idy];
}
}
}
ty.mat() = param->W1.val.mat() * x1.mat() + param->W2.val.mat() * x2.mat();
if (param->bUseB) {
ty.vec() = ty.vec() + b.vec();
}
y.vec() = ty.vec().unaryExpr(ptr_fun(activate));
for (int idx = 0; idx < count; idx++) {
BiNode* ptr = (BiNode*)batch[idx];
for (int idy = 0; idy < outDim; idy++) {
ptr->val[idy] = y[idx][idy];
}
ptr->forward_drop(bTrain);
}
}
inline void backward() {
int count = batch.size();
Tensor2D lx1, lx2, lty, ly;
lx1.init(inDim1, count);
lx2.init(inDim2, count);
lty.init(outDim, count);
ly.init(outDim, count);
for (int idx = 0; idx < count; idx++) {
BiNode* ptr = (BiNode*)batch[idx];
ptr->backward_drop();
for (int idy = 0; idy < outDim; idy++) {
ly[idx][idy] = ptr->loss[idy];
}
}
lty.vec() = ly.vec() * ty.vec().binaryExpr(y.vec(), ptr_fun(derivate));
param->W1.grad.mat() += lty.mat() * x1.mat().transpose();
param->W2.grad.mat() += lty.mat() * x2.mat().transpose();
if (param->bUseB) {
for (int idx = 0; idx < count; idx++) {
for (int idy = 0; idy < outDim; idy++) {
param->b.grad.v[idy] += lty[idx][idy];
}
}
}
lx1.mat() += param->W1.val.mat().transpose() * lty.mat();
lx2.mat() += param->W2.val.mat().transpose() * lty.mat();
for (int idx = 0; idx < count; idx++) {
BiNode* ptr = (BiNode*)batch[idx];
for (int idy = 0; idy < inDim1; idy++) {
ptr->in1->loss[idy] += lx1[idx][idy];
}
for (int idy = 0; idy < inDim2; idy++) {
ptr->in2->loss[idy] += lx2[idx][idy];
}
}
}
};
class LinearBiExecute :public Execute {
public:
Tensor2D x1, x2, y, b;
int inDim1, inDim2, outDim, count;
BiParams* param;
bool bTrain;
public:
inline void forward() {
count = batch.size();
x1.init(inDim1, count);
x2.init(inDim2, count);
b.init(outDim, count);
y.init(outDim, count);
for (int idx = 0; idx < count; idx++) {
LinearBiNode* ptr = (LinearBiNode*)batch[idx];
for (int idy = 0; idy < inDim1; idy++) {
x1[idx][idy] = ptr->in1->val[idy];
}
for (int idy = 0; idy < inDim2; idy++) {
x2[idx][idy] = ptr->in2->val[idy];
}
if (param->bUseB) {
for (int idy = 0; idy < outDim; idy++) {
b[idx][idy] = param->b.val.v[idy];
}
}
}
y.mat() = param->W1.val.mat() * x1.mat() + param->W2.val.mat() * x2.mat();
if (param->bUseB) {
y.vec() += b.vec();
}
for (int idx = 0; idx < count; idx++) {
LinearBiNode* ptr = (LinearBiNode*)batch[idx];
for (int idy = 0; idy < outDim; idy++) {
ptr->val[idy] = y[idx][idy];
}
ptr->forward_drop(bTrain);
}
}
inline void backward() {
Tensor2D lx1, lx2, ly;
lx1.init(inDim1, count);
lx2.init(inDim2, count);
ly.init(outDim, count);
for (int idx = 0; idx < count; idx++) {
LinearBiNode* ptr = (LinearBiNode*)batch[idx];
ptr->backward_drop();
for (int idy = 0; idy < outDim; idy++) {
ly[idx][idy] = ptr->loss[idy];
}
}
param->W1.grad.mat() += ly.mat() * x1.mat().transpose();
param->W2.grad.mat() += ly.mat() * x2.mat().transpose();
if (param->bUseB) {
for (int idx = 0; idx < count; idx++) {
for (int idy = 0; idy < outDim; idy++) {
param->b.grad.v[idy] += ly[idx][idy];
}
}
}
lx1.mat() += param->W1.val.mat().transpose() * ly.mat();
lx2.mat() += param->W2.val.mat().transpose() * ly.mat();
for (int idx = 0; idx < count; idx++) {
LinearBiNode* ptr = (LinearBiNode*)batch[idx];
for (int idy = 0; idy < inDim1; idy++) {
ptr->in1->loss[idy] += lx1[idx][idy];
}
for (int idy = 0; idy < inDim2; idy++) {
ptr->in2->loss[idy] += lx2[idx][idy];
}
}
}
};
inline PExecute BiNode::generate(bool bTrain) {
BiExecute* exec = new BiExecute();
exec->batch.push_back(this);
exec->inDim1 = param->W1.inDim();
exec->inDim2 = param->W2.inDim();
exec->outDim = param->W1.outDim();
exec->param = param;
exec->activate = activate;
exec->derivate = derivate;
exec->bTrain = bTrain;
return exec;
}
inline PExecute LinearBiNode::generate(bool bTrain) {
LinearBiExecute* exec = new LinearBiExecute();
exec->batch.push_back(this);
exec->inDim1 = param->W1.inDim();
exec->inDim2 = param->W2.inDim();
exec->outDim = param->W1.outDim();
exec->param = param;
exec->bTrain = bTrain;
return exec;
}
#endif /* BIOP_H_ */