forked from zhangmeishan/EGN3LDG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRNN.h
167 lines (137 loc) · 3.1 KB
/
RNN.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
#ifndef RNN
#define RNN
#include "BiOP.h"
#include "Graph.h"
struct RNNParams {
BiParams _rnn;
RNNParams() {
}
inline void exportAdaParams(ModelUpdate& ada) {
_rnn.exportAdaParams(ada);
}
inline void initial(int nOSize, int nISize, AlignedMemoryPool* mem = NULL) {
_rnn.initial(nOSize, nOSize, nISize, true, mem);
}
inline int inDim(){
return _rnn.W2.inDim();
}
inline int outDim(){
return _rnn.W2.outDim();
}
};
class RNNBuilder{
public:
int _nSize;
int _inDim;
int _outDim;
Node _bucket;
vector<BiNode> _output;
RNNParams* _params;
bool _left2right;
public:
~RNNBuilder() {
clear();
}
RNNBuilder() {
clear();
}
public:
inline void init(RNNParams* paramInit, dtype dropout, bool left2right = true, AlignedMemoryPool* mem = NULL) {
_params = paramInit;
_inDim = _params->_rnn.W2.inDim();
_outDim = _params->_rnn.W2.outDim();
int maxsize = _output.size();
for (int idx = 0; idx < maxsize; idx++){
_output[idx].setParam(&_params->_rnn);
_output[idx].setFunctions(&ftanh, &dtanh);
_output[idx].init(_outDim, dropout, mem);
}
_left2right = left2right;
_bucket.init(_outDim, -1, mem);
_bucket.set_bucket();
}
inline void resize(int maxsize){
_output.resize(maxsize);
}
inline void clear() {
_output.clear();
_nSize = 0;
_inDim = 0;
_outDim = 0;
_left2right = true;
_params = NULL;
}
inline void forward(Graph *cg, const vector<PNode>& x) {
if (x.size() == 0){
std::cout << "empty inputs for RNN operation" << std::endl;
return;
}
_nSize = x.size();
if (x[0]->val.dim != _inDim) {
std::cout << "input dim dose not match for seg 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)
_output[idx].forward(cg, &_bucket, x[idx]);
else
_output[idx].forward(cg, &_output[idx - 1], x[idx]);
}
}
inline void right2left_forward(Graph *cg, const vector<PNode>& x) {
for (int idx = _nSize - 1; idx >= 0; idx--) {
if (idx == _nSize - 1)
_output[idx].forward(cg, &_bucket, x[idx]);
else
_output[idx].forward(cg, &_output[idx + 1], x[idx]);
}
}
};
class IncRNNBuilder{
public:
int _nSize;
int _inDim;
int _outDim;
Node _bucket;
BiNode _output;
RNNParams* _params;
public:
~IncRNNBuilder() {
clear();
}
IncRNNBuilder() {
clear();
}
public:
inline void init(RNNParams* paramInit, dtype dropout, AlignedMemoryPool* mem = NULL) {
_params = paramInit;
_inDim = _params->_rnn.W2.inDim();
_outDim = _params->_rnn.W2.outDim();
_output.setParam(&_params->_rnn);
_output.setFunctions(&ftanh, &dtanh);
_output.init(_outDim, dropout, mem);
_bucket.init(_outDim, -1, mem);
_bucket.set_bucket();
}
inline void clear() {
_nSize = 0;
_inDim = 0;
_outDim = 0;
_params = NULL;
}
public:
inline void forward(Graph *cg, PNode x, IncRNNBuilder* prev = NULL) {
if (prev == NULL)
_output.forward(cg, &_bucket, x);
else
_output.forward(cg, &prev->_output, x);
}
};
#endif