forked from zhangmeishan/EGN3LDG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TriOP.h
260 lines (207 loc) · 5.12 KB
/
TriOP.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
/*
* TriOP.h
*
* Created on: Jul 20, 2016
* Author: mason
*/
#ifndef TRIOP_H_
#define TRIOP_H_
#include "Param.h"
#include "MyLib.h"
#include "Node.h"
#include "Graph.h"
struct TriParams {
public:
Param W1;
Param W2;
Param W3;
Param b;
bool bUseB;
public:
TriParams() {
bUseB = true;
}
inline void exportAdaParams(ModelUpdate& ada) {
ada.addParam(&W1);
ada.addParam(&W2);
ada.addParam(&W3);
if (bUseB) {
ada.addParam(&b);
}
}
inline void initial(int nOSize, int nISize1, int nISize2, int nISize3, bool useB = true, AlignedMemoryPool* mem = NULL) {
W1.initial(nOSize, nISize1, mem);
W2.initial(nOSize, nISize2, mem);
W3.initial(nOSize, nISize3, mem);
bUseB = useB;
if(bUseB){
b.initial(nOSize, 1, mem);
}
}
inline void save(std::ofstream &os) const {
os << bUseB << std::endl;
W1.save(os);
W2.save(os);
W3.save(os);
if (bUseB) {
b.save(os);
}
}
inline void load(std::ifstream &is, AlignedMemoryPool* mem = NULL) {
is >> bUseB;
W1.load(is, mem);
W2.load(is, mem);
W3.load(is, mem);
if (bUseB) {
b.load(is, mem);
}
}
};
// 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)
struct TriNode : Node {
public:
PNode in1, in2, in3;
Tensor1D ty, lty; // t means temp, ty is to save temp vector before activation
int inDim1, inDim2, inDim3;
TriParams* param;
dtype (*activate)(const dtype&); // activation function
dtype (*derivate)(const dtype&, const dtype&); // derivation function of activation function
public:
TriNode() : Node(){
in1 = NULL;
in2 = NULL;
in3 = NULL;
activate = ftanh;
derivate = dtanh;
param = NULL;
inDim1 = 0;
inDim2 = 0;
inDim3 = 0;
}
inline void setParam(TriParams* paramInit) {
param = paramInit;
inDim1 = param->W1.inDim();
inDim2 = param->W2.inDim();
inDim3 = param->W3.inDim();
}
inline void clearValue(){
Node::clearValue();
in1 = NULL;
in2 = NULL;
in3 = NULL;
ty.zero();
lty.zero();
}
// define the activation function and its derivation form
inline void setFunctions(dtype (*f)(const dtype&), dtype (*f_deri)(const dtype&, const dtype&)) {
activate = f;
derivate = f_deri;
}
inline void init(int dim, dtype dropOut, AlignedMemoryPool* mem = NULL){
Node::init(dim, dropOut, mem);
ty.init(dim, mem);
lty.init(dim, mem);
}
public:
void forward(Graph *cg, PNode x1, PNode x2, PNode x3) {
in1 = x1;
in2 = x2;
in3 = x3;
ty.mat() = param->W1.val.mat() * in1->val.mat() + param->W2.val.mat() * in2->val.mat() + param->W3.val.mat() * in3->val.mat();
if(param->bUseB){
ty.vec() += param->b.val.vec();
}
val.vec() = ty.vec().unaryExpr(ptr_fun(activate));
in1->increase_loc();
in2->increase_loc();
in3->increase_loc();
cg->addNode(this);
}
void backward() {
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();
param->W3.grad.mat() += lty.mat() * in3->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();
in3->loss.mat() += param->W3.val.mat().transpose() * lty.mat();
}
inline void unlock(){
in1->decrease_loc();
in2->decrease_loc();
in3->decrease_loc();
if(!lossed)return;
in1->lossed = true;
in2->lossed = true;
in3->lossed = true;
}
};
struct LinearTriNode : Node {
public:
PNode in1, in2, in3;
int inDim1, inDim2, inDim3;
TriParams* param;
public:
LinearTriNode() : Node(){
in1 = NULL;
in2 = NULL;
in3 = NULL;
param = NULL;
inDim1 = 0;
inDim2 = 0;
inDim3 = 0;
}
inline void setParam(TriParams* paramInit) {
param = paramInit;
inDim1 = param->W1.inDim();
inDim2 = param->W2.inDim();
inDim3 = param->W3.inDim();
}
inline void clearValue(){
Node::clearValue();
in1 = NULL;
in2 = NULL;
in3 = NULL;
}
public:
void forward(Graph *cg, PNode x1, PNode x2, PNode x3) {
in1 = x1;
in2 = x2;
in3 = x3;
val.mat() = param->W1.val.mat() * in1->val.mat() + param->W2.val.mat() * in2->val.mat() + param->W3.val.mat() * in3->val.mat();
if(param->bUseB){
val.vec() += param->b.val.vec();
}
in1->increase_loc();
in2->increase_loc();
in3->increase_loc();
cg->addNode(this);
}
void backward() {
param->W1.grad.mat() += loss.mat() * in1->val.tmat();
param->W2.grad.mat() += loss.mat() * in2->val.tmat();
param->W3.grad.mat() += loss.mat() * in3->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();
in3->loss.mat() += param->W3.val.mat().transpose() * loss.mat();
}
inline void unlock(){
in1->decrease_loc();
in2->decrease_loc();
in3->decrease_loc();
if(!lossed)return;
in1->lossed = true;
in2->lossed = true;
in3->lossed = true;
}
};
#endif /* TRIOP_H_ */