-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecoders.py
335 lines (287 loc) · 12.9 KB
/
decoders.py
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
from __future__ import print_function, division
import numpy as np
import dynet as dy
import sys
class Decoder(object):
"""Base Encoder class"""
def __init__(self, pc):
self.pc = pc.add_subcollection('dec')
def init(self, H, y, usr, test=True, update=True):
pass
def next(self, w, c, test=True, state=None):
raise NotImplemented()
def s(self, h, c, e, test=True):
raise NotImplemented()
class LSTMLMDecoder(Decoder):
"""docstring for EmptyEncoder"""
def __init__(self, nl, di, dh, vt, pc, pre_embs=None, dr=0.0, wdr=0.0):
super(LSTMLMDecoder, self).__init__(pc)
# Store hyperparameters
self.nl, self.di, self.dh = nl, di, dh
self.dr, self.wdr = dr, wdr
self.vt = vt
# LSTM Encoder
self.lstm = dy.VanillaLSTMBuilder(self.nl, self.di, self.dh, self.pc)
# Output layer
self.Wo_p = self.pc.add_parameters((self.di, self.dh + self.di), name='Wo')
self.bo_p = self.pc.add_parameters((self.di,), name='bo')
# Embedding matrix
self.E_p = self.pc.add_parameters((self.vt, self.di), name='E')
if pre_embs is not None:
self.E.set_value(pre_embs)
def init(self, H, y, usr, test=True, update=True):
bs = len(y[0])
if not test:
self.lstm.set_dropout(self.dr)
else:
self.lstm.disable_dropout()
# Add encoder to computation graph
self.ds = self.lstm.initial_state(update=update)
if not test:
self.lstm.set_dropout_masks(bs)
self.Wo = self.Wo_p.expr(update)
self.bo = self.bo_p.expr(update)
self.E = self.E_p.expr(update)
def next(self, w, c, test=True, state=None):
e = dy.pick_batch(self.E, w)
if not test:
e = dy.dropout_dim(e, 0, self.wdr)
# Run LSTM
if state is None:
self.ds = self.ds.add_input(e)
next_state = self.ds
else:
next_state = state.add_input(e)
h = next_state.output()
return h, e, next_state
def s(self, h, c, e, test=True):
output = dy.affine_transform([self.bo, self.Wo, dy.concatenate([h, e])])
if not test:
output = dy.dropout(output, self.dr)
# Score
s = self.E * output
return s
class LSTMDecoder(Decoder):
"""docstring for LSTMDecoder"""
def __init__(self, nl, di, de, dh, vt, pc, pre_embs=None, dr=0.0, wdr=0.0):
super(LSTMDecoder, self).__init__(pc)
# Store hyperparameters
self.nl, self.di, self.de, self.dh = nl, di, de, dh
self.dr, self.wdr = dr, wdr
self.vt = vt
# LSTM Encoder
self.lstm = dy.VanillaLSTMBuilder(self.nl, self.di + self.de, self.dh, self.pc)
# Linear layer from last encoding to initial state
self.Wp_p = self.pc.add_parameters((self.di, self.de), name='Wp')
self.bp_p = self.pc.add_parameters((self.di,), name='bp')
# Output layer
self.Wo_p = self.pc.add_parameters((self.di, self.dh + self.de + self.di), name='Wo')
self.bo_p = self.pc.add_parameters((self.di,), name='bo')
# Embedding matrix
if pre_embs is not None:
self.E_p = self.pc.parameters_from_numpy(pre_embs, name='E')
else:
self.E_p = self.pc.add_parameters((self.vt, self.di), name='E')
self.b_p = self.pc.add_parameters((self.vt,), init=dy.ConstInitializer(0), name='b')
def init(self, H, y, usr, test=True, update=True):
bs = len(y[0])
if not test:
self.lstm.set_dropout(self.dr)
else:
self.lstm.disable_dropout()
# Initialize first state of the decoder with the last state of the encoder
self.Wp = self.Wp_p.expr(update)
self.bp = self.bp_p.expr(update)
last_enc = dy.pick(H, index=H.dim()[0][-1] - 1, dim=1)
init_state = dy.affine_transform([self.bp, self.Wp, last_enc])
init_state = [dy.zeros(self.dh, batch_size=bs), init_state]
self.ds = self.lstm.initial_state(init_state, update=update)
# Initialize dropout masks
if not test:
self.lstm.set_dropout_masks(bs)
self.Wo = self.Wo_p.expr(update)
self.bo = self.bo_p.expr(update)
self.E = self.E_p.expr(update)
self.b = self.b_p.expr(update)
def next(self, w, c, test=True, state=None):
if isinstance(w, dy.Expression):
e = w
else:
e = dy.pick_batch(self.E, w)
if not test:
e = dy.dropout_dim(e, 0, self.wdr)
x = dy.concatenate([e, c])
# Run LSTM
if state is None:
self.ds = self.ds.add_input(x)
next_state = self.ds
else:
next_state = state.add_input(x)
h = next_state.output()
return h, e, next_state
def s(self, h, c, e, test=True):
output = dy.affine_transform([self.bo, self.Wo, dy.concatenate([h, c, e])])
if not test:
output = dy.dropout(output, self.dr)
# Score
s = dy.affine_transform([self.b, self.E, output])
return s
def load_pretrained(self, filename):
self.lstm.param_collection().populate(filename, self.lstm.param_collection().name())
self.Wp_p.populate(filename, self.pc.name() + '/Wp')
self.bp_p.populate(filename, self.pc.name() + '/bp')
self.Wo_p.populate(filename, self.pc.name() + '/Wo')
self.bo_p.populate(filename, self.pc.name() + '/bo')
self.E_p.populate(filename, self.pc.name() + '/E')
self.b_p.populate(filename, self.pc.name() + '/b')
class OutLSTMDecoder(Decoder):
"""docstring for LSTMDecoder"""
def __init__(self, nl, di, de, dh, vt, dt, pc, pre_embs=None, dr=0.0, wdr=0.0):
super(OutLSTMDecoder, self).__init__(pc)
# Store hyperparameters
self.nl, self.di, self.de, self.dh = nl, di, de, dh
self.dr, self.wdr = dr, wdr
self.dt = dt
self.vt = vt
# LSTM Encoder
self.lstm = dy.VanillaLSTMBuilder(self.nl, self.di + self.de, self.dh, self.pc)
# Linear layer from last encoding to initial state
self.Wp_p = self.pc.add_parameters((self.di, self.de), name='Wp')
self.bp_p = self.pc.add_parameters((self.di,), name='bp')
# Output layer
self.Wo_p = self.pc.add_parameters((self.di, self.dh + self.de + self.di), name='Wo')
self.To_p = self.pc.add_parameters((self.di, self.dh + self.de + self.di, self.dt), name='To', init=dy.ConstInitializer(0))
self.bo_p = self.pc.add_parameters((self.di,), name='bo')
# Embedding matrix
if pre_embs is not None:
self.E_p = self.pc.parameters_from_numpy(pre_embs, name='E')
else:
self.E_p = self.pc.add_parameters((self.vt, self.di), name='E')
self.b_p = self.pc.add_parameters((self.vt,), init=dy.ConstInitializer(0), name='b')
def init(self, H, y, usr, test=True, update=True):
bs = len(y[0])
if not test:
self.lstm.set_dropout(self.dr)
else:
self.lstm.disable_dropout()
# Initialize first state of the decoder with the last state of the encoder
self.Wp = self.Wp_p.expr(update)
self.bp = self.bp_p.expr(update)
last_enc = dy.pick(H, index=H.dim()[0][-1] - 1, dim=1)
init_state = dy.affine_transform([self.bp, self.Wp, last_enc])
init_state = [dy.zeros(self.dh, batch_size=bs), init_state]
self.ds = self.lstm.initial_state(init_state, update=update)
# Initialize dropout masks
if not test:
self.lstm.set_dropout_masks(bs)
self.Wo = dy.contract3d_1d_bias(self.To_p.expr(update), usr, self.Wo_p.expr(update))
self.bo = self.bo_p.expr(update)
self.E = self.E_p.expr(update)
self.b = self.b_p.expr(False)
def next(self, w, c, test=True, state=None):
e = dy.pick_batch(self.E, w)
if not test:
e = dy.dropout_dim(e, 0, self.wdr)
x = dy.concatenate([e, c])
# Run LSTM
if state is None:
self.ds = self.ds.add_input(x)
next_state = self.ds
else:
next_state = state.add_input(x)
h = next_state.output()
return h, e, next_state
def s(self, h, c, e, test=True):
output = dy.affine_transform([self.bo, self.Wo, dy.concatenate([h, c, e])])
if not test:
output = dy.dropout(output, self.dr)
# Score
s = dy.affine_transform([self.b, self.E, output])
return s
def load_pretrained(self, filename):
self.lstm.param_collection().populate(filename, self.lstm.param_collection().name())
self.Wp_p.populate(filename, self.pc.name() + '/Wp')
self.bp_p.populate(filename, self.pc.name() + '/bp')
self.Wo_p.populate(filename, self.pc.name() + '/Wo')
self.bo_p.populate(filename, self.pc.name() + '/bo')
self.E_p.populate(filename, self.pc.name() + '/E')
self.b_p.populate(filename, self.pc.name() + '/b')
class InitLSTMDecoder(LSTMDecoder):
"""The InitLSTM decoder uses a special initialization for all users"""
def __init__(self, nl, di, de, dh, vt, du, pc, pre_embs=None, dr=0.0, wdr=0.0):
super(InitLSTMDecoder, self).__init__(nl, di, de, dh, vt, pc, pre_embs, dr, wdr)
# Store hyperparameters
self.du = du
# Transform user vector
self.Wu_p = self.pc.add_parameters((self.di, self.du), name='Wu')
def init(self, H, y, usr, test=True, update=True):
bs = len(y[0])
if not test:
self.lstm.set_dropout(self.dr)
else:
self.lstm.disable_dropout()
# Initialize first state of the decoder with the last state of the encoder
self.Wp = self.Wp_p.expr(update)
self.bp = self.bp_p.expr(update)
self.Wu = self.Wu_p.expr(update)
last_enc = dy.pick(H, index=H.dim()[0][-1] - 1, dim=1)
init_state = dy.affine_transform([self.bp, self.Wp, last_enc, self.Wu, dy.nobackprop(usr)])
init_state = [init_state, dy.zeroes((self.dh,), batch_size=bs)]
self.ds = self.lstm.initial_state(init_state, update=update)
# Initialize dropout masks
if not test:
self.lstm.set_dropout_masks(bs)
self.Wo = self.Wo_p.expr(update)
self.bo = self.bo_p.expr(update)
self.E = self.E_p.expr(update)
self.b = self.b_p.expr(update)
class VocLSTMDecoder(LSTMDecoder):
"""The VocLSTM decoder uses a special bias for all users"""
def __init__(self, nl, di, de, dh, vt, du, pc, pre_embs=None, dr=0.0, wdr=0.0):
super(VocLSTMDecoder, self).__init__(nl, di, de, dh, vt, pc, pre_embs, dr, wdr)
# Store hyperparameters
self.du = du
# User bias
self.ub_p = self.pc.add_parameters((self.vt, self.du), init=dy.ConstInitializer(0), name='ub')
def init(self, H, y, usr, test=True, update=True):
super(VocLSTMDecoder, self).init(H, y, usr, test, update)
# Init vocab bias
self.ub = dy.affine_transform([self.b, self.ub_p.expr(update), usr])
def s(self, h, c, e, test=True):
output = dy.affine_transform([self.bo, self.Wo, dy.concatenate([h, c, e])])
if not test:
output = dy.dropout(output, self.dr)
# Score
s = dy.affine_transform([self.ub, self.E, output])
return s
class FullVocLSTMDecoder(LSTMDecoder):
"""The FullVocLSTM decoder uses a special bias for all users, not factorized (omg)"""
def init(self, H, y, usr, test=True, update=True):
super(FullVocLSTMDecoder, self).init(H, y, usr, test, update)
# Init vocab bias
self.ub = usr
self.scores = []
def s(self, h, c, e, test=True):
output = dy.affine_transform([self.bo, self.Wo, dy.concatenate([h, c, e])])
if not test:
output = dy.dropout(output, self.dr)
# Score
self.scores.append(dy.affine_transform([self.b, self.E, output]))
s = self.scores[-1] + self.ub
return s
def get_decoder(decoder, nl, di, de, dh, vt, du, pc, pre_embs=None, dr=0.0, wdr=0.0):
if decoder == 'lm':
return LSTMLMDecoder(nl, di, dh, vt, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)
elif decoder == 'lstm':
return LSTMDecoder(nl, di, de, dh, vt, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)
elif decoder == 'usr_out_lstm':
return OutLSTMDecoder(nl, di, de, dh, vt, du, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)
elif decoder == 'usr_init_lstm':
return InitLSTMDecoder(nl, di, de, dh, vt, du, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)
elif decoder == 'usr_voc_lstm':
return VocLSTMDecoder(nl, di, de, dh, vt, du, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)
elif decoder == 'usr_full_voc_lstm':
return FullVocLSTMDecoder(nl, di, de, dh, vt, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)
else:
print('Unknown decoder type "%s", using lstm decoder' % decoder)
return LSTMDecoder(nl, di, de, dh, vt, pc, dr=dr, wdr=wdr, pre_embs=pre_embs)