-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcustom_rnn.py
More file actions
673 lines (624 loc) · 31.9 KB
/
custom_rnn.py
File metadata and controls
673 lines (624 loc) · 31.9 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
import lasagne
from lasagne.layers import (DenseLayer,
InputLayer,
Layer,
ExpressionLayer,
BatchNormLayer)
from lasagne.layers import helper
import numpy as np
import theano
from theano import tensor as T
from lasagne.layers import MergeLayer
import lasagne.nonlinearities as nonlinearities
import lasagne.init as init
class CustomRecurrentLayer(MergeLayer):
"""
CustomRecurrentLayer(incoming, input_to_hidden,
hidden_to_hidden, nonlinearity=lasagne.nonlinearities.rectify,
hid_init=lasagne.init.Constant(0.), backwards=False,
learn_init=False, gradient_steps=-1, grad_clipping=0,
unroll_scan=False, precompute_input=True, mask_input=None,
only_return_final=False, **kwargs)
A layer which implements a recurrent connection.
This layer allows you to specify custom input-to-hidden and
hidden-to-hidden connections by instantiating :class:`lasagne.layers.Layer`
instances and passing them on initialization. Note that these connections
can consist of multiple layers chained together. The output shape for the
provided input-to-hidden and hidden-to-hidden connections must be the same.
If you are looking for a standard, densely-connected recurrent layer,
please see :class:`RecurrentLayer`. The output is computed by
.. math ::
h_t = \sigma(f_i(x_t) + f_h(h_{t-1}))
Parameters
----------
incoming : a :class:`lasagne.layers.Layer` instance or a tuple
The layer feeding into this layer, or the expected input shape.
input_to_hidden : :class:`lasagne.layers.Layer`
:class:`lasagne.layers.Layer` instance which connects input to the
hidden state (:math:`f_i`). This layer may be connected to a chain of
layers, which must end in a :class:`lasagne.layers.InputLayer` with the
same input shape as `incoming`, except for the first dimension: When
``precompute_input == True`` (the default), it must be
``incoming.output_shape[0]*incoming.output_shape[1]`` or ``None``; when
``precompute_input == False``, it must be ``incoming.output_shape[0]``
or ``None``.
hidden_to_hidden : :class:`lasagne.layers.Layer`
Layer which connects the previous hidden state to the new state
(:math:`f_h`). This layer may be connected to a chain of layers, which
must end in a :class:`lasagne.layers.InputLayer` with the same input
shape as `hidden_to_hidden`'s output shape.
nonlinearity : callable or None
Nonlinearity to apply when computing new state (:math:`\sigma`). If
None is provided, no nonlinearity will be applied.
hid_init : callable, np.ndarray, theano.shared or :class:`Layer`
Initializer for initial hidden state (:math:`h_0`).
backwards : bool
If True, process the sequence backwards and then reverse the
output again such that the output from the layer is always
from :math:`x_1` to :math:`x_n`.
learn_init : bool
If True, initial hidden values are learned.
gradient_steps : int
Number of timesteps to include in the backpropagated gradient.
If -1, backpropagate through the entire sequence.
grad_clipping : float
If nonzero, the gradient messages are clipped to the given value during
the backward pass. See [1]_ (p. 6) for further explanation.
unroll_scan : bool
If True the recursion is unrolled instead of using scan. For some
graphs this gives a significant speed up but it might also consume
more memory. When `unroll_scan` is True, backpropagation always
includes the full sequence, so `gradient_steps` must be set to -1 and
the input sequence length must be known at compile time (i.e., cannot
be given as None).
precompute_input : bool
If True, precompute input_to_hid before iterating through
the sequence. This can result in a speedup at the expense of
an increase in memory usage.
mask_input : :class:`lasagne.layers.Layer`
Layer which allows for a sequence mask to be input, for when sequences
are of variable length. Default `None`, which means no mask will be
supplied (i.e. all sequences are of the same length).
only_return_final : bool
If True, only return the final sequential output (e.g. for tasks where
a single target value for the entire sequence is desired). In this
case, Theano makes an optimization which saves memory.
Examples
--------
The following example constructs a simple `CustomRecurrentLayer` which
has dense input-to-hidden and hidden-to-hidden connections.
>>> import lasagne
>>> n_batch, n_steps, n_in = (2, 3, 4)
>>> n_hid = 5
>>> l_in = lasagne.layers.InputLayer((n_batch, n_steps, n_in))
>>> l_in_hid = lasagne.layers.DenseLayer(
... lasagne.layers.InputLayer((None, n_in)), n_hid)
>>> l_hid_hid = lasagne.layers.DenseLayer(
... lasagne.layers.InputLayer((None, n_hid)), n_hid)
>>> l_rec = lasagne.layers.CustomRecurrentLayer(l_in, l_in_hid, l_hid_hid)
The CustomRecurrentLayer can also support "convolutional recurrence", as is
demonstrated below.
>>> n_batch, n_steps, n_channels, width, height = (2, 3, 4, 5, 6)
>>> n_out_filters = 7
>>> filter_shape = (3, 3)
>>> l_in = lasagne.layers.InputLayer(
... (n_batch, n_steps, n_channels, width, height))
>>> l_in_to_hid = lasagne.layers.Conv2DLayer(
... lasagne.layers.InputLayer((None, n_channels, width, height)),
... n_out_filters, filter_shape, pad='same')
>>> l_hid_to_hid = lasagne.layers.Conv2DLayer(
... lasagne.layers.InputLayer(l_in_to_hid.output_shape),
... n_out_filters, filter_shape, pad='same')
>>> l_rec = lasagne.layers.CustomRecurrentLayer(
... l_in, l_in_to_hid, l_hid_to_hid)
References
----------
.. [1] Graves, Alex: "Generating sequences with recurrent neural networks."
arXiv preprint arXiv:1308.0850 (2013).
"""
def __init__(self, incoming, input_to_hidden, hidden_to_hidden,
gt_zeros,
nonlinearity=nonlinearities.rectify,
hid_init=init.Constant(0.),
backwards=False,
learn_init=False,
gradient_steps=-1,
grad_clipping=0,
unroll_scan=False,
precompute_input=True,
mask_input=None,
only_return_final=False,
**kwargs):
# This layer inherits from a MergeLayer, because it can have three
# inputs - the layer input, the mask and the initial hidden state. We
# will just provide the layer input as incomings, unless a mask input
# or initial hidden state was provided.
incomings = [incoming]
self.mask_incoming_index = -1
self.hid_init_incoming_index = -1
self.gt_zeros_index = -1
if mask_input is not None:
incomings.append(mask_input)
self.mask_incoming_index = len(incomings)-1
if isinstance(hid_init, Layer):
incomings.append(hid_init)
self.hid_init_incoming_index = len(incomings)-1
incomings.append(gt_zeros)
self.gt_zeros_index = len(incomings)-1
super(CustomRecurrentLayer, self).__init__(incomings, **kwargs)
self.input_to_hidden = input_to_hidden
self.hidden_to_hidden = hidden_to_hidden
self.learn_init = learn_init
self.backwards = backwards
self.gradient_steps = gradient_steps
self.grad_clipping = grad_clipping
self.unroll_scan = unroll_scan
self.precompute_input = precompute_input
self.only_return_final = only_return_final
if unroll_scan and gradient_steps != -1:
raise ValueError(
"Gradient steps must be -1 when unroll_scan is true.")
# Retrieve the dimensionality of the incoming layer
input_shape = self.input_shapes[0]
if unroll_scan and input_shape[1] is None:
raise ValueError("Input sequence length cannot be specified as "
"None when unroll_scan is True")
# Check that the input_to_hidden connection can appropriately handle
# a first dimension of input_shape[0]*input_shape[1] when we will
# precompute the input dot product
if (self.precompute_input and
input_to_hidden.output_shape[0] is not None and
input_shape[0] is not None and
input_shape[1] is not None and
(input_to_hidden.output_shape[0] !=
input_shape[0]*input_shape[1])):
raise ValueError(
'When precompute_input == True, '
'input_to_hidden.output_shape[0] must equal '
'incoming.output_shape[0]*incoming.output_shape[1] '
'(i.e. batch_size*sequence_length) or be None but '
'input_to_hidden.output_shape[0] = {} and '
'incoming.output_shape[0]*incoming.output_shape[1] = '
'{}'.format(input_to_hidden.output_shape[0],
input_shape[0]*input_shape[1]))
# Check that the first dimension of input_to_hidden and
# hidden_to_hidden's outputs match when we won't precompute the input
# dot product
if (not self.precompute_input and
input_to_hidden.output_shape[0] is not None and
hidden_to_hidden.output_shape[0] is not None and
(input_to_hidden.output_shape[0] !=
hidden_to_hidden.output_shape[0])):
raise ValueError(
'When precompute_input == False, '
'input_to_hidden.output_shape[0] must equal '
'hidden_to_hidden.output_shape[0] but '
'input_to_hidden.output_shape[0] = {} and '
'hidden_to_hidden.output_shape[0] = {}'.format(
input_to_hidden.output_shape[0],
hidden_to_hidden.output_shape[0]))
# Check that input_to_hidden and hidden_to_hidden output shapes match,
# but don't check a dimension if it's None for either shape
if not all(s1 is None or s2 is None or s1 == s2
for s1, s2 in zip(input_to_hidden.output_shape[1:],
hidden_to_hidden.output_shape[1:])):
raise ValueError("The output shape for input_to_hidden and "
"hidden_to_hidden must be equal after the first "
"dimension, but input_to_hidden.output_shape={} "
"and hidden_to_hidden.output_shape={}".format(
input_to_hidden.output_shape,
hidden_to_hidden.output_shape))
# Check that input_to_hidden's output shape is the same as
# hidden_to_hidden's input shape but don't check a dimension if it's
# None for either shape
if not all(s1 is None or s2 is None or s1 == s2
for s1, s2 in zip(input_to_hidden.output_shape[1:],
hidden_to_hidden.input_shape[1:])):
raise ValueError("The output shape for input_to_hidden "
"must be equal to the input shape of "
"hidden_to_hidden after the first dimension, but "
"input_to_hidden.output_shape={} and "
"hidden_to_hidden.input_shape={}".format(
input_to_hidden.output_shape,
hidden_to_hidden.input_shape))
if nonlinearity is None:
self.nonlinearity = nonlinearities.identity
else:
self.nonlinearity = nonlinearity
# Initialize hidden state
if isinstance(hid_init, Layer):
self.hid_init = hid_init
else:
self.hid_init = self.add_param(
hid_init, (1,) + hidden_to_hidden.output_shape[1:],
name="hid_init", trainable=learn_init, regularizable=False)
def get_params(self, **tags):
# Get all parameters from this layer, the master layer
params = super(CustomRecurrentLayer, self).get_params(**tags)
# Combine with all parameters from the child layers
params += helper.get_all_params(self.input_to_hidden, **tags)
params += helper.get_all_params(self.hidden_to_hidden, **tags)
return params
def get_output_shape_for(self, input_shapes):
# The shape of the input to this layer will be the first element
# of input_shapes, whether or not a mask input is being used.
input_shape = input_shapes[0]
# When only_return_final is true, the second (sequence step) dimension
# will be flattened
if self.only_return_final:
return (input_shape[0],) + self.hidden_to_hidden.output_shape[1:]
# Otherwise, the shape will be (n_batch, n_steps, trailing_dims...)
else:
return ((input_shape[0], input_shape[1]) +
self.hidden_to_hidden.output_shape[1:])
def get_output_for(self, inputs, **kwargs):
"""
Compute this layer's output function given a symbolic input variable.
Parameters
----------
inputs : list of theano.TensorType
`inputs[0]` should always be the symbolic input variable. When
this layer has a mask input (i.e. was instantiated with
`mask_input != None`, indicating that the lengths of sequences in
each batch vary), `inputs` should have length 2, where `inputs[1]`
is the `mask`. The `mask` should be supplied as a Theano variable
denoting whether each time step in each sequence in the batch is
part of the sequence or not. `mask` should be a matrix of shape
``(n_batch, n_time_steps)`` where ``mask[i, j] = 1`` when ``j <=
(length of sequence i)`` and ``mask[i, j] = 0`` when ``j > (length
of sequence i)``. When the hidden state of this layer is to be
pre-filled (i.e. was set to a :class:`Layer` instance) `inputs`
should have length at least 2, and `inputs[-1]` is the hidden state
to prefill with.
Returns
-------
layer_output : theano.TensorType
Symbolic output variable.
"""
# Retrieve the layer input
input = inputs[0]
# Retrieve the mask when it is supplied
mask = None
hid_init = None
if self.mask_incoming_index > 0:
mask = inputs[self.mask_incoming_index]
if self.hid_init_incoming_index > 0:
hid_init = inputs[self.hid_init_incoming_index]
gt_zeros = inputs[self.gt_zeros_index]
# Input should be provided as (n_batch, n_time_steps, n_features)
# but scan requires the iterable dimension to be first
# So, we need to dimshuffle to (n_time_steps, n_batch, n_features)
input = input.dimshuffle(1, 0, *range(2, input.ndim))
gt_zeros = gt_zeros.dimshuffle(1, 0, *range(2, gt_zeros.ndim))
seq_len, num_batch = input.shape[0], input.shape[1]
if self.precompute_input:
# Because the input is given for all time steps, we can precompute
# the inputs to hidden before scanning. First we need to reshape
# from (seq_len, batch_size, trailing dimensions...) to
# (seq_len*batch_size, trailing dimensions...)
# This strange use of a generator in a tuple was because
# input.shape[2:] was raising a Theano error
trailing_dims = tuple(input.shape[n] for n in range(2, input.ndim))
input = T.reshape(input, (seq_len*num_batch,) + trailing_dims)
input = helper.get_output(
self.input_to_hidden, input, **kwargs)
# Reshape back to (seq_len, batch_size, trailing dimensions...)
trailing_dims = tuple(input.shape[n] for n in range(1, input.ndim))
input = T.reshape(input, (seq_len, num_batch) + trailing_dims)
# We will always pass the hidden-to-hidden layer params to step
non_seqs = helper.get_all_params(self.hidden_to_hidden)
# When we are not precomputing the input, we also need to pass the
# input-to-hidden parameters to step
if not self.precompute_input:
non_seqs += helper.get_all_params(self.input_to_hidden)
dims = (seq_len, num_batch) + trailing_dims
# Create single recurrent computation step function
def step(input_n, zero_n, hid_previous, *args):
# Compute the hidden-to-hidden activation
hid_pre = helper.get_output(
self.hidden_to_hidden, hid_previous, **kwargs)
# If the dot product is precomputed then add it, otherwise
# calculate the input_to_hidden values and add them
if self.precompute_input:
hid_pre += input_n
else:
hid_pre += helper.get_output(
self.input_to_hidden, input_n, **kwargs)
# Clip gradients
if self.grad_clipping:
hid_pre = theano.gradient.grad_clip(
hid_pre, -self.grad_clipping, self.grad_clipping)
return self.nonlinearity(hid_pre) + zero_n
def step_masked(input_n, zero_n, mask_n, hid_previous, *args):
# Skip over any input with mask 0 by copying the previous
# hidden state; proceed normally for any input with mask 1.
hid = step(input_n, zero_n, hid_previous, *args)
hid_out = T.switch(mask_n, hid, hid_previous)
return [hid_out]
if mask is not None:
mask = mask.dimshuffle(1, 0, 'x')
sequences = [input, gt_zeros, mask]
step_fun = step_masked
else:
sequences = [input, gt_zeros]
step_fun = step
if not isinstance(self.hid_init, Layer):
# The code below simply repeats self.hid_init num_batch times in
# its first dimension. Turns out using a dot product and a
# dimshuffle is faster than T.repeat.
dot_dims = (list(range(1, self.hid_init.ndim - 1)) +
[0, self.hid_init.ndim - 1])
hid_init = T.dot(T.ones((num_batch, 1)),
self.hid_init.dimshuffle(dot_dims))
if self.unroll_scan:
# Retrieve the dimensionality of the incoming layer
input_shape = self.input_shapes[0]
# Explicitly unroll the recurrence instead of using scan
hid_out = unroll_scan(
fn=step_fun,
sequences=sequences,
outputs_info=[hid_init],
go_backwards=self.backwards,
non_sequences=non_seqs,
n_steps=input_shape[1])[0]
else:
# Scan op iterates over first dimension of input and repeatedly
# applies the step function
hid_out = theano.scan(
fn=step_fun,
sequences=sequences,
go_backwards=self.backwards,
outputs_info=[hid_init],
non_sequences=non_seqs,
truncate_gradient=self.gradient_steps,
strict=True)[0]
# When it is requested that we only return the final sequence step,
# we need to slice it out immediately after scan is applied
if self.only_return_final:
hid_out = hid_out[-1]
else:
# dimshuffle back to (n_batch, n_time_steps, n_features))
hid_out = hid_out.dimshuffle(1, 0, *range(2, hid_out.ndim))
#gt_zeros = gt_zeros.dimshuffle(1, 0, *range(2, hid_out.ndim))
# if scan is backward reverse the output
if self.backwards:
hid_out = hid_out[:, ::-1]
#return [hid_out, gt_zeros]
return hid_out
class SVDFactorizedDenseLayer(Layer):
def __init__(self, incoming, num_units,
W_hid_to_hid=lasagne.init.Orthogonal(),
b=lasagne.init.Constant(0.),
nonlinearity=lasagne.nonlinearities.rectify,
hard_spectral_boundary=None,
**kwargs):
self.hard_spectral_boundary = hard_spectral_boundary
super(SVDFactorizedDenseLayer, self).__init__(incoming, **kwargs)
self.nonlinearity = (lasagne.nonlinearities.identity \
if nonlinearity is None
else nonlinearity)
self.num_units = num_units
num_inputs = int(np.prod(self.input_shape[1:]))
W_rand = W_hid_to_hid.sample((num_inputs, num_units))
W_rand = W_rand.astype(theano.config.floatX)
W_u, _, W_v = np.linalg.svd(W_rand, full_matrices=False)
self.W_u = self.add_param(W_u, W_u.shape, name="W_u", basis=True)
self.W_v = self.add_param(W_v, W_v.shape, name="W_v", basis=True)
if hard_spectral_boundary is not None:
sb = self.hard_spectral_boundary
W_s_params = theano.shared(np.zeros(num_units,
dtype=theano.config.floatX))
self.W_s_params = self.add_param(W_s_params, shape=(num_units,),
name="W_s_params")
W_s = 2*sb*T.nnet.sigmoid(W_s_params)+(1-sb)
W_s = W_s.dimshuffle(('x',0)) # Make it a row vector
self.W_s = self.add_param(W_s, shape=(1,num_units),
name="W_s", spectrum=True,
trainable=False, regularizable=True)
else:
W_s = np.ones((1,num_units), dtype=theano.config.floatX)
self.W_s = self.add_param(W_s, (1,num_units), name="W_s",
spectrum=True)
W = T.dot(self.W_u*lasagne.nonlinearities.rectify(self.W_s), self.W_v)
self.W = self.add_param(W, (num_inputs, num_units), name="W",
trainable=False, regularizable=True)
if b is None:
self.b = None
else:
self.b = self.add_param(b, (num_units,), name="b",
regularizable=False)
def get_output_shape_for(self, input_shape):
return (input_shape[0], self.num_units)
def get_output_for(self, input, **kwargs):
if input.ndim > 2:
# if the input has more than two dimensions, flatten it into a
# batch of feature vectors.
input = input.flatten(2)
activation = T.dot(input, self.W)
if self.b is not None:
activation = activation + self.b.dimshuffle('x', 0)
return self.nonlinearity(activation)
class FactorizedRecurrentLayer(CustomRecurrentLayer):
def __init__(self, incoming, num_units,
gt_zeros,
W_in_to_hid=lasagne.init.Uniform(),
b=lasagne.init.Constant(0.),
nonlinearity=lasagne.nonlinearities.rectify,
hid_init=lasagne.init.Constant(0.),
backwards=False,
learn_init=False,
gradient_steps=-1,
grad_clipping=0,
unroll_scan=False,
precompute_input=True,
mask_input=None,
only_return_final=False,
hard_spectral_boundary=None,
**kwargs):
self.hard_spectral_boundary = hard_spectral_boundary
if isinstance(incoming, tuple):
input_shape = incoming
else:
input_shape = incoming.output_shape
# Retrieve the supplied name, if it exists; otherwise use ''
if 'name' in kwargs:
basename = kwargs['name'] + '.'
# Create a separate version of kwargs for the contained layers
# which does not include 'name'
layer_kwargs = dict((key, arg) for key, arg in kwargs.items()
if key != 'name')
else:
basename = ''
layer_kwargs = kwargs
# We will be passing the input at each time step to the dense layer,
# so we need to remove the second dimension (the time dimension)
in_to_hid = DenseLayer(InputLayer((None,) + input_shape[2:]),
num_units, W=W_in_to_hid, b=b,
nonlinearity=None,
name=basename + 'input_to_hidden',
**layer_kwargs)
# The hidden-to-hidden layer expects its inputs to have num_units
# features because it recycles the previous hidden state
hid_to_hid = SVDFactorizedDenseLayer(InputLayer((None, num_units)),
num_units, b=None,
nonlinearity=None,
name=basename + 'hidden_to_hidden',
hard_spectral_boundary=hard_spectral_boundary,
**layer_kwargs)
# Make child layer parameters intuitively accessible
self.W_in_to_hid = in_to_hid.W
self.W_hid_to_hid = hid_to_hid.W
self.b = in_to_hid.b
# Just use the CustomRecurrentLayer with the DenseLayers we created
super(FactorizedRecurrentLayer, self).__init__(
incoming, in_to_hid, hid_to_hid,
gt_zeros=gt_zeros, nonlinearity=nonlinearity,
hid_init=hid_init, backwards=backwards, learn_init=learn_init,
gradient_steps=gradient_steps,
grad_clipping=grad_clipping, unroll_scan=unroll_scan,
precompute_input=precompute_input, mask_input=mask_input,
only_return_final=only_return_final) # , **kwargs)
class RecurrentLayer(CustomRecurrentLayer):
"""
lasagne.layers.recurrent.RecurrentLayer(incoming, num_units,
W_in_to_hid=lasagne.init.Uniform(), W_hid_to_hid=lasagne.init.Uniform(),
b=lasagne.init.Constant(0.), nonlinearity=lasagne.nonlinearities.rectify,
hid_init=lasagne.init.Constant(0.), backwards=False, learn_init=False,
gradient_steps=-1, grad_clipping=0, unroll_scan=False,
precompute_input=True, mask_input=None, only_return_final=False, **kwargs)
Dense recurrent neural network (RNN) layer
A "vanilla" RNN layer, which has dense input-to-hidden and
hidden-to-hidden connections. The output is computed as
.. math ::
h_t = \sigma(x_t W_x + h_{t-1} W_h + b)
Parameters
----------
incoming : a :class:`lasagne.layers.Layer` instance or a tuple
The layer feeding into this layer, or the expected input shape.
num_units : int
Number of hidden units in the layer.
W_in_to_hid : Theano shared variable, numpy array or callable
Initializer for input-to-hidden weight matrix (:math:`W_x`).
W_hid_to_hid : Theano shared variable, numpy array or callable
Initializer for hidden-to-hidden weight matrix (:math:`W_h`).
b : Theano shared variable, numpy array, callable or None
Initializer for bias vector (:math:`b`). If None is provided there will
be no bias.
nonlinearity : callable or None
Nonlinearity to apply when computing new state (:math:`\sigma`). If
None is provided, no nonlinearity will be applied.
hid_init : callable, np.ndarray, theano.shared or :class:`Layer`
Initializer for initial hidden state (:math:`h_0`).
backwards : bool
If True, process the sequence backwards and then reverse the
output again such that the output from the layer is always
from :math:`x_1` to :math:`x_n`.
learn_init : bool
If True, initial hidden values are learned.
gradient_steps : int
Number of timesteps to include in the backpropagated gradient.
If -1, backpropagate through the entire sequence.
grad_clipping : float
If nonzero, the gradient messages are clipped to the given value during
the backward pass. See [1]_ (p. 6) for further explanation.
unroll_scan : bool
If True the recursion is unrolled instead of using scan. For some
graphs this gives a significant speed up but it might also consume
more memory. When `unroll_scan` is True, backpropagation always
includes the full sequence, so `gradient_steps` must be set to -1 and
the input sequence length must be known at compile time (i.e., cannot
be given as None).
precompute_input : bool
If True, precompute input_to_hid before iterating through
the sequence. This can result in a speedup at the expense of
an increase in memory usage.
mask_input : :class:`lasagne.layers.Layer`
Layer which allows for a sequence mask to be input, for when sequences
are of variable length. Default `None`, which means no mask will be
supplied (i.e. all sequences are of the same length).
only_return_final : bool
If True, only return the final sequential output (e.g. for tasks where
a single target value for the entire sequence is desired). In this
case, Theano makes an optimization which saves memory.
References
----------
.. [1] Graves, Alex: "Generating sequences with recurrent neural networks."
arXiv preprint arXiv:1308.0850 (2013).
"""
def __init__(self, incoming, num_units,
gt_zeros,
W_in_to_hid=init.Uniform(),
W_hid_to_hid=init.Uniform(),
b=init.Constant(0.),
nonlinearity=nonlinearities.rectify,
hid_init=init.Constant(0.),
backwards=False,
learn_init=False,
gradient_steps=-1,
grad_clipping=0,
unroll_scan=False,
precompute_input=True,
mask_input=None,
only_return_final=False,
**kwargs):
if isinstance(incoming, tuple):
input_shape = incoming
else:
input_shape = incoming.output_shape
# Retrieve the supplied name, if it exists; otherwise use ''
if 'name' in kwargs:
basename = kwargs['name'] + '.'
# Create a separate version of kwargs for the contained layers
# which does not include 'name'
layer_kwargs = dict((key, arg) for key, arg in kwargs.items()
if key != 'name')
else:
basename = ''
layer_kwargs = kwargs
# We will be passing the input at each time step to the dense layer,
# so we need to remove the second dimension (the time dimension)
in_to_hid = DenseLayer(InputLayer((None,) + input_shape[2:]),
num_units, W=W_in_to_hid, b=b,
nonlinearity=None,
name=basename + 'input_to_hidden',
**layer_kwargs)
# The hidden-to-hidden layer expects its inputs to have num_units
# features because it recycles the previous hidden state
hid_to_hid = DenseLayer(InputLayer((None, num_units)),
num_units, W=W_hid_to_hid, b=None,
nonlinearity=None,
name=basename + 'hidden_to_hidden',
**layer_kwargs)
# Make child layer parameters intuitively accessible
self.W_in_to_hid = in_to_hid.W
self.W_hid_to_hid = hid_to_hid.W
self.b = in_to_hid.b
# Just use the CustomRecurrentLayer with the DenseLayers we created
super(RecurrentLayer, self).__init__(
incoming, in_to_hid, hid_to_hid,
gt_zeros=gt_zeros, nonlinearity=nonlinearity,
hid_init=hid_init, backwards=backwards, learn_init=learn_init,
gradient_steps=gradient_steps,
grad_clipping=grad_clipping, unroll_scan=unroll_scan,
precompute_input=precompute_input, mask_input=mask_input,
only_return_final=only_return_final, **kwargs)