-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLSTM1.h
More file actions
398 lines (292 loc) · 10.3 KB
/
LSTM1.h
File metadata and controls
398 lines (292 loc) · 10.3 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
#ifndef LSTM1
#define LSTM1
/*
* LSTM1.h:
* LSTM variation 1
*
* Created on: June 13, 2017
* Author: mszhang
*/
#include "MyLib.h"
#include "Node.h"
#include "BiOP.h"
#include "AtomicOP.h"
#include "Graph.h"
#include "PMultiOP.h"
#include "PAddOP.h"
#include "BucketOP.h"
struct LSTM1Params {
BiParams input;
BiParams output;
BiParams forget;
BiParams cell;
LSTM1Params() {
}
inline void exportAdaParams(ModelUpdate& ada) {
input.exportAdaParams(ada);
output.exportAdaParams(ada);
forget.exportAdaParams(ada);
cell.exportAdaParams(ada);
}
inline void initial(int nOSize, int nISize) {
input.initial(nOSize, nOSize, nISize, true);
output.initial(nOSize, nOSize, nISize, true);
forget.initial(nOSize, nOSize, nISize, true);
cell.initial(nOSize, nOSize, nISize, true);
}
inline int inDim() {
return input.W2.inDim();
}
inline int outDim() {
return input.W2.outDim();
}
inline void save(std::ofstream &os) const {
input.save(os);
output.save(os);
forget.save(os);
cell.save(os);
}
inline void load(std::ifstream &is) {
input.load(is);
output.load(is);
forget.load(is);
cell.load(is);
}
};
// standard LSTM1 using tanh as activation function
// other conditions are not implemented unless they are clear
class LSTM1Builder {
public:
int _nSize;
int _inDim;
int _outDim;
vector<BiNode> _inputgates;
vector<BiNode> _forgetgates;
vector<BiNode> _halfcells;
vector<PMultiNode> _inputfilters;
vector<PMultiNode> _forgetfilters;
vector<PAddNode> _cells;
vector<BiNode> _outputgates;
vector<TanhNode> _halfhiddens;
vector<PMultiNode> _hiddens; // intermediate result without dropout
BucketNode _bucket;
LSTM1Params* _param;
bool _left2right;
public:
LSTM1Builder() {
clear();
}
~LSTM1Builder() {
clear();
}
public:
inline void init(LSTM1Params* paramInit, dtype dropout, bool left2right = true) {
_param = paramInit;
_inDim = _param->input.W2.inDim();
_outDim = _param->input.W2.outDim();
int maxsize = _inputgates.size();
for (int idx = 0; idx < maxsize; idx++) {
_inputgates[idx].setParam(&_param->input);
_forgetgates[idx].setParam(&_param->forget);
_outputgates[idx].setParam(&_param->output);
_halfcells[idx].setParam(&_param->cell);
_inputgates[idx].setFunctions(&fsigmoid, &dsigmoid);
_forgetgates[idx].setFunctions(&fsigmoid, &dsigmoid);
_outputgates[idx].setFunctions(&fsigmoid, &dsigmoid);
_halfcells[idx].setFunctions(&ftanh, &dtanh);
}
_left2right = left2right;
for (int idx = 0; idx < maxsize; idx++) {
_inputgates[idx].init(_outDim, -1);
_forgetgates[idx].init(_outDim, -1);
_halfcells[idx].init(_outDim, -1);
_inputfilters[idx].init(_outDim, -1);
_forgetfilters[idx].init(_outDim, -1);
_cells[idx].init(_outDim, -1);
_outputgates[idx].init(_outDim, -1);
_halfhiddens[idx].init(_outDim, -1);
_hiddens[idx].init(_outDim, dropout);
}
_bucket.init(_outDim, -1);
}
inline void resize(int maxsize) {
_inputgates.resize(maxsize);
_forgetgates.resize(maxsize);
_halfcells.resize(maxsize);
_inputfilters.resize(maxsize);
_forgetfilters.resize(maxsize);
_cells.resize(maxsize);
_outputgates.resize(maxsize);
_halfhiddens.resize(maxsize);
_hiddens.resize(maxsize);
}
//whether vectors have been allocated
inline bool empty() {
return _hiddens.empty();
}
inline void clear() {
_inputgates.clear();
_forgetgates.clear();
_halfcells.clear();
_inputfilters.clear();
_forgetfilters.clear();
_cells.clear();
_outputgates.clear();
_halfhiddens.clear();
_hiddens.clear();
_left2right = true;
_param = NULL;
_nSize = 0;
_inDim = 0;
_outDim = 0;
}
public:
inline void forward(Graph *cg, const vector<PNode>& x) {
if (x.size() == 0) {
std::cout << "empty inputs for lstm operation" << std::endl;
return;
}
_nSize = x.size();
if (x[0]->val.dim != _inDim) {
std::cout << "input dim does not match for lstm operation" << std::endl;
return;
}
if (_left2right) {
left2right_forward(cg, x);
} else {
right2left_forward(cg, x);
}
}
protected:
inline void left2right_forward(Graph *cg, const vector<PNode>& x) {
for (int idx = 0; idx < _nSize; idx++) {
if (idx == 0) {
_bucket.forward(cg, 0);
_inputgates[idx].forward(cg, &_bucket, x[idx]);
_halfcells[idx].forward(cg, &_bucket, x[idx]);
_inputfilters[idx].forward(cg, &_halfcells[idx], &_inputgates[idx]);
_cells[idx].forward(cg, &_inputfilters[idx], &_bucket);
_halfhiddens[idx].forward(cg, &_cells[idx]);
_outputgates[idx].forward(cg, &_bucket, x[idx]);
_hiddens[idx].forward(cg, &_halfhiddens[idx], &_outputgates[idx]);
} else {
_inputgates[idx].forward(cg, &_hiddens[idx - 1], x[idx]);
_forgetgates[idx].forward(cg, &_hiddens[idx - 1], x[idx]);
_halfcells[idx].forward(cg, &_hiddens[idx - 1], x[idx]);
_inputfilters[idx].forward(cg, &_halfcells[idx], &_inputgates[idx]);
_forgetfilters[idx].forward(cg, &_cells[idx - 1], &_forgetgates[idx]);
_cells[idx].forward(cg, &_inputfilters[idx], &_forgetfilters[idx]);
_halfhiddens[idx].forward(cg, &_cells[idx]);
_outputgates[idx].forward(cg, &_hiddens[idx - 1], x[idx]);
_hiddens[idx].forward(cg, &_halfhiddens[idx], &_outputgates[idx]);
}
}
}
inline void right2left_forward(Graph *cg, const vector<PNode>& x) {
for (int idx = _nSize - 1; idx >= 0; idx--) {
if (idx == _nSize - 1) {
_bucket.forward(cg, 0);
_inputgates[idx].forward(cg, &_bucket, x[idx]);
_halfcells[idx].forward(cg, &_bucket, x[idx]);
_inputfilters[idx].forward(cg, &_halfcells[idx], &_inputgates[idx]);
_cells[idx].forward(cg, &_inputfilters[idx], &_bucket);
_halfhiddens[idx].forward(cg, &_cells[idx]);
_outputgates[idx].forward(cg, &_bucket, x[idx]);
_hiddens[idx].forward(cg, &_halfhiddens[idx], &_outputgates[idx]);
} else {
_inputgates[idx].forward(cg, &_hiddens[idx + 1], x[idx]);
_forgetgates[idx].forward(cg, &_hiddens[idx + 1], x[idx]);
_halfcells[idx].forward(cg, &_hiddens[idx + 1], x[idx]);
_inputfilters[idx].forward(cg, &_halfcells[idx], &_inputgates[idx]);
_forgetfilters[idx].forward(cg, &_cells[idx + 1], &_forgetgates[idx]);
_cells[idx].forward(cg, &_inputfilters[idx], &_forgetfilters[idx]);
_halfhiddens[idx].forward(cg, &_cells[idx]);
_outputgates[idx].forward(cg, &_hiddens[idx + 1], x[idx]);
_hiddens[idx].forward(cg, &_halfhiddens[idx], &_outputgates[idx]);
}
}
}
};
class IncLSTM1Builder {
public:
int _nSize;
int _inDim;
int _outDim;
IncLSTM1Builder* _pPrev;
BiNode _inputgate;
BiNode _forgetgate;
BiNode _halfcell;
PMultiNode _inputfilter;
PMultiNode _forgetfilter;
PAddNode _cell;
BiNode _outputgate;
TanhNode _halfhidden;
PMultiNode _hidden; // intermediate result without dropout
BucketNode _bucket;
LSTM1Params* _param;
public:
IncLSTM1Builder() {
clear();
}
~IncLSTM1Builder() {
clear();
}
void clear() {
_nSize = 0;
_inDim = 0;
_outDim = 0;
_param = NULL;
_pPrev = NULL;
}
public:
inline void init(LSTM1Params* paramInit, dtype dropout) {
_param = paramInit;
_inDim = _param->input.W2.inDim();
_outDim = _param->input.W2.outDim();
_inputgate.setParam(&_param->input);
_forgetgate.setParam(&_param->forget);
_outputgate.setParam(&_param->output);
_halfcell.setParam(&_param->cell);
_inputgate.setFunctions(&fsigmoid, &dsigmoid);
_forgetgate.setFunctions(&fsigmoid, &dsigmoid);
_outputgate.setFunctions(&fsigmoid, &dsigmoid);
_halfcell.setFunctions(&ftanh, &dtanh);
_inputgate.init(_outDim, -1);
_forgetgate.init(_outDim, -1);
_halfcell.init(_outDim, -1);
_inputfilter.init(_outDim, -1);
_forgetfilter.init(_outDim, -1);
_cell.init(_outDim, -1);
_outputgate.init(_outDim, -1);
_halfhidden.init(_outDim, -1);
_hidden.init(_outDim, dropout);
_bucket.init(_outDim, -1);
}
public:
inline void forward(Graph *cg, PNode x, IncLSTM1Builder* prev = NULL) {
if (prev == NULL) {
_bucket.forward(cg, 0);
_inputgate.forward(cg, &_bucket, x);
_halfcell.forward(cg, &_bucket, x);
_inputfilter.forward(cg, &_halfcell, &_inputgate);
_cell.forward(cg, &_inputfilter, &_bucket);
_halfhidden.forward(cg, &_cell);
_outputgate.forward(cg, &_bucket, x);
_hidden.forward(cg, &_halfhidden, &_outputgate);
_nSize = 1;
} else {
_inputgate.forward(cg, &(prev->_hidden), x);
_forgetgate.forward(cg, &(prev->_hidden), x);
_halfcell.forward(cg, &(prev->_hidden), x);
_inputfilter.forward(cg, &_halfcell, &_inputgate);
_forgetfilter.forward(cg, &(prev->_cell), &_forgetgate);
_cell.forward(cg, &_inputfilter, &_forgetfilter);
_halfhidden.forward(cg, &_cell);
_outputgate.forward(cg, &(prev->_hidden), x);
_hidden.forward(cg, &_halfhidden, &_outputgate);
_nSize = prev->_nSize + 1;
}
_pPrev = prev;
}
};
#endif