-
Notifications
You must be signed in to change notification settings - Fork 0
/
recurrent.py
740 lines (611 loc) · 27.9 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
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# -*- coding: utf-8 -*-
import copy
import inspect
import logging
from functools import wraps
from picklable_itertools.extras import equizip
import theano
from theano import tensor, Variable
from theano.ifelse import ifelse
from blocks.bricks import Initializable, Sigmoid, Tanh
from blocks.bricks.base import Application, application, Brick, lazy
from blocks.initialization import NdarrayInitialization
from blocks.roles import add_role, WEIGHT, BIAS
from blocks.utils import (pack, shared_floatx_nans, dict_union, dict_subset,
is_shared_variable)
logger = logging.getLogger()
unknown_scan_input = """
Your function uses a non-shared variable other than those given \
by scan explicitly. That can significantly slow down `tensor.grad` \
call. Did you forget to declare it in `contexts`?"""
class BaseRecurrent(Brick):
"""Base class for brick with recurrent application method."""
has_bias = False
@application
def initial_state(self, state_name, batch_size, *args, **kwargs):
r"""Return an initial state for an application call.
Parameters
----------
state_name : str
The name of the state.
batch_size : int
The batch size.
\*args
The positional arguments of the application call.
\*\*kwargs
The keyword arguments of the application call.
"""
dim = self.get_dim(state_name)
if dim == 0:
return tensor.zeros((batch_size,))
return tensor.zeros((batch_size, dim))
def recurrent(*args, **kwargs):
"""Wraps an apply method to allow its iterative application.
This decorator allows you to use implementation of an RNN
transition to process sequences without writing the
iteration-related code again and again. In the most general form
information flow of a recurrent network can be described as
follows: depending on the context variables and driven by input
sequences the RNN updates its states and produces output sequences.
Thus the input variables of your transition function play one of
three roles: an input, a context or a state. These roles should be
specified in the method's signature to make iteration possible.
Parameters
----------
inputs : list of strs
Names of the arguments of the apply method that play input
roles.
states : list of strs
Names of the arguments of the apply method that play state
roles.
contexts : list of strs
Names of the arguments of the apply method that play context
roles.
outputs : list of strs
Names of the outputs.
"""
def recurrent_wrapper(application_function):
arg_spec = inspect.getargspec(application_function)
arg_names = arg_spec.args[1:]
@wraps(application_function)
def recurrent_apply(brick, application, application_call,
*args, **kwargs):
"""Iterates a transition function.
Parameters
----------
iterate : bool
If ``True`` iteration is made. By default ``True``.
reverse : bool
If ``True``, the sequences are processed in backward
direction. ``False`` by default.
return_initial_states : bool
If ``True``, initial states are included in the returned
state tensors. ``False`` by default.
.. todo::
* Handle `updates` returned by the :func:`theano.scan`
routine.
* ``kwargs`` has a random order; check if this is a
problem.
"""
# Extract arguments related to iteration and immediately relay the
# call to the wrapped function if `iterate=False`
iterate = kwargs.pop('iterate', True)
if not iterate:
return application_function(brick, *args, **kwargs)
reverse = kwargs.pop('reverse', False)
return_initial_states = kwargs.pop('return_initial_states', False)
# Push everything to kwargs
for arg, arg_name in zip(args, arg_names):
kwargs[arg_name] = arg
# Make sure that all arguments for scan are tensor variables
scan_arguments = (application.sequences + application.states +
application.contexts)
for arg in scan_arguments:
if arg in kwargs:
if kwargs[arg] is None:
del kwargs[arg]
else:
kwargs[arg] = tensor.as_tensor_variable(kwargs[arg])
# Check which sequence and contexts were provided
sequences_given = dict_subset(kwargs, application.sequences,
must_have=False)
contexts_given = dict_subset(kwargs, application.contexts,
must_have=False)
# Determine number of steps and batch size.
if len(sequences_given):
# TODO Assumes 1 time dim!
shape = list(sequences_given.values())[0].shape
if not iterate:
batch_size = shape[0]
else:
n_steps = shape[0]
batch_size = shape[1]
else:
# TODO Raise error if n_steps and batch_size not found?
n_steps = kwargs.pop('n_steps')
batch_size = kwargs.pop('batch_size')
# Handle the rest kwargs
rest_kwargs = {key: value for key, value in kwargs.items()
if key not in scan_arguments}
for value in rest_kwargs.values():
if (isinstance(value, Variable) and not
is_shared_variable(value)):
logger.warning("unknown input {}".format(value) +
unknown_scan_input)
# Ensure that all initial states are available.
for state_name in application.states:
dim = brick.get_dim(state_name)
if state_name in kwargs:
if isinstance(kwargs[state_name], NdarrayInitialization):
kwargs[state_name] = tensor.alloc(
kwargs[state_name].generate(brick.rng, (1, dim)),
batch_size, dim)
elif isinstance(kwargs[state_name], Application):
kwargs[state_name] = (
kwargs[state_name](state_name, batch_size,
*args, **kwargs))
else:
# TODO init_func returns 2D-tensor, fails for iterate=False
kwargs[state_name] = (
brick.initial_state(state_name, batch_size,
*args, **kwargs))
assert kwargs[state_name]
states_given = dict_subset(kwargs, application.states)
# Theano issue 1772
for name, state in states_given.items():
states_given[name] = tensor.unbroadcast(state,
*range(state.ndim))
def scan_function(*args):
args = list(args)
arg_names = (list(sequences_given) +
[output for output in application.outputs
if output in application.states] +
list(contexts_given))
kwargs = dict(equizip(arg_names, args))
kwargs.update(rest_kwargs)
outputs = application(iterate=False, **kwargs)
# We want to save the computation graph returned by the
# `application_function` when it is called inside the
# `theano.scan`.
application_call.inner_inputs = args
application_call.inner_outputs = pack(outputs)
return outputs
outputs_info = [
states_given[name] if name in application.states
else None
for name in application.outputs]
result, updates = theano.scan(
scan_function, sequences=list(sequences_given.values()),
outputs_info=outputs_info,
non_sequences=list(contexts_given.values()),
n_steps=n_steps,
go_backwards=reverse)
result = pack(result)
if return_initial_states:
# Undo Subtensor
for i in range(len(states_given)):
assert isinstance(result[i].owner.op,
tensor.subtensor.Subtensor)
result[i] = result[i].owner.inputs[0]
if updates:
application_call.updates = dict_union(application_call.updates,
updates)
return result
return recurrent_apply
# Decorator can be used with or without arguments
assert (args and not kwargs) or (not args and kwargs)
if args:
application_function, = args
return application(recurrent_wrapper(application_function))
else:
def wrap_application(application_function):
return application(**kwargs)(
recurrent_wrapper(application_function))
return wrap_application
class SimpleRecurrent(BaseRecurrent, Initializable):
"""The traditional recurrent transition.
The most well-known recurrent transition: a matrix multiplication,
optionally followed by a non-linearity.
Parameters
----------
dim : int
The dimension of the hidden state
activation : :class:`.Brick`
The brick to apply as activation.
Notes
-----
See :class:`.Initializable` for initialization parameters.
"""
@lazy(allocation=['dim'])
def __init__(self, dim, activation, **kwargs):
super(SimpleRecurrent, self).__init__(**kwargs)
self.dim = dim
self.children = [activation]
@property
def W(self):
return self.params[0]
def get_dim(self, name):
if name == 'mask':
return 0
if name in (SimpleRecurrent.apply.sequences +
SimpleRecurrent.apply.states):
return self.dim
return super(SimpleRecurrent, self).get_dim(name)
def _allocate(self):
self.params.append(shared_floatx_nans((self.dim, self.dim), name="W"))
def _initialize(self):
self.weights_init.initialize(self.W, self.rng)
@recurrent(sequences=['inputs', 'mask'], states=['states'],
outputs=['states'], contexts=[])
def apply(self, inputs=None, states=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.
"""
next_states = inputs + tensor.dot(states, self.W)
next_states = self.children[0].apply(next_states)
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states
class LSTM(BaseRecurrent, Initializable):
u"""Long Short Term Memory.
Every unit of an LSTM is equipped with input, forget and output gates.
This implementation is based on code by Mohammad Pezeshki that
implements the architecture used in [GSS03]_ and [Grav13]_. It aims to
do as many computations in parallel as possible and expects the last
dimension of the input to be four times the output dimension.
Unlike a vanilla LSTM as described in [HS97]_, this model has peephole
connections from the cells to the gates. The output gates receive
information about the cells at the current time step, while the other
gates only receive information about the cells at the previous time
step. All 'peephole' weight matrices are diagonal.
.. [GSS03] Gers, Felix A., Nicol N. Schraudolph, and Jürgen
Schmidhuber, *Learning precise timing with LSTM recurrent
networks*, Journal of Machine Learning Research 3 (2003),
pp. 115-143.
.. [Grav13] Graves, Alex, *Generating sequences with recurrent neural
networks*, arXiv preprint arXiv:1308.0850 (2013).
.. [HS97] Sepp Hochreiter, and Jürgen Schmidhuber, *Long Short-Term
Memory*, Neural Computation 9(8) (1997), pp. 1735-1780.
Parameters
----------
dim : int
The dimension of the hidden state.
activation : :class:`.Brick`, optional
The activation function. The default and by far the most popular
is :class:`.Tanh`.
Notes
-----
See :class:`.Initializable` for initialization parameters.
"""
@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')
self.W_cell_to_in = shared_floatx_nans((self.dim,),
name='W_cell_to_in')
self.W_cell_to_forget = shared_floatx_nans((self.dim,),
name='W_cell_to_forget')
self.W_cell_to_out = shared_floatx_nans((self.dim,),
name='W_cell_to_out')
self.biases = shared_floatx_nans((4*self.dim,), name='biases')
add_role(self.W_state, WEIGHT)
add_role(self.W_cell_to_in, WEIGHT)
add_role(self.W_cell_to_forget, WEIGHT)
add_role(self.W_cell_to_out, WEIGHT)
add_role(self.biases, BIAS)
self.params = [self.W_state, self.W_cell_to_in, self.W_cell_to_forget,
self.W_cell_to_out, self.biases]
def _initialize(self):
self.biases_init.initialize(self.biases, self.rng)
for w in self.params[:-1]:
self.weights_init.initialize(w, self.rng)
@recurrent(sequences=['inputs', 'mask'], states=['states', 'cells'],
contexts=[], outputs=['states', 'cells'])
def apply(self, inputs, states, cells, mask=None):
"""Apply the Long Short Term Memory transition.
Parameters
----------
states : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current states in the shape
(batch_size, features). Required for `one_step` usage.
cells : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current cells in the shape
(batch_size, features). Required for `one_step` usage.
inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs in the shape (batch_size,
features * 4).
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
-------
states : :class:`~tensor.TensorVariable`
Next states of the network.
cells : :class:`~tensor.TensorVariable`
Next cell activations of the network.
"""
def slice_last(x, no):
return x.T[no*self.dim: (no+1)*self.dim].T
nonlinearity = self.children[0].apply
activation = tensor.dot(states, self.W_state) + inputs + self.biases
in_gate = tensor.nnet.sigmoid(slice_last(activation, 0) +
cells * self.W_cell_to_in)
forget_gate = tensor.nnet.sigmoid(slice_last(activation, 1) +
cells * self.W_cell_to_forget)
next_cells = (forget_gate * cells +
in_gate * nonlinearity(slice_last(activation, 2)))
out_gate = tensor.nnet.sigmoid(slice_last(activation, 3) +
next_cells * self.W_cell_to_out)
next_states = out_gate * nonlinearity(next_cells)
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
class GatedRecurrent(BaseRecurrent, Initializable):
u"""Gated recurrent neural network.
Gated recurrent neural network (GRNN) as introduced in [CvMG14]_. Every
unit of a GRNN is equipped with update and reset gates that facilitate
better gradient propagation.
Parameters
----------
dim : int
The dimension of the hidden state.
activation : :class:`.Brick` or None
The brick to apply as activation. If ``None`` a
:class:`.Tanh` brick is used.
gate_activation : :class:`.Brick` or None
The brick to apply as activation for gates. If ``None`` a
:class:`.Sigmoid` brick is used.
use_upgate_gate : bool
If True the update gates are used.
use_reset_gate : bool
If True the reset gates are used.
Notes
-----
See :class:`.Initializable` for initialization parameters.
.. [CvMG14] Kyunghyun Cho, Bart van Merriënboer, Çağlar Gülçehre,
Dzmitry Bahdanau, Fethi Bougares, Holger Schwenk, and Yoshua
Bengio, *Learning Phrase Representations using RNN Encoder-Decoder
for Statistical Machine Translation*, EMNLP (2014), pp. 1724-1734.
"""
@lazy(allocation=['dim'])
def __init__(self, dim, activation=None, gate_activation=None,
use_update_gate=True, use_reset_gate=True, **kwargs):
super(GatedRecurrent, self).__init__(**kwargs)
self.dim = dim
self.use_update_gate = use_update_gate
self.use_reset_gate = use_reset_gate
if not activation:
activation = Tanh()
if not gate_activation:
gate_activation = Sigmoid()
self.activation = activation
self.gate_activation = gate_activation
self.children = [activation, gate_activation]
@property
def state_to_state(self):
return self.params[0]
@property
def state_to_update(self):
return self.params[1]
@property
def state_to_reset(self):
return self.params[2]
def get_dim(self, name):
if name == 'mask':
return 0
if name in self.apply.sequences + self.apply.states:
return self.dim
return super(GatedRecurrent, self).get_dim(name)
def _allocate(self):
def new_param(name):
return shared_floatx_nans((self.dim, self.dim), name=name)
self.params.append(new_param('state_to_state'))
self.params.append(new_param('state_to_update')
if self.use_update_gate else None)
self.params.append(new_param('state_to_reset')
if self.use_reset_gate else None)
def _initialize(self):
self.weights_init.initialize(self.state_to_state, self.rng)
if self.use_update_gate:
self.weights_init.initialize(self.state_to_update, self.rng)
if self.use_reset_gate:
self.weights_init.initialize(self.state_to_reset, self.rng)
@recurrent(states=['states'], outputs=['states'], contexts=[])
def apply(self, inputs, update_inputs=None, reset_inputs=None,
states=None, mask=None):
"""Apply the gated recurrent transition.
Parameters
----------
states : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of current states in the shape
(batch_size, features). Required for `one_step` usage.
inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs in the shape (batch_size,
features)
update_inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs to the update gates in the
shape (batch_size, features). None when the update gates are
not used.
reset_inputs : :class:`~tensor.TensorVariable`
The 2 dimensional matrix of inputs to the reset gates in the
shape (batch_size, features). None when the reset gates are not
used.
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.
"""
if (self.use_update_gate != (update_inputs is not None)) or \
(self.use_reset_gate != (reset_inputs is not None)):
raise ValueError("Configuration and input mismatch: You should "
"provide inputs for gates if and only if the "
"gates are on.")
states_reset = states
if self.use_reset_gate:
reset_values = self.gate_activation.apply(
states.dot(self.state_to_reset) + reset_inputs)
states_reset = states * reset_values
next_states = self.activation.apply(
states_reset.dot(self.state_to_state) + inputs)
if self.use_update_gate:
update_values = self.gate_activation.apply(
states.dot(self.state_to_update) + update_inputs)
next_states = (next_states * update_values +
states * (1 - update_values))
if mask:
next_states = (mask[:, None] * next_states +
(1 - mask[:, None]) * states)
return next_states
@apply.property('sequences')
def apply_inputs(self):
sequences = ['mask', 'inputs']
if self.use_update_gate:
sequences.append('update_inputs')
if self.use_reset_gate:
sequences.append('reset_inputs')
return sequences
class ClockWork(BaseRecurrent, Initializable):
u"""A Clockwork Module, elementary brick to build the Clockwork RNN.
TODO: correct description
Parameters
----------
module : int
The number modules.
periods : numpy.array
1D vector of shape (module) containing the time period for each module
Assumed to be sorted in ascending order
unit : numpy.array
Number of units inside each module
input size :
activation : :class:`.Brick` or None
The brick to apply as activation. If ``None`` a
:class:`.Tanh` brick is used.
Notes
-----
See :class:`.Initializable` for initialization parameters.
"""
@lazy()
def __init__(self, input_dim=None, module=None, periods=None, unit=None, activation=None, **kwargs):
super(ClockWork, self).__init__(**kwargs)
self.input_dim = input_dim
self.module = module
self.periods = periods
self.unit = unit
self.dim = self.module * self.unit
if not activation:
activation = Tanh()
self.activation = activation
self.children = [activation]
def get_dim(self, name):
if name == 'mask':
return 0
if name in self.apply.sequences + self.apply.states:
return self.unit * self.module
return super(ClockWork, self).get_dim(name)
def _allocate(self):
self.Wh = shared_floatx_nans((self.dim, self.dim), name='Wh')
self.Wi = shared_floatx_nans((self.input_dim, self.dim), name='Wi')
self.b = shared_floatx_nans((1,self.dim), name='b')
self.params.append(self.Wi)
self.params.append(self.Wh)
self.params.append(self.b)
add_role(self.Wi, WEIGHT)
add_role(self.Wh, WEIGHT)
add_role(self.b, BIAS)
#triangular matrix !
def _initialize(self):
self.weights_init.initialize(self.Wh, self.rng)
self.weights_init.initialize(self.Wi, self.rng)
self.weights_init.initialize(self.b, self.rng)
@recurrent(sequences=['inputs', 'time', 'mask'], states=['states'],
outputs=['states'], contexts=[])
def apply(self, inputs=None, time=None, states=None, mask=None):
"""Apply the simple transition.
Parameters
----------
inputs : :class:`~tensor.TensorVariable`
The 2D inputs, in the shape (batch, input_dim).
time :
The time index of the sequence the module gets as input.
states : :class:`~tensor.TensorVariable`
The 2D states, in the shape (batch, dim).
"""
actual_Wi = tensor.zeros((self.input_dim, self.dim))
actual_Wh = tensor.zeros((self.dim, self.dim))
actual_b = tensor.zeros((1, self.dim))
for i in range(self.module):
actual_Wi, actual_Wh, actual_b = ifelse(tensor.eq(time % self.periods[i], 0.0),
[tensor.set_subtensor(actual_Wi[:,i*self.unit:(i+1)*self.unit], self.Wi[:,i*self.unit:(i+1)*self.unit]),
tensor.set_subtensor(actual_Wh[i*self.unit:,i*self.unit:(i+1)*self.unit], self.Wh[i*self.unit:,i*self.unit:(i+1)*self.unit]),
tensor.set_subtensor(actual_b[:,i*self.unit:(i+1)*self.unit], self.b[:,i*self.unit:(i+1)*self.unit])
],
[actual_Wi, actual_Wh, actual_b]
)
batch = tensor.shape(states)[0]
one = tensor.ones((batch, 1))
actual_b = tensor.dot(one, actual_b)
next_states = tensor.dot(inputs, actual_Wi) + tensor.dot(states, actual_Wh) + actual_b
next_states = self.children[0].apply(next_states)
if mask:
next_states = (mask[:, None] * next_states + (1 - mask[:, None]) * states)
return next_states
class Bidirectional(Initializable):
"""Bidirectional network.
A bidirectional network is a combination of forward and backward
recurrent networks which process inputs in different order.
Parameters
----------
prototype : instance of :class:`BaseRecurrent`
A prototype brick from which the forward and backward bricks are
cloned.
Notes
-----
See :class:`.Initializable` for initialization parameters.
"""
has_bias = False
@lazy()
def __init__(self, prototype, **kwargs):
super(Bidirectional, self).__init__(**kwargs)
self.prototype = prototype
self.children = [copy.deepcopy(prototype) for _ in range(2)]
self.children[0].name = 'forward'
self.children[1].name = 'backward'
@application
def apply(self, *args, **kwargs):
"""Applies forward and backward networks and concatenates outputs."""
forward = self.children[0].apply(as_list=True, *args, **kwargs)
backward = [x[::-1] for x in
self.children[1].apply(reverse=True, as_list=True,
*args, **kwargs)]
return [tensor.concatenate([f, b], axis=2)
for f, b in equizip(forward, backward)]