forked from larryniven/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq2seq.cc
203 lines (153 loc) · 7.55 KB
/
seq2seq.cc
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
#include "nn/seq2seq.h"
#include "nn/lstm-tensor-tree.h"
#include "nn/lstm-frame.h"
namespace seq2seq {
std::shared_ptr<tensor_tree::vertex> make_tensor_tree(int encoder_layers)
{
tensor_tree::vertex root { "nil" };
root.children.push_back(lstm_frame::make_tensor_tree(encoder_layers));
root.children.push_back(lstm::lstm_tensor_tree_factory()());
root.children.push_back(tensor_tree::make_tensor("label embedding"));
root.children.push_back(tensor_tree::make_tensor("pred softmax mul"));
root.children.push_back(tensor_tree::make_tensor("pred softmax bias"));
root.children.push_back(tensor_tree::make_tensor("initial cell"));
return std::make_shared<tensor_tree::vertex>(root);
}
attention::~attention()
{}
std::shared_ptr<autodiff::op_t>
bilinear_attention::operator()(std::shared_ptr<autodiff::op_t> output,
std::shared_ptr<autodiff::op_t> hidden,
int nhidden,
int cell_dim)
{
auto output_col = autodiff::weak_var(output, 0,
std::vector<unsigned int> { (unsigned int) cell_dim, 1 });
return autodiff::weak_var(autodiff::mul(hidden, output_col), 0,
std::vector<unsigned int> { (unsigned int) nhidden });
}
std::shared_ptr<autodiff::op_t>
bilinear_softmax_attention::operator()(std::shared_ptr<autodiff::op_t> output,
std::shared_ptr<autodiff::op_t> hidden,
int nhidden,
int cell_dim)
{
auto output_col = autodiff::weak_var(output, 0,
std::vector<unsigned int> { (unsigned int) cell_dim, 1 });
return autodiff::softmax(autodiff::weak_var(autodiff::mul(hidden, output_col), 0,
std::vector<unsigned int> { (unsigned int) nhidden }));
}
seq2seq_nn_t make_training_nn(
std::vector<int> labels,
int label_set_size,
std::shared_ptr<autodiff::op_t> hidden,
int nhidden,
int cell_dim,
std::shared_ptr<tensor_tree::vertex> var_tree,
attention& att_func)
{
auto& comp_graph = *hidden->graph;
std::shared_ptr<autodiff::op_t> cell = tensor_tree::get_var(var_tree->children[5]);
std::shared_ptr<autodiff::op_t> output = autodiff::tanh(cell);
la::cpu::tensor<double> pred_storage_t;
pred_storage_t.resize(std::vector<unsigned int> { (unsigned int) labels.size(),
(unsigned int) label_set_size });
auto pred_storage = comp_graph.var(pred_storage_t);
std::vector<std::shared_ptr<autodiff::op_t>> preds;
seq2seq_nn_t result;
for (int i = 0; i < labels.size(); ++i) {
auto att = att_func(output, hidden, nhidden, cell_dim);
result.atts.push_back(att);
auto c = autodiff::mul(att, hidden);
std::shared_ptr<autodiff::op_t> pred_embedding = c;
// TODO: inefficient
// one copy from logsoftmax to pred_storage can be eliminated
// but this needs a special logsoftmax_to operation
auto pred = autodiff::add_to(autodiff::row_at(pred_storage, i),
std::vector<std::shared_ptr<autodiff::op_t>> {
autodiff::logsoftmax(autodiff::add(tensor_tree::get_var(var_tree->children[4]),
autodiff::mul(pred_embedding, tensor_tree::get_var(var_tree->children[3]))))
});
preds.push_back(pred);
auto input = autodiff::weak_var(autodiff::row_at(
tensor_tree::get_var(var_tree->children[2]), labels[i]),
0, std::vector<unsigned int> {1, (unsigned int) cell_dim});
input = autodiff::add(
autodiff::mul(input, tensor_tree::get_var(var_tree->children[1]->children[0])),
tensor_tree::get_var(var_tree->children[1]->children[1]));
la::cpu::tensor<double> output_storage_t;
output_storage_t.resize({(unsigned int) cell_dim});
auto output_storage = comp_graph.var(output_storage_t);
lstm::lstm_step_nn_t lstm_step = lstm::make_lstm_step_nn(input, output, cell,
tensor_tree::get_var(var_tree->children[1]->children[2]),
tensor_tree::get_var(var_tree->children[1]->children[3]),
tensor_tree::get_var(var_tree->children[1]->children[4]),
tensor_tree::get_var(var_tree->children[1]->children[5]),
nullptr,
output_storage,
1,
cell_dim);
output = lstm_step.output;
cell = lstm_step.cell;
}
result.pred = autodiff::weak_cat(preds, pred_storage);
return result;
}
std::vector<int> decode(
std::vector<std::string> const& id_label,
std::shared_ptr<autodiff::op_t> hidden,
int nhidden,
int cell_dim,
std::shared_ptr<tensor_tree::vertex> var_tree,
attention& att_func)
{
std::vector<int> result;
auto& comp_graph = *hidden->graph;
std::shared_ptr<autodiff::op_t> cell = tensor_tree::get_var(var_tree->children[5]);
std::shared_ptr<autodiff::op_t> output = autodiff::tanh(cell);
std::vector<std::shared_ptr<autodiff::op_t>> preds;
while (result.size() == 0 || id_label[result.back()] != "<eos>") {
auto att = att_func(output, hidden, nhidden, cell_dim);
auto c = autodiff::mul(att, hidden);
std::shared_ptr<autodiff::op_t> pred_embedding = c;
// TODO: inefficient
// one copy from logsoftmax to pred_storage can be eliminated
// but this needs a special logsoftmax_to operation
auto pred = autodiff::logsoftmax(autodiff::add(tensor_tree::get_var(var_tree->children[4]),
autodiff::mul(pred_embedding, tensor_tree::get_var(var_tree->children[3]))));
preds.push_back(pred);
auto& pred_t = autodiff::get_output<la::cpu::tensor_like<double>>(pred);
double max = -std::numeric_limits<double>::infinity();
int argmax = -1;
for (int i = 0; i < pred_t.vec_size(); ++i) {
if (pred_t.data()[i] > max) {
argmax = i;
max = pred_t.data()[i];
}
}
result.push_back(argmax);
// TODO: weak_var necessary?
auto input = autodiff::weak_var(autodiff::row_at(
tensor_tree::get_var(var_tree->children[2]), argmax),
0, std::vector<unsigned int> {1, (unsigned int) cell_dim});
input = autodiff::add(
autodiff::mul(input, tensor_tree::get_var(var_tree->children[1]->children[0])),
tensor_tree::get_var(var_tree->children[1]->children[1]));
la::cpu::tensor<double> output_storage_t;
output_storage_t.resize({(unsigned int) cell_dim});
auto output_storage = comp_graph.var(output_storage_t);
lstm::lstm_step_nn_t lstm_step = lstm::make_lstm_step_nn(input, output, cell,
tensor_tree::get_var(var_tree->children[1]->children[2]),
tensor_tree::get_var(var_tree->children[1]->children[3]),
tensor_tree::get_var(var_tree->children[1]->children[4]),
tensor_tree::get_var(var_tree->children[1]->children[5]),
nullptr,
output_storage,
1,
cell_dim);
output = lstm_step.output;
cell = lstm_step.cell;
}
return result;
}
}