-
Notifications
You must be signed in to change notification settings - Fork 4
/
lstm.h
204 lines (147 loc) · 5.52 KB
/
lstm.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
#ifndef LSTM_H
#define LSTM_H
#include "autodiff/autodiff.h"
#include <random>
#include "nn/tensor-tree.h"
namespace lstm {
// lstm
struct lstm_step_nn_t {
std::shared_ptr<autodiff::op_t> input_gate;
std::shared_ptr<autodiff::op_t> output_gate;
std::shared_ptr<autodiff::op_t> forget_gate;
std::shared_ptr<autodiff::op_t> output;
std::shared_ptr<autodiff::op_t> cell;
std::shared_ptr<autodiff::op_t> input;
};
lstm_step_nn_t make_lstm_step_nn(
std::shared_ptr<autodiff::op_t> input,
std::shared_ptr<autodiff::op_t> prev_output,
std::shared_ptr<autodiff::op_t> prev_cell,
std::shared_ptr<autodiff::op_t> output_weight,
std::shared_ptr<autodiff::op_t> cell2i,
std::shared_ptr<autodiff::op_t> cell2f,
std::shared_ptr<autodiff::op_t> cell2o,
std::shared_ptr<autodiff::op_t> cell_mask,
std::shared_ptr<autodiff::op_t> output_storage,
int batch_size,
int cell_dim);
lstm_step_nn_t make_dyer_lstm_step_nn(
std::shared_ptr<autodiff::op_t> input,
std::shared_ptr<autodiff::op_t> prev_output,
std::shared_ptr<autodiff::op_t> prev_cell,
std::shared_ptr<autodiff::op_t> output_weight,
std::shared_ptr<autodiff::op_t> cell2i,
std::shared_ptr<autodiff::op_t> cell2o,
std::shared_ptr<autodiff::op_t> cell_mask,
std::shared_ptr<autodiff::op_t> output_storage,
int batch_size,
int cell_dim);
// tanscriber
struct trans_seq_t {
int nframes;
int batch_size;
int dim;
std::shared_ptr<autodiff::op_t> feat;
std::shared_ptr<autodiff::op_t> mask;
};
trans_seq_t make_trans_seq(std::shared_ptr<autodiff::op_t> t);
struct transcriber {
virtual ~transcriber();
virtual bool require_param() const;
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const = 0;
};
struct lstm_transcriber
: public transcriber {
int cell_dim;
bool reverse;
lstm_transcriber(int cell_dim, bool reverse = false);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct dyer_lstm_transcriber
: public transcriber {
int cell_dim;
bool reverse;
dyer_lstm_transcriber(int cell_dim, bool reverse = false);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct input_dropout_transcriber
: public transcriber {
std::shared_ptr<transcriber> base;
double prob;
std::default_random_engine& gen;
input_dropout_transcriber(std::shared_ptr<transcriber> base,
double prob, std::default_random_engine& gen);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct output_dropout_transcriber
: public transcriber {
std::shared_ptr<transcriber> base;
double prob;
std::default_random_engine& gen;
output_dropout_transcriber(std::shared_ptr<transcriber> base,
double prob, std::default_random_engine& gen);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct bi_transcriber
: public transcriber {
int output_dim;
std::shared_ptr<transcriber> f_base;
std::shared_ptr<transcriber> b_base;
bi_transcriber(int output_dim, std::shared_ptr<transcriber> f_base,
std::shared_ptr<transcriber> b_base);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct layered_transcriber
: public transcriber {
std::vector<std::shared_ptr<transcriber>> layer;
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct fc_transcriber
: public transcriber {
int output_dim;
fc_transcriber(int output_dim);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct logsoftmax_transcriber
: public transcriber {
virtual bool require_param() const override;
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct res_transcriber
: public transcriber {
std::shared_ptr<transcriber> base;
res_transcriber(std::shared_ptr<transcriber> base);
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
struct subsampled_transcriber
: public transcriber {
int freq;
int shift;
subsampled_transcriber(int freq, int shift);
virtual bool require_param() const override;
virtual trans_seq_t operator()(
std::shared_ptr<tensor_tree::vertex> var_tree,
trans_seq_t const& seq) const override;
};
}
#endif