forked from teganmaharaj/zoneout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurrent.py
385 lines (296 loc) · 14 KB
/
recurrent.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import copy
import numpy as np
numpy = np
from theano import tensor
from blocks.bricks.parallel import Fork
from blocks.bricks import (application, Initializable, Tanh,
Logistic, Rectifier, lazy, application)
from blocks.bricks.recurrent import BaseRecurrent, recurrent, LSTM
from blocks.roles import VariableRole, add_role, WEIGHT, INITIAL_STATE
from blocks.utils import shared_floatx_nans, shared_floatx_zeros
from picklable_itertools.extras import equizip
##############################################################
#
# FOR NORM STABILIZER VARIABLE FILTER
#
##############################################################
class MemoryCellRole(VariableRole):
pass
MEMORY_CELL = MemoryCellRole()
##############################################################
##############################################################
#
# David GRU
#
##############################################################
class ZoneoutGRU(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, gate_activation=None, **kwargs):
self.dim = dim
if not activation:
activation = Tanh()
if not gate_activation:
gate_activation = Logistic()
self.activation = activation
self.gate_activation = gate_activation
children = [self.activation, self.gate_activation]
kwargs.setdefault('children', []).extend(children)
super(ZoneoutGRU, self).__init__(**kwargs)
def get_dim(self, name):
if name == 'inputs':
return self.dim * 3
if name in ['states', 'drops_states', 'drops_cells', 'drops_igates']:
return self.dim
if name == 'mask':
return 0
return super(ZoneoutGRU, self).get_dim(name)
def _allocate(self):
self.W_rz = shared_floatx_nans((self.dim, 2 * self.dim),
name='W_state')
self.W_htilde = shared_floatx_nans((self.dim, self.dim),
name='W_state')
# The underscore is required to prevent collision with
# the `initial_state` application method
self.initial_state_ = shared_floatx_zeros((self.dim,),
name="initial_state")
add_role(self.W_rz, WEIGHT)
add_role(self.W_htilde, WEIGHT)
add_role(self.initial_state_, INITIAL_STATE)
#self.parameters = [self.W_state, self.initial_state_, self.initial_cells]
self.parameters = [self.W_rz, self.W_htilde, self.initial_state_]
def _initialize(self):
for weights in self.parameters[:2]:
self.weights_init.initialize(weights, self.rng)
# NTS: scan may complain about unused input?
@recurrent(sequences=['inputs', 'drops_states', 'drops_cells', 'drops_igates', 'mask'],
states=['states'],
contexts=[], outputs=['states'])
# naming (r, z, htilde) comes from Wojciech's "Empirical Evaluation..."
def apply(self, inputs, drops_states, drops_cells, drops_igates, states, mask=None):
def slice_last(x, no):
return x[:, no * self.dim: (no + 1) * self.dim]
rz = self.gate_activation.apply(tensor.dot(states, self.W_rz) + inputs[:, self.dim:])
r = slice_last(rz, 0)
z = slice_last(rz, 1)
htilde = self.activation.apply(tensor.dot(r * states, self.W_htilde) + inputs[:, :self.dim])
next_states = z * states + (1 - z) * htilde * drops_igates
next_states = next_states * drops_states + (1 - drops_states) * states
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.initial_state_[None, :], batch_size, 0)]
#END David GRU#####################################################
##############################################################
#
# LSTM
#
##############################################################
class ZoneoutLSTM(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, gate_activation=None,
model_type=6, ogates_zoneout=False, **kwargs):
self.dim = dim
self.model_type = model_type
self.ogates_zoneout = ogates_zoneout
if not activation:
activation = Tanh()
if not gate_activation:
gate_activation = Logistic()
self.activation = activation
self.gate_activation = gate_activation
children = [self.activation, self.gate_activation]
kwargs.setdefault('children', []).extend(children)
super(ZoneoutLSTM, self).__init__(**kwargs)
def get_dim(self, name):
if name == 'inputs':
return self.dim * 4
if name in ['states', 'cells', 'drops_states', 'drops_cells', 'drops_igates']:
return self.dim
if name == 'mask':
return 0
return super(ZoneoutLSTM, self).get_dim(name)
def _allocate(self):
self.W_state = shared_floatx_nans((self.dim, 4 * self.dim),
name='W_state')
# The underscore is required to prevent collision with
# the `initial_state` application method
self.initial_state_ = shared_floatx_zeros((self.dim,),
name="initial_state")
self.initial_cells = shared_floatx_zeros((self.dim,),
name="initial_cells")
add_role(self.W_state, WEIGHT)
add_role(self.initial_state_, INITIAL_STATE)
add_role(self.initial_cells, INITIAL_STATE)
self.parameters = [
self.W_state , self.initial_state_, self.initial_cells]
def _initialize(self):
for weights in self.parameters[:1]:
self.weights_init.initialize(weights, self.rng)
@recurrent(sequences=['inputs', 'drops_states', 'drops_cells', 'drops_igates', 'mask'],
states=['states', 'cells'],
contexts=[], outputs=['states', 'cells'])
def apply(self, inputs, drops_states, drops_cells, drops_igates, states, cells, mask=None):
def slice_last(x, no):
return x[:, no * self.dim: (no + 1) * self.dim]
activation = tensor.dot(states, self.W_state) + inputs
in_gate = self.gate_activation.apply(slice_last(activation, 0)) * drops_igates #elephant
forget_gate_input = slice_last(activation, 1)
forget_gate = self.gate_activation.apply(
forget_gate_input + tensor.ones_like(forget_gate_input))
next_cells = (
forget_gate * cells +
in_gate * self.activation.apply(slice_last(activation, 2)))
out_gate = self.gate_activation.apply(slice_last(activation, 3))
next_states = out_gate * self.activation.apply(next_cells)
# In training time drops is either 0 or 1
# In test time drops is 0.5 (if drop_prob=0.5)
if self.model_type == 2:
next_states = next_states * drops_states
next_cells = next_cells * drops_cells
elif self.model_type == 3:
next_states = (next_states + states) / 2
elif self.model_type == 4:
next_states = (next_states + states) * drops_states
elif self.model_type == 5:
next_states = next_states * drops_states + states
# we always use this model type, and pass masks which effectively turn off zoneout
# by setting drops to 1 (i.e. pass all ones, or pass actual probabilites)
elif self.model_type == 6:
next_states = next_states * drops_states + (1 - drops_states) * states
next_cells = next_cells * drops_cells + (1 - drops_cells) * cells
if self.ogates_zoneout:
next_states = drops_igates * next_states + (1 - drops_igates) * forget_gate * states
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
next_cells = (mask[:, None] * next_cells +
(1 - mask[:, None]) * cells)
return next_states, next_cells
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.initial_state_[None, :], batch_size, 0),
tensor.repeat(self.initial_cells[None, :], batch_size, 0)]
#END LSTM#####################################################
##############################################################
#
# SRNN
#
##############################################################
class ZoneoutSimpleRecurrent(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation, **kwargs):
self.dim = dim
children = [activation] + kwargs.get('children', [])
super(ZoneoutSimpleRecurrent, self).__init__(children=children, **kwargs)
@property
def W(self):
return self.parameters[0]
def get_dim(self, name):
if name == 'inputs':
return self.dim
if name in ['states', 'drops_states', 'drops_cells', 'drops_igates']:
return self.dim
if name == 'mask':
return 0
return super(ZoneoutSimpleRecurrent, self).get_dim(name)
def _allocate(self):
self.parameters.append(shared_floatx_nans((self.dim, self.dim),
name="W"))
add_role(self.parameters[0], WEIGHT)
self.parameters.append(shared_floatx_zeros((self.dim,),
name="initial_state"))
add_role(self.parameters[1], INITIAL_STATE)
def _initialize(self):
self.weights_init.initialize(self.W, self.rng)
@recurrent(sequences=['inputs', 'drops_states', 'drops_cells', 'drops_igates', 'mask'], states=['states'],
outputs=['states'], contexts=[])
def apply(self, inputs, drops_states, drops_cells, drops_igates, states, mask=None):
next_states = inputs + tensor.dot(states, self.W)
next_states = self.children[0].apply(next_states)
# apply zoneout
next_states = drops_states * next_states + (1 - drops_states) * states
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return tensor.repeat(self.parameters[1][None, :], batch_size, 0)
#END SRNN#####################################################
##############################################################
#
# GRU
#
##############################################################
class TeganZoneoutGRU(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, gate_activation=None,
**kwargs):
self.dim = dim
if not activation:
activation = Tanh()
if not gate_activation:
gate_activation = Logistic()
self.activation = activation
self.gate_activation = gate_activation
children = [activation, gate_activation]
kwargs.setdefault('children', []).extend(children)
super(ZoneoutGRU, self).__init__(**kwargs)
@property
def state_to_state(self):
return self.parameters[0]
@property
def state_to_gates(self):
return self.parameters[1]
def get_dim(self, name):
if name == 'mask':
return 0
if name in ['inputs', 'states', 'drops_states']:
return self.dim
if name in ['gate_inputs', 'drops_igates']:
return 2 * self.dim
return super(ZoneoutGRU, self).get_dim(name)
def _allocate(self):
self.parameters.append(shared_floatx_nans((self.dim, self.dim),
name='state_to_state'))
self.parameters.append(shared_floatx_nans((self.dim, 2 * self.dim),
name='state_to_gates'))
self.parameters.append(shared_floatx_zeros((self.dim,),
name="initial_state"))
for i in range(2):
if self.parameters[i]:
add_role(self.parameters[i], WEIGHT)
add_role(self.parameters[2], INITIAL_STATE)
def _initialize(self):
self.weights_init.initialize(self.state_to_state, self.rng)
state_to_update = self.weights_init.generate(
self.rng, (self.dim, self.dim))
state_to_reset = self.weights_init.generate(
self.rng, (self.dim, self.dim))
self.state_to_gates.set_value(
np.hstack([state_to_update, state_to_reset]))
@recurrent(sequences=['mask', 'inputs', 'gate_inputs', 'drops_states', 'drops_igates' ],
states=['states'], outputs=['states'], contexts=[])
def apply(self, inputs, gate_inputs, drops_states, drops_igates, states, mask=None):
gate_values = self.gate_activation.apply(
states.dot(self.state_to_gates) + gate_inputs) * drops_igates #elephant
update_values = gate_values[:, :self.dim]
reset_values = gate_values[:, self.dim:]
states_reset = states * reset_values
next_states = self.activation.apply(
states_reset.dot(self.state_to_state) + inputs)
#no zoneout
#next_states = (next_states * update_values + states * (1 - update_values))
#zoneout
next_states = next_states*drops_states + (1-drops_states)*states
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.parameters[2][None, :], batch_size, 0)]
#END GRU######################################################