-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhsm-builder.cc
More file actions
291 lines (253 loc) · 7.52 KB
/
Copy pathhsm-builder.cc
File metadata and controls
291 lines (253 loc) · 7.52 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "dynet/hsm-builder.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include "dynet/param-init.h"
using namespace std;
namespace dynet {
Cluster::Cluster() {}
void Cluster::new_graph(ComputationGraph& cg) {
for (Cluster* child : children) {
child->new_graph(cg);
}
bias.pg = NULL;
weights.pg = NULL;
}
Cluster* Cluster::add_child(unsigned sym) {
auto it = word2ind.find(sym);
unsigned i;
if (it == word2ind.end()) {
Cluster* c = new Cluster();
c->rep_dim = rep_dim;
c->path = path;
c->path.push_back(sym);
i = children.size();
word2ind.insert(make_pair(sym, i));
children.push_back(c);
}
else {
i = it->second;
}
return children[i];
}
void Cluster::add_word(unsigned word) {
word2ind[word] = terminals.size();
terminals.push_back(word);
}
void Cluster::initialize(unsigned rep_dim, ParameterCollection& model) {
this->rep_dim = rep_dim;
initialize(model);
}
void Cluster::initialize(ParameterCollection& model) {
output_size = (children.size() > 0) ? children.size() : terminals.size();
if (output_size == 1) {
}
else if (output_size == 2) {
p_weights = model.add_parameters({1, rep_dim});
p_bias = model.add_parameters({1}, ParameterInitConst(0.f));
}
else {
p_weights = model.add_parameters({output_size, rep_dim});
p_bias = model.add_parameters({output_size}, ParameterInitConst(0.f));
}
for (Cluster* child : children) {
child->rep_dim = this->rep_dim;
child->initialize(model);
}
}
unsigned Cluster::num_children() const {
return children.size();
}
const Cluster* Cluster::get_child(unsigned i) const {
return children[i];
}
const vector<unsigned>& Cluster::get_path() const { return path; }
unsigned Cluster::get_index(unsigned word) const { return word2ind.find(word)->second; }
unsigned Cluster::get_word(unsigned index) const { return terminals[index]; }
Expression Cluster::predict(Expression h, ComputationGraph& cg) const {
if (output_size == 1) {
return input(cg, 1.0f);
} else {
Expression b = get_bias(cg);
Expression w = get_weights(cg);
return affine_transform({b, w, h});
}
}
Expression Cluster::neg_log_softmax(Expression h, unsigned r, ComputationGraph& cg) const {
if (output_size == 1) {
return input(cg, 0.0f);
}
else if (output_size == 2) {
Expression p = logistic(predict(h, cg));
if (r == 1) {
p = 1 - p;
}
return -log(p);
}
else {
Expression dist = predict(h, cg);
return pickneglogsoftmax(dist, r);
}
}
unsigned Cluster::sample(Expression h, ComputationGraph& cg) const {
if (output_size == 1) {
return 0;
}
else if (output_size == 2) {
Expression prob0_expr = logistic(predict(h, cg));
double prob0 = as_scalar(cg.incremental_forward(prob0_expr));
double p = rand01();
if (p < prob0) {
return 0;
}
else {
return 1;
}
}
else {
Expression dist_expr = softmax(predict(h, cg));
vector<float> dist = as_vector(cg.incremental_forward(dist_expr));
unsigned c = 0;
double p = rand01();
for (; c < dist.size(); ++c) {
p -= dist[c];
if (p < 0.0) { break; }
}
if (c == dist.size()) {
--c;
}
return c;
}
}
Expression Cluster::get_weights(ComputationGraph& cg) const {
if (weights.pg != &cg) {
weights = parameter(cg, p_weights);
}
return weights;
}
Expression Cluster::get_bias(ComputationGraph& cg) const {
if (bias.pg != &cg) {
bias = parameter(cg, p_bias);
}
return bias;
}
string Cluster::toString() const {
stringstream ss;
for (unsigned i = 0; i < path.size(); ++i) {
if (i != 0) {
ss << " ";
}
ss << path[i];
}
return ss.str();
}
HierarchicalSoftmaxBuilder::HierarchicalSoftmaxBuilder(unsigned rep_dim,
const std::string& cluster_file,
Dict& word_dict,
ParameterCollection& model) {
local_model = model.add_subcollection("hsm-builder");
root = read_cluster_file(cluster_file, word_dict);
root->initialize(rep_dim, local_model);
}
HierarchicalSoftmaxBuilder::~HierarchicalSoftmaxBuilder() {
}
void HierarchicalSoftmaxBuilder::initialize(ParameterCollection& model) {
root->initialize(model);
}
void HierarchicalSoftmaxBuilder::new_graph(ComputationGraph& cg) {
pcg = &cg;
root->new_graph(cg);
}
Expression HierarchicalSoftmaxBuilder::neg_log_softmax(const Expression& rep, unsigned wordidx) {
if(pcg == NULL)
DYNET_INVALID_ARG("In HierarchicalSoftmaxBuilder, you must call new_graph before calling neg_log_softmax!");
Cluster* path = widx2path[wordidx];
unsigned i = 0;
const Cluster* node = root;
DYNET_ASSERT(root != NULL, "Null root in HierarchicalSoftmaxBuilder");
vector<Expression> log_probs;
Expression lp;
unsigned r;
while (node->num_children() > 0) {
r = node->get_index(path->get_path()[i]);
lp = node->neg_log_softmax(rep, r, *pcg);
log_probs.push_back(lp);
node = node->get_child(r);
DYNET_ASSERT(node != NULL, "Null node in HierarchicalSoftmaxBuilder");
i += 1;
}
r = path->get_index(wordidx);
lp = node->neg_log_softmax(rep, r, *pcg);
log_probs.push_back(lp);
return sum(log_probs);
}
unsigned HierarchicalSoftmaxBuilder::sample(const Expression& rep) {
if(pcg == NULL)
DYNET_INVALID_ARG("In HierarchicalSoftmaxBuilder, you must call new_graph before calling sample!");
const Cluster* node = root;
vector<float> dist;
unsigned c;
while (node->num_children() > 0) {
c = node->sample(rep, *pcg);
node = node->get_child(c);
}
c = node->sample(rep, *pcg);
return node->get_word(c);
}
Expression HierarchicalSoftmaxBuilder::full_log_distribution(const Expression& rep) {
DYNET_RUNTIME_ERR("full_distribution not implemented for HierarchicalSoftmaxBuilder");
return dynet::Expression();
}
inline bool is_ws(char x) { return (x == ' ' || x == '\t'); }
inline bool not_ws(char x) { return (x != ' ' && x != '\t'); }
Cluster* HierarchicalSoftmaxBuilder::read_cluster_file(const std::string& cluster_file, Dict& word_dict) {
cerr << "Reading clusters from " << cluster_file << " ...\n";
ifstream in(cluster_file);
if(!in)
DYNET_INVALID_ARG("HierarchicalSoftmaxBuilder couldn't read clusters from " << cluster_file);
int wc = 0;
string line;
vector<unsigned> path;
Cluster* root = new Cluster();
while(getline(in, line)) {
path.clear();
++wc;
const unsigned len = line.size();
unsigned startp = 0;
unsigned endp = 0;
while (startp < len) {
while (is_ws(line[startp]) && startp < len) { ++startp; }
endp = startp;
while (not_ws(line[endp]) && endp < len) { ++endp; }
string symbol = line.substr(startp, endp - startp);
path.push_back(path_symbols.convert(symbol));
if (line[endp] == ' ') {
startp = endp + 1;
continue;
}
else {
break;
}
}
Cluster* node = root;
for (unsigned symbol : path) {
node = node->add_child(symbol);
}
unsigned startw = endp;
while (is_ws(line[startw]) && startw < len) { ++startw; }
unsigned endw = startw;
while (not_ws(line[endw]) && endw < len) { ++endw; }
if(endp <= startp || startw <= endp || endw <= startw)
DYNET_INVALID_ARG("File formatting error in HierarchicalSoftmaxBuilder");
string word = line.substr(startw, endw - startw);
unsigned widx = word_dict.convert(word);
node->add_word(widx);
if (widx2path.size() <= widx) {
widx2path.resize(widx + 1);
}
widx2path[widx] = node;
}
cerr << "Done reading clusters.\n";
return root;
}
} // namespace dynet