-
Notifications
You must be signed in to change notification settings - Fork 225
/
clstm_prefab.cc
189 lines (169 loc) · 6.95 KB
/
clstm_prefab.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
#include "clstm.h"
#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "extras.h"
#include "utils.h"
namespace ocropus {
map<string, INetworkFactory> network_factories;
string get(const Assoc ¶ms, const string &key, const string &dflt) {
auto it = params.find(key);
if (it == params.end()) return dflt;
return it->second;
}
// A 1D unidirectional LSTM with Softmax/Sigmoid output layer.
Network make_lstm1(const Assoc ¶ms) {
int ninput = params.get("ninput");
int nhidden = params.get("nhidden");
int noutput = params.get("noutput");
string lstm_type = get(params, "lstm_type", "NPLSTM");
string output_type = get(params, "output_type",
noutput == 1 ? "SigmoidLayer" : "SoftmaxLayer");
return layer("Stacked", ninput, noutput, {},
{layer(lstm_type, ninput, nhidden, params, {}),
layer(output_type, nhidden, noutput, params, {})});
}
// A 1D unidirectional reversed LSTM with Softmax/Sigmoid output layer.
Network make_revlstm1(const Assoc ¶ms) {
int ninput = params.get("ninput");
int nhidden = params.get("nhidden");
int noutput = params.get("noutput");
string lstm_type = get(params, "lstm_type", "NPLSTM");
string output_type = get(params, "output_type",
noutput == 1 ? "SigmoidLayer" : "SoftmaxLayer");
return layer("Stacked", ninput, noutput, {},
{layer("Reversed", ninput, nhidden, {},
{layer(lstm_type, ninput, nhidden, params, {})}),
layer(output_type, nhidden, noutput, params, {})});
}
// A 1D bidirectional LSTM with Softmax/Sigmoid output layer.
Network make_bidi(const Assoc ¶ms) {
int ninput = params.get("ninput");
int nhidden = params.get("nhidden");
int noutput = params.get("noutput");
string lstm_type = get(params, "lstm_type", "NPLSTM");
string output_type = get(params, "output_type",
noutput == 1 ? "SigmoidLayer" : "SoftmaxLayer");
return layer(
"Stacked", ninput, noutput, {},
{layer("Parallel", ninput, 2 * nhidden, {},
{
layer(lstm_type, ninput, nhidden, params, {}),
layer("Reversed", ninput, ninput, {},
{layer(lstm_type, ninput, nhidden, params, {})}),
}),
layer(output_type, 2 * nhidden, noutput, params, {})});
}
// A 1D bidirectional LSTM with Softmax/Sigmoid output layer.
Network make_bidi0(const Assoc ¶ms) {
int ninput = params.get("ninput");
int noutput = params.get("noutput");
string lstm_type = get(params, "lstm_type", "NPLSTM");
return layer("Parallel", ninput, 2 * noutput, {},
{
layer(lstm_type, ninput, noutput, params, {}),
layer("Reversed", ninput, ninput, {},
{layer(lstm_type, ninput, noutput, params, {})}),
});
}
// Two stacked 1D bidirectional LSTM with Softmax/Sigmoid output layer.
Network make_bidi2(const Assoc ¶ms) {
int ninput = params.get("ninput");
int nhidden = params.get("nhidden");
int nhidden2 = params.get("nhidden2");
int noutput = params.get("noutput");
string lstm_type = get(params, "lstm_type", "NPLSTM");
string output_type = get(params, "output_type",
noutput == 1 ? "SigmoidLayer" : "SoftmaxLayer");
return layer(
"Stacked", ninput, noutput, {},
{layer("Parallel", ninput, 2 * nhidden, {},
{
layer(lstm_type, ninput, nhidden, params, {}),
layer("Reversed", ninput, ninput, {},
{layer(lstm_type, ninput, nhidden, params, {})}),
}),
layer("Parallel", 2 * nhidden, 2 * nhidden2, {},
{
layer(lstm_type, 2 * nhidden, nhidden2, params, {}),
layer("Reversed", 2 * nhidden, 2 * nhidden, {},
{layer(lstm_type, 2 * nhidden, nhidden2, params, {})}),
}),
layer(output_type, 2 * nhidden2, noutput, params, {})});
}
Network make_perplstm(const Assoc ¶ms) {
int ninput = params.get("ninput");
int nhidden = params.get("nhidden");
int noutput = params.get("noutput");
string output_type = get(params, "output_type", "SigmoidLayer");
Network vertical = make_bidi({{"ninput", ninput},
{"nhidden", nhidden},
{"noutput", noutput},
{"output_type", output_type}});
return layer("Stacked", ninput, noutput, {},
{
// layer("Btswitch", nhidden2, nhidden2, {}, {}),
vertical,
// layer("Btswitch", noutput, noutput, {}, {})
});
}
// Two dimensional LSTM
Network make_twod(const Assoc ¶ms) {
int ninput = params.get("ninput");
int nhidden = params.get("nhidden");
int nhidden2 = params.get("nhidden2", nhidden);
int nhidden3 = params.get("nhidden3", nhidden2);
int noutput = params.get("noutput");
string output_type = get(params, "output_type",
noutput == 1 ? "SigmoidLayer" : "SoftmaxLayer");
Network horizontal = make_bidi({{"ninput", ninput},
{"nhidden", nhidden},
{"noutput", nhidden2},
{"output_type", "SigmoidLayer"}});
Network vertical = make_bidi({{"ninput", nhidden2},
{"nhidden", nhidden3},
{"noutput", noutput},
{"output_type", output_type}});
return layer("Stacked", ninput, noutput, {},
{horizontal, layer("Btswitch", nhidden2, nhidden2, {}, {}),
vertical, layer("Btswitch", noutput, noutput, {}, {})});
}
void init_clstm_prefab() {
network_factories["lstm1"] = make_lstm1;
network_factories["revlstm1"] = make_revlstm1;
network_factories["bidi"] = make_bidi;
network_factories["bidi0"] = make_bidi0;
network_factories["bidi2"] = make_bidi2;
network_factories["twod"] = make_twod;
network_factories["perplstm"] = make_perplstm;
}
static int init_ = (init_clstm_prefab(), 0);
Network make_net(const string &kind, const Assoc &args) {
Network result;
if (network_factories.find(kind) != network_factories.end()) {
result = network_factories[kind](args);
} else {
result = layer(kind, args.get("ninput"), args.get("noutput"), args, {});
}
if (!result) throwf("no such network or layer: %s", kind.c_str());
result->attr.set("kind", kind);
return result;
}
// Make a network, using parameter specifications of the
// form "ninput=28:noutput=10:nhidden=50:output_type=SoftmaxLayer"
Network make_net_init(const string &kind, const string ¶ms) {
using std::cerr;
using std::endl;
Assoc args(params);
if (getienv("verbose_params", 0)) {
for (auto it : args) {
cerr << it.first << ": " << it.second << endl;
}
}
return make_net(kind, args);
}
}