/
hidden.hpp
357 lines (344 loc) · 11.6 KB
/
hidden.hpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#ifndef LVRNN_HIDDEN_HPP
#define LVRNN_HIDDEN_HPP
#include "util.hpp"
template <class Builder>
class LVRNNHidden{
private:
LookupParameters* p_W; // word embeddings VxK1
LookupParameters* p_T;
Parameters* p_R; // output weight
Parameters* p_C; // forward context vector: VxK2
Parameters* p_bias; // bias Vx1
Parameters* p_context; // default context vector
Parameters* p_L; // for latent variable prediction
Parameters* p_lbias; // for latent variable prediction
Builder builder;
unsigned nlatvar;
vector<float> final_h;
vector<vector<float>> final_hlist;
public:
LVRNNHidden(Model& model, unsigned nlayers, unsigned inputdim,
unsigned hiddendim, unsigned vocabsize,
unsigned nlatent):builder(nlayers, inputdim+hiddendim,
hiddendim, &model){
// number of latent variables
nlatvar = nlatent;
// word representation
p_W = model.add_lookup_parameters(vocabsize, {inputdim});
// output weight
p_R = model.add_parameters({vocabsize, hiddendim});
// prediction bias
p_bias = model.add_parameters({vocabsize});
// default context vector
p_context = model.add_parameters({hiddendim});
// latent variable distribution
p_L = model.add_parameters({nlatent, hiddendim});
p_lbias = model.add_parameters({nlatent});
// transform matrix
p_T = model.add_lookup_parameters(nlatent,
{inputdim+hiddendim,
inputdim+hiddendim});
}
/************************************************
* Build CG of a given doc with a latent sequence
*
* doc:
* cg: computation graph
* latseq: latent sequence from decoding
* obsseq: latent sequence from observation
* flag: what we expected to get from this function
************************************************/
Expression BuildGraph(const Doc& doc, ComputationGraph& cg,
LatentSeq latseq, LatentSeq obsseq,
const string& flag){
builder.new_graph(cg);
// define expression
Expression i_R = parameter(cg, p_R);
Expression i_bias = parameter(cg, p_bias);
Expression i_context = parameter(cg, p_context);
Expression i_L = parameter(cg, p_L);
Expression i_lbias = parameter(cg, p_lbias);
vector<Expression> negloglik, neglogprob;
// -----------------------------------------
// check hidden variable list
assert(latseq.size() <= doc.size());
// -----------------------------------------
// iterate over latent sequences
// get LV-related transformation matrix
Expression i_h_t;
for (unsigned k = 0; k < doc.size(); k++){
// using latent size as constraint
builder.start_new_sequence();
// for each sentence in this doc
Expression cvec;
auto& sent = doc[k];
// start a new sequence for each sentence
if (k == 0){
cvec = i_context;
} else {
cvec = input(cg, {(unsigned)final_h.size()}, final_h);
}
// latent variable distribution
int latval = 0;
if (obsseq[k] >=0){
latval = obsseq[k];
Expression k_neglogprob = pickneglogsoftmax((i_L * cvec) + i_lbias, latval);
neglogprob.push_back(k_neglogprob);
} else {
latval = latseq[k];
}
// build RNN for the current sentence
Expression i_x_t, i_h_t, i_y_t, i_negloglik;
Expression i_Tk = lookup(cg, p_T, latval);
unsigned slen = sent.size() - 1;
for (unsigned t = 0; t < slen; t++){
// get word representation
i_x_t = lookup(cg, p_W, sent[t]);
vector<Expression> vecexp;
vecexp.push_back(i_x_t);
vecexp.push_back(cvec);
i_x_t = concatenate(vecexp);
// compute hidden state
i_h_t = builder.add_input(i_Tk * i_x_t);
// compute prediction
i_y_t = (i_R * i_h_t) + i_bias;
// get prediction error
i_negloglik = pickneglogsoftmax(i_y_t, sent[t+1]);
// add back
negloglik.push_back(i_negloglik);
}
final_h.clear();
final_h = as_vector(i_h_t.value());
}
// get result
Expression res;
if ((flag != "INFER") && (flag != "OBJ")){
cerr << "Unrecognized flag: " << flag << endl;
abort();
} else if ((neglogprob.size() > 0) && (flag == "OBJ")){
res = sum(negloglik) + sum(neglogprob);
} else {
res = sum(negloglik);
}
return res;
}
/************************************************
* Build CG of a given doc with a latent sequence
*
* doc:
* cg: computation graph
* latseq: latent sequence from decoding
* obsseq: latent sequence from observation
* flag: what we expected to get from this function
* "PROB": compute the probability of the last sentence
* given the latent value
* "ERROR": compute the prediction error of entire doc
* "INFER": compute prediction error on words with
* inferred latent variables
************************************************/
Expression BuildRelaGraph(const Doc& doc, ComputationGraph& cg,
LatentSeq latseq, LatentSeq obsseq){
builder.new_graph(cg);
// define expression
Expression i_R = parameter(cg, p_R);
Expression i_bias = parameter(cg, p_bias);
Expression i_context = parameter(cg, p_context);
Expression i_L = parameter(cg, p_L);
Expression i_lbias = parameter(cg, p_lbias);
vector<Expression> negloglik, neglogprob;
// -----------------------------------------
// check hidden variable list
assert(latseq.size() <= doc.size());
// -----------------------------------------
// iterate over latent sequences
// get LV-related transformation matrix
Expression i_h_t;
vector<Expression> obj;
for (unsigned k = 0; k < doc.size(); k++){
auto& sent = doc[k];
// start a new sequence for each sentence
Expression cvec;
if (k == 0){
cvec = i_context;
} else {
cvec = input(cg, {(unsigned)final_h.size()}, final_h);
}
// two parts of the objective function
Expression sent_objpart1;
vector<Expression> sent_objpart2;
for (int latval = 0; latval < nlatvar; latval ++){
builder.start_new_sequence();
// latent variable distribution
vector<Expression> l_negloglik;
Expression l_neglogprob = pickneglogsoftmax((i_L * cvec) + i_lbias, latval);
// build RNN for the current sentence
Expression i_x_t, i_h_t, i_y_t, i_negloglik;
Expression i_Tk = lookup(cg, p_T, latval);
// for each word
unsigned slen = sent.size() - 1;
for (unsigned t = 0; t < slen; t++){
// get word representation
i_x_t = const_lookup(cg, p_W, sent[t]);
vector<Expression> vecexp;
vecexp.push_back(i_x_t);
vecexp.push_back(cvec);
i_x_t = concatenate(vecexp);
// compute hidden state
i_h_t = builder.add_input(i_Tk * i_x_t);
// compute prediction
i_y_t = (i_R * i_h_t) + i_bias;
// get prediction error
i_negloglik = pickneglogsoftmax(i_y_t, sent[t+1]);
// add back
l_negloglik.push_back(i_negloglik);
}
// update context vector
if (latval == (nlatvar - 1)){
final_h.clear();
final_h = as_vector(i_h_t.value());
}
// - log P(Y, Z) given Y and a specific Z value
Expression pxz = sum(l_negloglik) + l_neglogprob;
sent_objpart2.push_back(pxz * (-1.0));
if (obsseq[k] == latval){
sent_objpart1 = pxz * (-1.0);
}
}
// if the latent variable is observed
if (obsseq[k] >= 0){
Expression sent_obj = logsumexp(sent_objpart2) - sent_objpart1;
obj.push_back(sent_obj);
// cout << as_scalar(sent_obj.value()) << endl;
}
}
// get the objectve for entire doc
if (obj.size() > 0){
// if at least one observed latent value
return sum(obj);
} else {
// otherwise
Expression zero = input(cg, 0.0);
return zero;
}
}
/*********************************************
* Build computation graph for one sentence
*
* sent: Sent instance
*********************************************/
Expression BuildSentGraph(const Sent& sent, const unsigned sidx,
ComputationGraph& cg,
const int latval){
builder.new_graph(cg);
builder.start_new_sequence();
// define expression
Expression i_R = input(cg, p_R->dim, as_vector(p_R->values));
Expression i_bias = input(cg, p_bias->dim, as_vector(p_bias->values));
Expression i_context = input(cg, p_context->dim, as_vector(p_context->values));
Expression i_L = input(cg, p_L->dim, as_vector(p_L->values));
Expression i_lbias = input(cg, p_lbias->dim, as_vector(p_lbias->values));
// Initialize cvec
Expression cvec;
if (sidx == 0)
cvec = i_context;
else
cvec = input(cg, {(unsigned)final_h.size()}, final_h);
// compute the prob for the given latval
Expression i_Tk = const_lookup(cg, p_T, latval);
Expression lv_neglogprob = pickneglogsoftmax(((i_L * cvec) + i_lbias), latval);
vector<Expression> negloglik;
Expression i_negloglik, i_x_t, i_h_t, i_y_t;
unsigned slen = sent.size() - 1;
for (unsigned t = 0; t < slen; t++){
// get word representation
i_x_t = const_lookup(cg, p_W, sent[t]);
vector<Expression> vecexp;
vecexp.push_back(i_x_t);
vecexp.push_back(cvec);
i_x_t = concatenate(vecexp);
// compute hidden state
i_h_t = builder.add_input(i_Tk * i_x_t);
// compute prediction
i_y_t = (i_R * i_h_t) + i_bias;
// get prediction error
i_negloglik = pickneglogsoftmax(i_y_t, sent[t+1]);
// push back
negloglik.push_back(i_negloglik);
}
// update final_h, if latval = nlatvar - 1
vector<float> temp_h = as_vector(i_h_t.value());
final_hlist.push_back(temp_h);
Expression res = (sum(negloglik) + lv_neglogprob) * (-1.0);
return res;
}
/*********************************************
* Sample particles for a given document
*
* doc:
*********************************************/
LatentSeq DecodeGraph(const Doc doc){
// ----------------------------------------
// init
int nsent = doc.size();
LatentSeq latseq;
// ----------------------------------------
// for each sentence in doc, each latval, compute
// the posterior prob p(R|cvec, sent)
vector<float> U;
for (unsigned sidx = 0; sidx < nsent; sidx ++){
final_hlist.clear();
for (int val = 0; val < nlatvar; val ++){
ComputationGraph cg;
BuildSentGraph(doc[sidx], sidx, cg, val);
float prob = as_scalar(cg.forward());
U.push_back(prob);
cg.clear();
}
// normalize and get the argmax
log_normalize(U);
// greedy decoding
int max_idx = argmax(U);
// get the corresponding context vector
final_h = final_hlist[max_idx];
//
U.clear();
// cerr << "max_latval = " << max_idx << endl;
latseq.push_back(max_idx);
}
// cerr << "====" << endl;
return latseq;
}
/**********************************************
* Build Obj graph for learning
*
**********************************************/
Expression BuildObjGraph(const Doc& doc,
ComputationGraph& cg,
LatentSeq latseq,
LatentSeq obsseq){
Expression obj = BuildGraph(doc, cg, latseq,
obsseq, "OBJ");
return obj;
}
/**********************************************
* Build Rela Obj graph for learning
*
**********************************************/
Expression BuildRelaObjGraph(const Doc& doc,
ComputationGraph& cg,
LatentSeq latseq,
LatentSeq obsseq){
Expression obj = BuildRelaGraph(doc, cg, latseq, obsseq);
return obj;
}
/**********************************************
* Build graph for inference
*
**********************************************/
Expression BuildInferGraph(const Doc& doc, ComputationGraph& cg,
LatentSeq latseq, LatentSeq obsseq){
Expression obj = BuildGraph(doc, cg, latseq, obsseq, "INFER");
return obj;
}
};
#endif