forked from larryniven/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm-seg.cc
138 lines (113 loc) · 5.2 KB
/
lstm-seg.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
#include "nn/lstm-seg.h"
#include "nn/lstm-tensor-tree.h"
namespace lstm_seg {
std::shared_ptr<tensor_tree::vertex> make_tensor_tree(int layer,
std::unordered_map<std::string, std::string> const& args)
{
if (ebt::in(std::string("endpoints"), args)) {
return lstm_seg::endpoints::make_tensor_tree(layer);
} else {
return lstm_seg::make_tensor_tree(layer);
}
}
std::shared_ptr<autodiff::op_t> make_pred_nn(
autodiff::computation_graph& graph,
lstm::stacked_bi_lstm_nn_t& nn,
std::shared_ptr<tensor_tree::vertex> var_tree,
std::shared_ptr<tensor_tree::vertex> param,
std::unordered_map<std::string, std::string> const& args)
{
if (ebt::in(std::string("uniform-att"), args)) {
la::vector<double>& h = tensor_tree::get_vector(
param->children[0]->children.back()->children.back());
return lstm_seg::make_pred_nn_uniform(graph, nn, var_tree, h.size());
} else if (ebt::in(std::string("endpoints"), args)) {
return lstm_seg::endpoints::make_pred_nn(graph, nn, var_tree);
} else {
return lstm_seg::make_pred_nn(graph, nn, var_tree);
}
}
std::shared_ptr<tensor_tree::vertex> make_tensor_tree(int layer)
{
tensor_tree::vertex v { tensor_tree::tensor_t::nil };
v.children.push_back(lstm::make_stacked_bi_lstm_tensor_tree(layer));
v.children.push_back(tensor_tree::make_matrix());
v.children.push_back(tensor_tree::make_vector());
return std::make_shared<tensor_tree::vertex>(v);
}
std::shared_ptr<autodiff::op_t> make_pred_nn(
autodiff::computation_graph& graph,
lstm::stacked_bi_lstm_nn_t& nn,
std::shared_ptr<tensor_tree::vertex> var_tree)
{
std::shared_ptr<autodiff::op_t> hs = autodiff::col_cat(nn.layer.back().output);
std::shared_ptr<autodiff::op_t> att_weight
= autodiff::softmax(autodiff::lmul(tensor_tree::get_var(var_tree->children[2]), hs));
std::shared_ptr<autodiff::op_t> phi = autodiff::mul(hs, att_weight);
std::shared_ptr<autodiff::op_t> pred_var = autodiff::logsoftmax(
autodiff::mul(tensor_tree::get_var(var_tree->children[1]), phi));
return pred_var;
}
std::shared_ptr<autodiff::op_t> make_pred_nn_uniform(
autodiff::computation_graph& graph,
lstm::stacked_bi_lstm_nn_t& nn,
std::shared_ptr<tensor_tree::vertex> var_tree,
int h_dim)
{
std::shared_ptr<autodiff::op_t> h = autodiff::add(nn.layer.back().output);
la::vector<double> v;
v.resize(h_dim, 1.0 / nn.layer.back().output.size());
std::shared_ptr<autodiff::op_t> z = graph.var(v);
std::shared_ptr<autodiff::op_t> phi = autodiff::emul(h, z);
std::shared_ptr<autodiff::op_t> pred_var = autodiff::logsoftmax(
autodiff::mul(tensor_tree::get_var(var_tree->children[1]), phi));
return pred_var;
}
namespace endpoints {
std::shared_ptr<tensor_tree::vertex> make_tensor_tree(int layer)
{
tensor_tree::vertex v { tensor_tree::tensor_t::nil };
v.children.push_back(lstm::make_stacked_bi_lstm_tensor_tree(layer));
v.children.push_back(tensor_tree::make_matrix());
v.children.push_back(tensor_tree::make_matrix());
return std::make_shared<tensor_tree::vertex>(v);
}
std::shared_ptr<autodiff::op_t> make_pred_nn(
autodiff::computation_graph& graph,
lstm::stacked_bi_lstm_nn_t& nn,
std::shared_ptr<tensor_tree::vertex> var_tree)
{
return autodiff::logsoftmax(autodiff::add(
autodiff::mul(tensor_tree::get_var(var_tree->children[1]),
nn.layer.back().output.front()),
autodiff::mul(tensor_tree::get_var(var_tree->children[2]),
nn.layer.back().output.back())));
}
}
namespace logp {
std::shared_ptr<tensor_tree::vertex> make_tensor_tree(int layer)
{
tensor_tree::vertex v { tensor_tree::tensor_t::nil };
v.children.push_back(lstm::make_stacked_bi_lstm_tensor_tree(layer));
v.children.push_back(tensor_tree::make_matrix());
v.children.push_back(tensor_tree::make_vector());
return std::make_shared<tensor_tree::vertex>(v);
}
std::shared_ptr<autodiff::op_t> make_pred_nn(
autodiff::computation_graph& graph,
lstm::stacked_bi_lstm_nn_t& nn,
std::shared_ptr<tensor_tree::vertex> var_tree,
int label_dim)
{
std::vector<std::shared_ptr<autodiff::op_t>> logp;
for (int i = 0; i < nn.layer.back().output.size(); ++i) {
logp.push_back(autodiff::logsoftmax(autodiff::add(
autodiff::mul(tensor_tree::get_var(var_tree->children[1]), nn.layer.back().output[i]),
tensor_tree::get_var(var_tree->children[2]))));
}
la::vector<double> v;
v.resize(label_dim, 1.0 / nn.layer.back().output.size());
return autodiff::emul(autodiff::add(logp), graph.var(v));
}
}
}