forked from mpezeshki/RNN_Experiments
/
bricks.py
410 lines (333 loc) · 14.4 KB
/
bricks.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from theano import tensor
from theano.sandbox.rng_mrg import MRG_RandomStreams
from blocks.bricks import Initializable, Tanh, Activation
from blocks.bricks.base import application, lazy
from blocks.bricks.recurrent import BaseRecurrent, recurrent
# from blocks.initialization import IsotropicGaussian, Constant
from blocks.roles import add_role, WEIGHT, BIAS, INITIAL_STATE
from blocks.utils import (
check_theano_variable, shared_floatx_nans, shared_floatx_zeros)
class LookupTable(Initializable):
"""Encapsulates representations of a range of integers.
Parameters
----------
length : int
The size of the lookup table, or in other words, one plus the
maximum index for which a representation is contained.
dim : int
The dimensionality of representations.
Notes
-----
See :class:`.Initializable` for initialization parameters.
"""
has_bias = True
@lazy(allocation=['length', 'dim'])
def __init__(self, length, dim, **kwargs):
super(LookupTable, self).__init__(**kwargs)
self.length = length
self.dim = dim
@property
def W(self):
return self.params[0]
@property
def b(self):
return self.params[1]
def _allocate(self):
W = shared_floatx_nans((self.length, self.dim), name='W_lookup')
self.params.append(W)
add_role(W, WEIGHT)
b = shared_floatx_nans((self.dim,), name='b_lookup')
self.params.append(b)
add_role(b, BIAS)
def _initialize(self):
self.weights_init.initialize(self.W, self.rng)
self.biases_init.initialize(self.b, self.rng)
@application
def apply(self, indices):
"""Perform lookup.
Parameters
----------
indices : :class:`~tensor.TensorVariable`
The indices of interest. The dtype must be integer.
Returns
-------
output : :class:`~tensor.TensorVariable`
Representations for the indices of the query. Has :math:`k+1`
dimensions, where :math:`k` is the number of dimensions of the
`indices` parameter. The last dimension stands for the
representation element.
"""
check_theano_variable(indices, None, "int")
output_shape = [indices.shape[i]
for i in range(indices.ndim)] + [self.dim]
return self.W[indices.flatten()].reshape(output_shape) + self.b
# Very similar to the SimpleRecurrent implementation. But the computation is
# made one every `period` time steps. This brick carries the time as a state
class ClockworkBase(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, period, activation, **kwargs):
super(ClockworkBase, self).__init__(**kwargs)
self.dim = dim
self.period = period
self.children = [activation]
@property
def W(self):
return self.params[0]
def get_dim(self, name):
if name == 'mask':
return 0
if name in (ClockworkBase.apply.sequences +
ClockworkBase.apply.states):
return self.dim
return super(ClockworkBase, self).get_dim(name)
def _allocate(self):
self.params.append(shared_floatx_nans((self.dim, self.dim), name="W"))
add_role(self.params[0], WEIGHT)
self.params.append(shared_floatx_zeros((self.dim,),
name="initial_state"))
add_role(self.params[1], INITIAL_STATE)
self.params.append(shared_floatx_zeros((1,), name="initial_time"))
add_role(self.params[2], INITIAL_STATE)
def _initialize(self):
self.weights_init.initialize(self.W, self.rng)
@recurrent(sequences=['inputs', 'mask'], states=['states', 'time'],
outputs=['states', 'time'], contexts=[])
def apply(self, inputs=None, states=None, time=None, mask=None):
"""Apply the simple transition.
Parameters
----------
inputs : :class:`~tensor.TensorVariable`
The 2D inputs, in the shape (batch, features).
states : :class:`~tensor.TensorVariable`
The 2D states, in the shape (batch, features).
mask : :class:`~tensor.TensorVariable`
A 1D binary array in the shape (batch,) which is 1 if
there is data available, 0 if not. Assumed to be 1-s
only if not given.
time : :class:`~tensor.TensorVariable`
A number representing the time steps currently computed
"""
# TODO check which one is faster: switch or ifelse
next_states = tensor.switch(tensor.eq(time[0, 0] % self.period, 0),
self.children[0].apply(
inputs + tensor.dot(states, self.W)),
states)
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
time = time + tensor.ones_like(time)
return next_states, time
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.params[1][None, :], batch_size, 0),
self.params[2][None, :]]
class SoftGatedRecurrent(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, mlp=None,
**kwargs):
super(SoftGatedRecurrent, self).__init__(**kwargs)
self.dim = dim
if not activation:
activation = Tanh()
self.activation = activation
# The activation of the mlp should be a Logistic function
self.mlp = mlp
self.children = [activation, mlp]
@property
def state_to_state(self):
return self.params[0]
@property
def matrix_gate(self):
return self.params[1]
def get_dim(self, name):
if name == 'mask':
return 0
if name in ['inputs', 'states']:
return self.dim
return super(SoftGatedRecurrent, self).get_dim(name)
def _allocate(self):
self.params.append(shared_floatx_nans((self.dim, self.dim),
name='state_to_state'))
self.params.append(shared_floatx_zeros((self.dim,),
name="initial_state"))
add_role(self.params[0], WEIGHT)
add_role(self.params[1], INITIAL_STATE)
def _initialize(self):
self.weights_init.initialize(self.state_to_state, self.rng)
@recurrent(sequences=['mask', 'inputs'], states=['states'],
outputs=['states', "gate_value"], contexts=[])
def apply(self, inputs, states, mask=None):
"""Apply the gated recurrent transition.
Parameters
----------
states : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current states in the shape
(batch_size, dim). Required for `one_step` usage.
inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs in the shape (batch_size,
dim)
mask : :class:`~tensor.TensorVariable`
A 1D binary array in the shape (batch,) which is 1 if there is
data available, 0 if not. Assumed to be 1-s only if not given.
Returns
-------
output : :class:`~tensor.TensorVariable`
Next states of the network.
"""
# Concatenate the inputs of the MLP
mlp_input = tensor.concatenate((inputs, states), axis=1)
# Compute the output of the MLP
gate_value = self.mlp.apply(mlp_input)
# TODO: Find a way to remove the following "hack".
# Simply removing the two next lines won't work
gate_value = gate_value[:, 0]
gate_value = gate_value[:, None]
# Compute the next_states value, before gating
next_states = self.activation.apply(
states.dot(self.state_to_state) + inputs)
# Apply the gating
next_states = (next_states * gate_value +
states * (1 - gate_value))
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states, gate_value
@application(outputs=apply.states)
def initial_states(self, batch_size, *args, **kwargs):
return [tensor.repeat(self.params[2][None, :], batch_size, 0)]
class HardGatedRecurrent(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, mlp=None,
**kwargs):
super(HardGatedRecurrent, self).__init__(**kwargs)
self.dim = dim
if not activation:
activation = Tanh()
self.activation = activation
# The activation of the mlp should be a Logistic function
self.mlp = mlp
# The random stream
self.randomstream = MRG_RandomStreams()
self.children = [activation, mlp]
@property
def state_to_state(self):
return self.params[0]
@property
def matrix_gate(self):
return self.params[1]
def get_dim(self, name):
if name == 'mask':
return 0
if name in ['inputs', 'states']:
return self.dim
return super(HardGatedRecurrent, self).get_dim(name)
def _allocate(self):
self.params.append(shared_floatx_nans((self.dim, self.dim),
name='state_to_state'))
self.params.append(shared_floatx_zeros((self.dim,),
name="initial_state"))
add_role(self.params[0], WEIGHT)
add_role(self.params[1], INITIAL_STATE)
def _initialize(self):
self.weights_init.initialize(self.state_to_state, self.rng)
@recurrent(sequences=['mask', 'inputs'],
states=['states'], outputs=['states'], contexts=[])
def apply(self, inputs, states, mask=None):
"""Apply the gated recurrent transition.
Parameters
----------
states : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current states in the shape
(batch_size, dim). Required for `one_step` usage.
inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs in the shape (batch_size,
dim)
mask : :class:`~tensor.TensorVariable`
A 1D binary array in the shape (batch,) which is 1 if there is
data available, 0 if not. Assumed to be 1-s only if not given.
Returns
-------
output : :class:`~tensor.TensorVariable`
Next states of the network.
"""
# Concatenate the inputs of the MLP
mlp_input = tensor.concatenate((inputs, states), axis=1)
# Compute the output of the MLP
gate_value = self.mlp.apply(mlp_input)
random = self.randomstream.uniform((1,))
# TODO: Find a way to remove the following "hack".
# Simply removing the two next lines won't work
gate_value = gate_value[:, 0]
gate_value = gate_value[:, None]
# Compute the next_states value, before gating
next_states = self.activation.apply(
states.dot(self.state_to_state) + inputs)
# Apply the gating
next_states = tensor.switch(tensor.le(random[0], gate_value),
next_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.params[2][None, :], batch_size, 0)]
class LSTM(BaseRecurrent, Initializable):
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, **kwargs):
super(LSTM, self).__init__(**kwargs)
self.dim = dim
if not activation:
activation = Tanh()
self.children = [activation]
def get_dim(self, name):
if name == 'inputs':
return self.dim * 4
if name in ['states', 'cells']:
return self.dim
if name == 'mask':
return 0
return super(LSTM, 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.params = [
self.W_state, self.initial_state_, self.initial_cells]
def _initialize(self):
self.weights_init.initialize(self.params[0], self.rng)
@recurrent(sequences=['inputs', 'mask'], states=['states', 'cells'],
contexts=[], outputs=['states', 'cells', 'in_gate',
'forget_gate', 'out_gate'])
def apply(self, inputs, states, cells, mask=None):
def slice_last(x, no):
return x[:, no * self.dim: (no + 1) * self.dim]
nonlinearity = self.children[0].apply
activation = tensor.dot(states, self.W_state) + inputs
in_gate = tensor.nnet.sigmoid(slice_last(activation, 0))
forget_gate = tensor.nnet.sigmoid(slice_last(activation, 1))
next_cells = (forget_gate * cells +
in_gate * nonlinearity(slice_last(activation, 3)))
out_gate = tensor.nnet.sigmoid(slice_last(activation, 2))
next_states = out_gate * nonlinearity(next_cells)
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states, next_cells, in_gate, forget_gate, out_gate
@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)]
class HardLogistic(Activation):
@application(inputs=['input_'], outputs=['output'])
def apply(self, input_):
return tensor.nnet.hard_sigmoid(input_)