keras 0.2.0__py3.5.egg
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- EGG-INFO/PKG-INFO +11 -0
- EGG-INFO/SOURCES.txt +57 -0
- EGG-INFO/dependency_links.txt +1 -0
- EGG-INFO/requires.txt +6 -0
- EGG-INFO/top_level.txt +2 -0
- EGG-INFO/zip-safe +1 -0
- keras/__init__.py +15 -0
- keras/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/__pycache__/activations.cpython-35.pyc +0 -0
- keras/__pycache__/callbacks.cpython-35.pyc +0 -0
- keras/__pycache__/constraints.cpython-35.pyc +0 -0
- keras/__pycache__/initializations.cpython-35.pyc +0 -0
- keras/__pycache__/models.cpython-35.pyc +0 -0
- keras/__pycache__/objectives.cpython-35.pyc +0 -0
- keras/__pycache__/optimizers.cpython-35.pyc +0 -0
- keras/__pycache__/regularizers.cpython-35.pyc +0 -0
- keras/activations.py +44 -0
- keras/callbacks.py +293 -0
- keras/constraints.py +48 -0
- keras/datasets/__init__.py +0 -0
- keras/datasets/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/cifar.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/cifar10.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/cifar100.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/data_utils.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/imdb.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/mnist.cpython-35.pyc +0 -0
- keras/datasets/__pycache__/reuters.cpython-35.pyc +0 -0
- keras/datasets/cifar.py +22 -0
- keras/datasets/cifar10.py +31 -0
- keras/datasets/cifar100.py +28 -0
- keras/datasets/data_utils.py +50 -0
- keras/datasets/imdb.py +66 -0
- keras/datasets/mnist.py +23 -0
- keras/datasets/reuters.py +148 -0
- keras/initializations.py +89 -0
- keras/layers/__init__.py +0 -0
- keras/layers/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/layers/__pycache__/advanced_activations.cpython-35.pyc +0 -0
- keras/layers/__pycache__/containers.cpython-35.pyc +0 -0
- keras/layers/__pycache__/convolutional.cpython-35.pyc +0 -0
- keras/layers/__pycache__/core.cpython-35.pyc +0 -0
- keras/layers/__pycache__/embeddings.cpython-35.pyc +0 -0
- keras/layers/__pycache__/noise.cpython-35.pyc +0 -0
- keras/layers/__pycache__/normalization.cpython-35.pyc +0 -0
- keras/layers/__pycache__/recurrent.cpython-35.pyc +0 -0
- keras/layers/advanced_activations.py +137 -0
- keras/layers/containers.py +318 -0
- keras/layers/convolutional.py +504 -0
- keras/layers/core.py +963 -0
- keras/layers/embeddings.py +153 -0
- keras/layers/noise.py +57 -0
- keras/layers/normalization.py +117 -0
- keras/layers/recurrent.py +837 -0
- keras/models.py +733 -0
- keras/objectives.py +67 -0
- keras/optimizers.py +228 -0
- keras/preprocessing/__init__.py +0 -0
- keras/preprocessing/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/preprocessing/__pycache__/image.cpython-35.pyc +0 -0
- keras/preprocessing/__pycache__/sequence.cpython-35.pyc +0 -0
- keras/preprocessing/__pycache__/text.cpython-35.pyc +0 -0
- keras/preprocessing/image.py +251 -0
- keras/preprocessing/sequence.py +124 -0
- keras/preprocessing/text.py +176 -0
- keras/regularizers.py +84 -0
- keras/utils/__init__.py +0 -0
- keras/utils/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/utils/__pycache__/generic_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/io_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/layer_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/np_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/test_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/theano_utils.cpython-35.pyc +0 -0
- keras/utils/__pycache__/visualize_util.cpython-35.pyc +0 -0
- keras/utils/generic_utils.py +133 -0
- keras/utils/io_utils.py +71 -0
- keras/utils/layer_utils.py +129 -0
- keras/utils/np_utils.py +53 -0
- keras/utils/test_utils.py +27 -0
- keras/utils/theano_utils.py +44 -0
- keras/utils/visualize_util.py +41 -0
- keras/wrappers/__init__.py +0 -0
- keras/wrappers/__pycache__/__init__.cpython-35.pyc +0 -0
- keras/wrappers/__pycache__/scikit_learn.cpython-35.pyc +0 -0
- keras/wrappers/scikit_learn.py +266 -0
- tests/__init__.py +0 -0
- tests/__pycache__/__init__.cpython-35.pyc +0 -0
- tests/manual/__init__.py +0 -0
- tests/manual/__pycache__/__init__.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_autoencoder.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_callbacks.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_constraints.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_masked_recurrent.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_model_utils.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_save_weights.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_wrappers.cpython-35.pyc +0 -0
- tests/manual/__pycache__/check_yaml.cpython-35.pyc +0 -0
- tests/manual/check_autoencoder.py +134 -0
- tests/manual/check_callbacks.py +236 -0
- tests/manual/check_constraints.py +100 -0
- tests/manual/check_masked_recurrent.py +130 -0
- tests/manual/check_model_utils.py +44 -0
- tests/manual/check_save_weights.py +40 -0
- tests/manual/check_wrappers.py +126 -0
- tests/manual/check_yaml.py +101 -0
|
@@ -0,0 +1,837 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from __future__ import absolute_import
|
|
3
|
+
import theano
|
|
4
|
+
import theano.tensor as T
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from .. import activations, initializations
|
|
8
|
+
from ..utils.theano_utils import shared_scalar, shared_zeros, alloc_zeros_matrix
|
|
9
|
+
from ..layers.core import Layer, MaskedLayer
|
|
10
|
+
from six.moves import range
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Recurrent(MaskedLayer):
|
|
14
|
+
input_ndim = 3
|
|
15
|
+
|
|
16
|
+
def get_output_mask(self, train=None):
|
|
17
|
+
if self.return_sequences:
|
|
18
|
+
return super(Recurrent, self).get_output_mask(train)
|
|
19
|
+
else:
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
def get_padded_shuffled_mask(self, train, X, pad=0):
|
|
23
|
+
mask = self.get_input_mask(train)
|
|
24
|
+
if mask is None:
|
|
25
|
+
mask = T.ones_like(X.sum(axis=-1)) # is there a better way to do this without a sum?
|
|
26
|
+
|
|
27
|
+
# mask is (nb_samples, time)
|
|
28
|
+
mask = T.shape_padright(mask) # (nb_samples, time, 1)
|
|
29
|
+
mask = T.addbroadcast(mask, -1) # the new dimension (the '1') is made broadcastable
|
|
30
|
+
# see http://deeplearning.net/software/theano/library/tensor/basic.html#broadcasting-in-theano-vs-numpy
|
|
31
|
+
mask = mask.dimshuffle(1, 0, 2) # (time, nb_samples, 1)
|
|
32
|
+
|
|
33
|
+
if pad > 0:
|
|
34
|
+
# left-pad in time with 0
|
|
35
|
+
padding = alloc_zeros_matrix(pad, mask.shape[1], 1)
|
|
36
|
+
mask = T.concatenate([padding, mask], axis=0)
|
|
37
|
+
return mask.astype('int8')
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def output_shape(self):
|
|
41
|
+
input_shape = self.input_shape
|
|
42
|
+
if self.return_sequences:
|
|
43
|
+
return (input_shape[0], input_shape[1], self.output_dim)
|
|
44
|
+
else:
|
|
45
|
+
return (input_shape[0], self.output_dim)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class SimpleRNN(Recurrent):
|
|
49
|
+
'''
|
|
50
|
+
Fully connected RNN where output is to fed back to input.
|
|
51
|
+
|
|
52
|
+
Not a particularly useful model,
|
|
53
|
+
included for demonstration purposes
|
|
54
|
+
(demonstrates how to use theano.scan to build a basic RNN).
|
|
55
|
+
'''
|
|
56
|
+
def __init__(self, output_dim,
|
|
57
|
+
init='glorot_uniform', inner_init='orthogonal', activation='sigmoid', weights=None,
|
|
58
|
+
truncate_gradient=-1, return_sequences=False, input_dim=None,
|
|
59
|
+
input_length=None, go_backwards=False, **kwargs):
|
|
60
|
+
self.output_dim = output_dim
|
|
61
|
+
self.init = initializations.get(init)
|
|
62
|
+
self.inner_init = initializations.get(inner_init)
|
|
63
|
+
self.truncate_gradient = truncate_gradient
|
|
64
|
+
self.activation = activations.get(activation)
|
|
65
|
+
self.return_sequences = return_sequences
|
|
66
|
+
self.initial_weights = weights
|
|
67
|
+
self.go_backwards = go_backwards
|
|
68
|
+
|
|
69
|
+
self.input_dim = input_dim
|
|
70
|
+
self.input_length = input_length
|
|
71
|
+
if self.input_dim:
|
|
72
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
73
|
+
super(SimpleRNN, self).__init__(**kwargs)
|
|
74
|
+
|
|
75
|
+
def build(self):
|
|
76
|
+
input_dim = self.input_shape[2]
|
|
77
|
+
self.input = T.tensor3()
|
|
78
|
+
|
|
79
|
+
self.W = self.init((input_dim, self.output_dim))
|
|
80
|
+
self.U = self.inner_init((self.output_dim, self.output_dim))
|
|
81
|
+
self.b = shared_zeros((self.output_dim))
|
|
82
|
+
self.params = [self.W, self.U, self.b]
|
|
83
|
+
|
|
84
|
+
if self.initial_weights is not None:
|
|
85
|
+
self.set_weights(self.initial_weights)
|
|
86
|
+
del self.initial_weights
|
|
87
|
+
|
|
88
|
+
def _step(self, x_t, mask_tm1, h_tm1, u):
|
|
89
|
+
'''
|
|
90
|
+
Variable names follow the conventions from:
|
|
91
|
+
http://deeplearning.net/software/theano/library/scan.html
|
|
92
|
+
|
|
93
|
+
'''
|
|
94
|
+
return self.activation(x_t + mask_tm1 * T.dot(h_tm1, u))
|
|
95
|
+
|
|
96
|
+
def get_output(self, train=False):
|
|
97
|
+
X = self.get_input(train) # shape: (nb_samples, time (padded with zeros), input_dim)
|
|
98
|
+
# new shape: (time, nb_samples, input_dim) -> because theano.scan iterates over main dimension
|
|
99
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
|
|
100
|
+
X = X.dimshuffle((1, 0, 2))
|
|
101
|
+
x = T.dot(X, self.W) + self.b
|
|
102
|
+
|
|
103
|
+
# scan = theano symbolic loop.
|
|
104
|
+
# See: http://deeplearning.net/software/theano/library/scan.html
|
|
105
|
+
# Iterate over the first dimension of the x array (=time).
|
|
106
|
+
outputs, updates = theano.scan(
|
|
107
|
+
self._step, # this will be called with arguments (sequences[i], outputs[i-1], non_sequences[i])
|
|
108
|
+
sequences=[x, dict(input=padded_mask, taps=[-1])], # tensors to iterate over, inputs to _step
|
|
109
|
+
# initialization of the output. Input to _step with default tap=-1.
|
|
110
|
+
outputs_info=T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
|
|
111
|
+
non_sequences=self.U, # static inputs to _step
|
|
112
|
+
truncate_gradient=self.truncate_gradient,
|
|
113
|
+
go_backwards=self.go_backwards)
|
|
114
|
+
|
|
115
|
+
if self.return_sequences:
|
|
116
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
117
|
+
return outputs[-1]
|
|
118
|
+
|
|
119
|
+
def get_config(self):
|
|
120
|
+
config = {"name": self.__class__.__name__,
|
|
121
|
+
"output_dim": self.output_dim,
|
|
122
|
+
"init": self.init.__name__,
|
|
123
|
+
"inner_init": self.inner_init.__name__,
|
|
124
|
+
"activation": self.activation.__name__,
|
|
125
|
+
"truncate_gradient": self.truncate_gradient,
|
|
126
|
+
"return_sequences": self.return_sequences,
|
|
127
|
+
"input_dim": self.input_dim,
|
|
128
|
+
"input_length": self.input_length,
|
|
129
|
+
"go_backwards": self.go_backwards}
|
|
130
|
+
base_config = super(SimpleRNN, self).get_config()
|
|
131
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class SimpleDeepRNN(Recurrent):
|
|
135
|
+
'''
|
|
136
|
+
Fully connected RNN where the output of multiple timesteps
|
|
137
|
+
(up to "depth" steps in the past) is fed back to the input:
|
|
138
|
+
|
|
139
|
+
output = activation( W.x_t + b + inner_activation(U_1.h_tm1) + inner_activation(U_2.h_tm2) + ... )
|
|
140
|
+
|
|
141
|
+
This demonstrates how to build RNNs with arbitrary lookback.
|
|
142
|
+
Also (probably) not a super useful model.
|
|
143
|
+
'''
|
|
144
|
+
def __init__(self, output_dim, depth=3,
|
|
145
|
+
init='glorot_uniform', inner_init='orthogonal',
|
|
146
|
+
activation='sigmoid', inner_activation='hard_sigmoid',
|
|
147
|
+
weights=None, truncate_gradient=-1, return_sequences=False,
|
|
148
|
+
input_dim=None, input_length=None, go_backwards=False, **kwargs):
|
|
149
|
+
self.output_dim = output_dim
|
|
150
|
+
self.init = initializations.get(init)
|
|
151
|
+
self.inner_init = initializations.get(inner_init)
|
|
152
|
+
self.truncate_gradient = truncate_gradient
|
|
153
|
+
self.activation = activations.get(activation)
|
|
154
|
+
self.inner_activation = activations.get(inner_activation)
|
|
155
|
+
self.depth = depth
|
|
156
|
+
self.return_sequences = return_sequences
|
|
157
|
+
self.initial_weights = weights
|
|
158
|
+
self.go_backwards = go_backwards
|
|
159
|
+
|
|
160
|
+
self.input_dim = input_dim
|
|
161
|
+
self.input_length = input_length
|
|
162
|
+
if self.input_dim:
|
|
163
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
164
|
+
super(SimpleDeepRNN, self).__init__(**kwargs)
|
|
165
|
+
|
|
166
|
+
def build(self):
|
|
167
|
+
input_dim = self.input_shape[2]
|
|
168
|
+
self.input = T.tensor3()
|
|
169
|
+
self.W = self.init((input_dim, self.output_dim))
|
|
170
|
+
self.Us = [self.inner_init((self.output_dim, self.output_dim)) for _ in range(self.depth)]
|
|
171
|
+
self.b = shared_zeros((self.output_dim))
|
|
172
|
+
self.params = [self.W] + self.Us + [self.b]
|
|
173
|
+
|
|
174
|
+
if self.initial_weights is not None:
|
|
175
|
+
self.set_weights(self.initial_weights)
|
|
176
|
+
del self.initial_weights
|
|
177
|
+
|
|
178
|
+
def _step(self, x_t, *args):
|
|
179
|
+
o = x_t
|
|
180
|
+
for i in range(self.depth):
|
|
181
|
+
mask_tmi = args[i]
|
|
182
|
+
h_tmi = args[i + self.depth]
|
|
183
|
+
U_tmi = args[i + 2*self.depth]
|
|
184
|
+
o += mask_tmi*self.inner_activation(T.dot(h_tmi, U_tmi))
|
|
185
|
+
return self.activation(o)
|
|
186
|
+
|
|
187
|
+
def get_output(self, train=False):
|
|
188
|
+
X = self.get_input(train)
|
|
189
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=self.depth)
|
|
190
|
+
X = X.dimshuffle((1, 0, 2))
|
|
191
|
+
|
|
192
|
+
x = T.dot(X, self.W) + self.b
|
|
193
|
+
|
|
194
|
+
if self.depth == 1:
|
|
195
|
+
initial = T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1)
|
|
196
|
+
else:
|
|
197
|
+
initial = T.unbroadcast(T.unbroadcast(alloc_zeros_matrix(self.depth, X.shape[1], self.output_dim), 0), 2)
|
|
198
|
+
|
|
199
|
+
outputs, updates = theano.scan(
|
|
200
|
+
self._step,
|
|
201
|
+
sequences=[x, dict(
|
|
202
|
+
input=padded_mask,
|
|
203
|
+
taps=[(-i) for i in range(self.depth)]
|
|
204
|
+
)],
|
|
205
|
+
outputs_info=[dict(
|
|
206
|
+
initial=initial,
|
|
207
|
+
taps=[(-i-1) for i in range(self.depth)]
|
|
208
|
+
)],
|
|
209
|
+
non_sequences=self.Us,
|
|
210
|
+
truncate_gradient=self.truncate_gradient,
|
|
211
|
+
go_backwards=self.go_backwards)
|
|
212
|
+
|
|
213
|
+
if self.return_sequences:
|
|
214
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
215
|
+
return outputs[-1]
|
|
216
|
+
|
|
217
|
+
def get_config(self):
|
|
218
|
+
config = {"name": self.__class__.__name__,
|
|
219
|
+
"output_dim": self.output_dim,
|
|
220
|
+
"depth": self.depth,
|
|
221
|
+
"init": self.init.__name__,
|
|
222
|
+
"inner_init": self.inner_init.__name__,
|
|
223
|
+
"activation": self.activation.__name__,
|
|
224
|
+
"inner_activation": self.inner_activation.__name__,
|
|
225
|
+
"truncate_gradient": self.truncate_gradient,
|
|
226
|
+
"return_sequences": self.return_sequences,
|
|
227
|
+
"input_dim": self.input_dim,
|
|
228
|
+
"input_length": self.input_length,
|
|
229
|
+
"go_backwards": self.go_backwards}
|
|
230
|
+
base_config = super(SimpleDeepRNN, self).get_config()
|
|
231
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
class GRU(Recurrent):
|
|
235
|
+
'''
|
|
236
|
+
Gated Recurrent Unit - Cho et al. 2014
|
|
237
|
+
|
|
238
|
+
Acts as a spatiotemporal projection,
|
|
239
|
+
turning a sequence of vectors into a single vector.
|
|
240
|
+
|
|
241
|
+
Eats inputs with shape:
|
|
242
|
+
(nb_samples, max_sample_length (samples shorter than this are padded with zeros at the end), input_dim)
|
|
243
|
+
|
|
244
|
+
and returns outputs with shape:
|
|
245
|
+
if not return_sequences:
|
|
246
|
+
(nb_samples, output_dim)
|
|
247
|
+
if return_sequences:
|
|
248
|
+
(nb_samples, max_sample_length, output_dim)
|
|
249
|
+
|
|
250
|
+
References:
|
|
251
|
+
On the Properties of Neural Machine Translation: Encoder–Decoder Approaches
|
|
252
|
+
http://www.aclweb.org/anthology/W14-4012
|
|
253
|
+
Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling
|
|
254
|
+
http://arxiv.org/pdf/1412.3555v1.pdf
|
|
255
|
+
'''
|
|
256
|
+
def __init__(self, output_dim,
|
|
257
|
+
init='glorot_uniform', inner_init='orthogonal',
|
|
258
|
+
activation='sigmoid', inner_activation='hard_sigmoid',
|
|
259
|
+
weights=None, truncate_gradient=-1, return_sequences=False,
|
|
260
|
+
input_dim=None, input_length=None, go_backwards=False, **kwargs):
|
|
261
|
+
self.output_dim = output_dim
|
|
262
|
+
self.init = initializations.get(init)
|
|
263
|
+
self.inner_init = initializations.get(inner_init)
|
|
264
|
+
self.activation = activations.get(activation)
|
|
265
|
+
self.inner_activation = activations.get(inner_activation)
|
|
266
|
+
self.truncate_gradient = truncate_gradient
|
|
267
|
+
self.return_sequences = return_sequences
|
|
268
|
+
self.initial_weights = weights
|
|
269
|
+
self.go_backwards = go_backwards
|
|
270
|
+
|
|
271
|
+
self.input_dim = input_dim
|
|
272
|
+
self.input_length = input_length
|
|
273
|
+
if self.input_dim:
|
|
274
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
275
|
+
super(GRU, self).__init__(**kwargs)
|
|
276
|
+
|
|
277
|
+
def build(self):
|
|
278
|
+
input_dim = self.input_shape[2]
|
|
279
|
+
self.input = T.tensor3()
|
|
280
|
+
|
|
281
|
+
self.W_z = self.init((input_dim, self.output_dim))
|
|
282
|
+
self.U_z = self.inner_init((self.output_dim, self.output_dim))
|
|
283
|
+
self.b_z = shared_zeros((self.output_dim))
|
|
284
|
+
|
|
285
|
+
self.W_r = self.init((input_dim, self.output_dim))
|
|
286
|
+
self.U_r = self.inner_init((self.output_dim, self.output_dim))
|
|
287
|
+
self.b_r = shared_zeros((self.output_dim))
|
|
288
|
+
|
|
289
|
+
self.W_h = self.init((input_dim, self.output_dim))
|
|
290
|
+
self.U_h = self.inner_init((self.output_dim, self.output_dim))
|
|
291
|
+
self.b_h = shared_zeros((self.output_dim))
|
|
292
|
+
|
|
293
|
+
self.params = [
|
|
294
|
+
self.W_z, self.U_z, self.b_z,
|
|
295
|
+
self.W_r, self.U_r, self.b_r,
|
|
296
|
+
self.W_h, self.U_h, self.b_h,
|
|
297
|
+
]
|
|
298
|
+
|
|
299
|
+
if self.initial_weights is not None:
|
|
300
|
+
self.set_weights(self.initial_weights)
|
|
301
|
+
del self.initial_weights
|
|
302
|
+
|
|
303
|
+
def _step(self,
|
|
304
|
+
xz_t, xr_t, xh_t, mask_tm1,
|
|
305
|
+
h_tm1,
|
|
306
|
+
u_z, u_r, u_h):
|
|
307
|
+
h_mask_tm1 = mask_tm1 * h_tm1
|
|
308
|
+
z = self.inner_activation(xz_t + T.dot(h_mask_tm1, u_z))
|
|
309
|
+
r = self.inner_activation(xr_t + T.dot(h_mask_tm1, u_r))
|
|
310
|
+
hh_t = self.activation(xh_t + T.dot(r * h_mask_tm1, u_h))
|
|
311
|
+
h_t = z * h_mask_tm1 + (1 - z) * hh_t
|
|
312
|
+
return h_t
|
|
313
|
+
|
|
314
|
+
def get_output(self, train=False):
|
|
315
|
+
X = self.get_input(train)
|
|
316
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
|
|
317
|
+
X = X.dimshuffle((1, 0, 2))
|
|
318
|
+
|
|
319
|
+
x_z = T.dot(X, self.W_z) + self.b_z
|
|
320
|
+
x_r = T.dot(X, self.W_r) + self.b_r
|
|
321
|
+
x_h = T.dot(X, self.W_h) + self.b_h
|
|
322
|
+
outputs, updates = theano.scan(
|
|
323
|
+
self._step,
|
|
324
|
+
sequences=[x_z, x_r, x_h, padded_mask],
|
|
325
|
+
outputs_info=T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
|
|
326
|
+
non_sequences=[self.U_z, self.U_r, self.U_h],
|
|
327
|
+
truncate_gradient=self.truncate_gradient,
|
|
328
|
+
go_backwards=self.go_backwards)
|
|
329
|
+
|
|
330
|
+
if self.return_sequences:
|
|
331
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
332
|
+
return outputs[-1]
|
|
333
|
+
|
|
334
|
+
def get_config(self):
|
|
335
|
+
config = {"name": self.__class__.__name__,
|
|
336
|
+
"output_dim": self.output_dim,
|
|
337
|
+
"init": self.init.__name__,
|
|
338
|
+
"inner_init": self.inner_init.__name__,
|
|
339
|
+
"activation": self.activation.__name__,
|
|
340
|
+
"inner_activation": self.inner_activation.__name__,
|
|
341
|
+
"truncate_gradient": self.truncate_gradient,
|
|
342
|
+
"return_sequences": self.return_sequences,
|
|
343
|
+
"input_dim": self.input_dim,
|
|
344
|
+
"input_length": self.input_length,
|
|
345
|
+
"go_backwards": self.go_backwards}
|
|
346
|
+
base_config = super(GRU, self).get_config()
|
|
347
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
class LSTM(Recurrent):
|
|
351
|
+
'''
|
|
352
|
+
Acts as a spatiotemporal projection,
|
|
353
|
+
turning a sequence of vectors into a single vector.
|
|
354
|
+
|
|
355
|
+
Eats inputs with shape:
|
|
356
|
+
(nb_samples, max_sample_length (samples shorter than this are padded with zeros at the end), input_dim)
|
|
357
|
+
|
|
358
|
+
and returns outputs with shape:
|
|
359
|
+
if not return_sequences:
|
|
360
|
+
(nb_samples, output_dim)
|
|
361
|
+
if return_sequences:
|
|
362
|
+
(nb_samples, max_sample_length, output_dim)
|
|
363
|
+
|
|
364
|
+
For a step-by-step description of the algorithm, see:
|
|
365
|
+
http://deeplearning.net/tutorial/lstm.html
|
|
366
|
+
|
|
367
|
+
References:
|
|
368
|
+
Long short-term memory (original 97 paper)
|
|
369
|
+
http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf
|
|
370
|
+
Learning to forget: Continual prediction with LSTM
|
|
371
|
+
http://www.mitpressjournals.org/doi/pdf/10.1162/089976600300015015
|
|
372
|
+
Supervised sequence labelling with recurrent neural networks
|
|
373
|
+
http://www.cs.toronto.edu/~graves/preprint.pdf
|
|
374
|
+
'''
|
|
375
|
+
def __init__(self, output_dim,
|
|
376
|
+
init='glorot_uniform', inner_init='orthogonal', forget_bias_init='one',
|
|
377
|
+
activation='tanh', inner_activation='hard_sigmoid',
|
|
378
|
+
weights=None, truncate_gradient=-1, return_sequences=False,
|
|
379
|
+
input_dim=None, input_length=None, go_backwards=False, **kwargs):
|
|
380
|
+
self.output_dim = output_dim
|
|
381
|
+
self.init = initializations.get(init)
|
|
382
|
+
self.inner_init = initializations.get(inner_init)
|
|
383
|
+
self.forget_bias_init = initializations.get(forget_bias_init)
|
|
384
|
+
self.activation = activations.get(activation)
|
|
385
|
+
self.inner_activation = activations.get(inner_activation)
|
|
386
|
+
self.truncate_gradient = truncate_gradient
|
|
387
|
+
self.return_sequences = return_sequences
|
|
388
|
+
self.initial_weights = weights
|
|
389
|
+
self.go_backwards = go_backwards
|
|
390
|
+
|
|
391
|
+
self.input_dim = input_dim
|
|
392
|
+
self.input_length = input_length
|
|
393
|
+
if self.input_dim:
|
|
394
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
395
|
+
super(LSTM, self).__init__(**kwargs)
|
|
396
|
+
|
|
397
|
+
def build(self):
|
|
398
|
+
input_dim = self.input_shape[2]
|
|
399
|
+
self.input = T.tensor3()
|
|
400
|
+
|
|
401
|
+
self.W_i = self.init((input_dim, self.output_dim))
|
|
402
|
+
self.U_i = self.inner_init((self.output_dim, self.output_dim))
|
|
403
|
+
self.b_i = shared_zeros((self.output_dim))
|
|
404
|
+
|
|
405
|
+
self.W_f = self.init((input_dim, self.output_dim))
|
|
406
|
+
self.U_f = self.inner_init((self.output_dim, self.output_dim))
|
|
407
|
+
self.b_f = self.forget_bias_init((self.output_dim))
|
|
408
|
+
|
|
409
|
+
self.W_c = self.init((input_dim, self.output_dim))
|
|
410
|
+
self.U_c = self.inner_init((self.output_dim, self.output_dim))
|
|
411
|
+
self.b_c = shared_zeros((self.output_dim))
|
|
412
|
+
|
|
413
|
+
self.W_o = self.init((input_dim, self.output_dim))
|
|
414
|
+
self.U_o = self.inner_init((self.output_dim, self.output_dim))
|
|
415
|
+
self.b_o = shared_zeros((self.output_dim))
|
|
416
|
+
|
|
417
|
+
self.params = [
|
|
418
|
+
self.W_i, self.U_i, self.b_i,
|
|
419
|
+
self.W_c, self.U_c, self.b_c,
|
|
420
|
+
self.W_f, self.U_f, self.b_f,
|
|
421
|
+
self.W_o, self.U_o, self.b_o,
|
|
422
|
+
]
|
|
423
|
+
|
|
424
|
+
if self.initial_weights is not None:
|
|
425
|
+
self.set_weights(self.initial_weights)
|
|
426
|
+
del self.initial_weights
|
|
427
|
+
|
|
428
|
+
def _step(self,
|
|
429
|
+
xi_t, xf_t, xo_t, xc_t, mask_tm1,
|
|
430
|
+
h_tm1, c_tm1,
|
|
431
|
+
u_i, u_f, u_o, u_c):
|
|
432
|
+
h_mask_tm1 = mask_tm1 * h_tm1
|
|
433
|
+
c_mask_tm1 = mask_tm1 * c_tm1
|
|
434
|
+
|
|
435
|
+
i_t = self.inner_activation(xi_t + T.dot(h_mask_tm1, u_i))
|
|
436
|
+
f_t = self.inner_activation(xf_t + T.dot(h_mask_tm1, u_f))
|
|
437
|
+
c_t = f_t * c_mask_tm1 + i_t * self.activation(xc_t + T.dot(h_mask_tm1, u_c))
|
|
438
|
+
o_t = self.inner_activation(xo_t + T.dot(h_mask_tm1, u_o))
|
|
439
|
+
h_t = o_t * self.activation(c_t)
|
|
440
|
+
return h_t, c_t
|
|
441
|
+
|
|
442
|
+
def get_output(self, train=False):
|
|
443
|
+
X = self.get_input(train)
|
|
444
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
|
|
445
|
+
X = X.dimshuffle((1, 0, 2))
|
|
446
|
+
|
|
447
|
+
xi = T.dot(X, self.W_i) + self.b_i
|
|
448
|
+
xf = T.dot(X, self.W_f) + self.b_f
|
|
449
|
+
xc = T.dot(X, self.W_c) + self.b_c
|
|
450
|
+
xo = T.dot(X, self.W_o) + self.b_o
|
|
451
|
+
|
|
452
|
+
[outputs, memories], updates = theano.scan(
|
|
453
|
+
self._step,
|
|
454
|
+
sequences=[xi, xf, xo, xc, padded_mask],
|
|
455
|
+
outputs_info=[
|
|
456
|
+
T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
|
|
457
|
+
T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1)
|
|
458
|
+
],
|
|
459
|
+
non_sequences=[self.U_i, self.U_f, self.U_o, self.U_c],
|
|
460
|
+
truncate_gradient=self.truncate_gradient,
|
|
461
|
+
go_backwards=self.go_backwards)
|
|
462
|
+
|
|
463
|
+
if self.return_sequences:
|
|
464
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
465
|
+
return outputs[-1]
|
|
466
|
+
|
|
467
|
+
def get_config(self):
|
|
468
|
+
config = {"name": self.__class__.__name__,
|
|
469
|
+
"output_dim": self.output_dim,
|
|
470
|
+
"init": self.init.__name__,
|
|
471
|
+
"inner_init": self.inner_init.__name__,
|
|
472
|
+
"forget_bias_init": self.forget_bias_init.__name__,
|
|
473
|
+
"activation": self.activation.__name__,
|
|
474
|
+
"inner_activation": self.inner_activation.__name__,
|
|
475
|
+
"truncate_gradient": self.truncate_gradient,
|
|
476
|
+
"return_sequences": self.return_sequences,
|
|
477
|
+
"input_dim": self.input_dim,
|
|
478
|
+
"input_length": self.input_length,
|
|
479
|
+
"go_backwards": self.go_backwards}
|
|
480
|
+
base_config = super(LSTM, self).get_config()
|
|
481
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
class JZS1(Recurrent):
|
|
485
|
+
'''
|
|
486
|
+
Evolved recurrent neural network architectures from the evaluation of thousands
|
|
487
|
+
of models, serving as alternatives to LSTMs and GRUs. See Jozefowicz et al. 2015.
|
|
488
|
+
|
|
489
|
+
This corresponds to the `MUT1` architecture described in the paper.
|
|
490
|
+
|
|
491
|
+
Takes inputs with shape:
|
|
492
|
+
(nb_samples, max_sample_length (samples shorter than this are padded with zeros at the end), input_dim)
|
|
493
|
+
|
|
494
|
+
and returns outputs with shape:
|
|
495
|
+
if not return_sequences:
|
|
496
|
+
(nb_samples, output_dim)
|
|
497
|
+
if return_sequences:
|
|
498
|
+
(nb_samples, max_sample_length, output_dim)
|
|
499
|
+
|
|
500
|
+
References:
|
|
501
|
+
An Empirical Exploration of Recurrent Network Architectures
|
|
502
|
+
http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf
|
|
503
|
+
'''
|
|
504
|
+
def __init__(self, output_dim,
|
|
505
|
+
init='glorot_uniform', inner_init='orthogonal',
|
|
506
|
+
activation='tanh', inner_activation='sigmoid',
|
|
507
|
+
weights=None, truncate_gradient=-1, return_sequences=False,
|
|
508
|
+
input_dim=None, input_length=None, go_backwards=False, **kwargs):
|
|
509
|
+
self.output_dim = output_dim
|
|
510
|
+
self.init = initializations.get(init)
|
|
511
|
+
self.inner_init = initializations.get(inner_init)
|
|
512
|
+
self.activation = activations.get(activation)
|
|
513
|
+
self.inner_activation = activations.get(inner_activation)
|
|
514
|
+
self.truncate_gradient = truncate_gradient
|
|
515
|
+
self.return_sequences = return_sequences
|
|
516
|
+
self.initial_weights = weights
|
|
517
|
+
self.go_backwards = go_backwards
|
|
518
|
+
|
|
519
|
+
self.input_dim = input_dim
|
|
520
|
+
self.input_length = input_length
|
|
521
|
+
if self.input_dim:
|
|
522
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
523
|
+
super(JZS1, self).__init__(**kwargs)
|
|
524
|
+
|
|
525
|
+
def build(self):
|
|
526
|
+
input_dim = self.input_shape[2]
|
|
527
|
+
self.input = T.tensor3()
|
|
528
|
+
|
|
529
|
+
self.W_z = self.init((input_dim, self.output_dim))
|
|
530
|
+
self.b_z = shared_zeros((self.output_dim))
|
|
531
|
+
|
|
532
|
+
self.W_r = self.init((input_dim, self.output_dim))
|
|
533
|
+
self.U_r = self.inner_init((self.output_dim, self.output_dim))
|
|
534
|
+
self.b_r = shared_zeros((self.output_dim))
|
|
535
|
+
|
|
536
|
+
self.U_h = self.inner_init((self.output_dim, self.output_dim))
|
|
537
|
+
self.b_h = shared_zeros((self.output_dim))
|
|
538
|
+
|
|
539
|
+
# P_h used to project X onto different dimension, using sparse random projections
|
|
540
|
+
if input_dim == self.output_dim:
|
|
541
|
+
self.Pmat = theano.shared(np.identity(self.output_dim, dtype=theano.config.floatX), name=None)
|
|
542
|
+
else:
|
|
543
|
+
P = np.random.binomial(1, 0.5, size=(input_dim, self.output_dim)).astype(theano.config.floatX) * 2 - 1
|
|
544
|
+
P = 1 / np.sqrt(input_dim) * P
|
|
545
|
+
self.Pmat = theano.shared(P, name=None)
|
|
546
|
+
|
|
547
|
+
self.params = [
|
|
548
|
+
self.W_z, self.b_z,
|
|
549
|
+
self.W_r, self.U_r, self.b_r,
|
|
550
|
+
self.U_h, self.b_h,
|
|
551
|
+
self.Pmat
|
|
552
|
+
]
|
|
553
|
+
|
|
554
|
+
if self.initial_weights is not None:
|
|
555
|
+
self.set_weights(self.initial_weights)
|
|
556
|
+
del self.initial_weights
|
|
557
|
+
|
|
558
|
+
def _step(self,
|
|
559
|
+
xz_t, xr_t, xh_t, mask_tm1,
|
|
560
|
+
h_tm1,
|
|
561
|
+
u_r, u_h):
|
|
562
|
+
h_mask_tm1 = mask_tm1 * h_tm1
|
|
563
|
+
z = self.inner_activation(xz_t)
|
|
564
|
+
r = self.inner_activation(xr_t + T.dot(h_mask_tm1, u_r))
|
|
565
|
+
hh_t = self.activation(xh_t + T.dot(r * h_mask_tm1, u_h))
|
|
566
|
+
h_t = hh_t * z + h_mask_tm1 * (1 - z)
|
|
567
|
+
return h_t
|
|
568
|
+
|
|
569
|
+
def get_output(self, train=False):
|
|
570
|
+
X = self.get_input(train)
|
|
571
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
|
|
572
|
+
X = X.dimshuffle((1, 0, 2))
|
|
573
|
+
|
|
574
|
+
x_z = T.dot(X, self.W_z) + self.b_z
|
|
575
|
+
x_r = T.dot(X, self.W_r) + self.b_r
|
|
576
|
+
x_h = T.tanh(T.dot(X, self.Pmat)) + self.b_h
|
|
577
|
+
outputs, updates = theano.scan(
|
|
578
|
+
self._step,
|
|
579
|
+
sequences=[x_z, x_r, x_h, padded_mask],
|
|
580
|
+
outputs_info=T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
|
|
581
|
+
non_sequences=[self.U_r, self.U_h],
|
|
582
|
+
truncate_gradient=self.truncate_gradient,
|
|
583
|
+
go_backwards=self.go_backwards)
|
|
584
|
+
if self.return_sequences:
|
|
585
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
586
|
+
return outputs[-1]
|
|
587
|
+
|
|
588
|
+
def get_config(self):
|
|
589
|
+
config = {"name": self.__class__.__name__,
|
|
590
|
+
"output_dim": self.output_dim,
|
|
591
|
+
"init": self.init.__name__,
|
|
592
|
+
"inner_init": self.inner_init.__name__,
|
|
593
|
+
"activation": self.activation.__name__,
|
|
594
|
+
"inner_activation": self.inner_activation.__name__,
|
|
595
|
+
"truncate_gradient": self.truncate_gradient,
|
|
596
|
+
"return_sequences": self.return_sequences,
|
|
597
|
+
"input_dim": self.input_dim,
|
|
598
|
+
"input_length": self.input_length,
|
|
599
|
+
"go_backwards": self.go_backwards}
|
|
600
|
+
base_config = super(JZS1, self).get_config()
|
|
601
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
class JZS2(Recurrent):
|
|
605
|
+
'''
|
|
606
|
+
Evolved recurrent neural network architectures from the evaluation of thousands
|
|
607
|
+
of models, serving as alternatives to LSTMs and GRUs. See Jozefowicz et al. 2015.
|
|
608
|
+
|
|
609
|
+
This corresponds to the `MUT2` architecture described in the paper.
|
|
610
|
+
|
|
611
|
+
Takes inputs with shape:
|
|
612
|
+
(nb_samples, max_sample_length (samples shorter than this are padded with zeros at the end), input_dim)
|
|
613
|
+
|
|
614
|
+
and returns outputs with shape:
|
|
615
|
+
if not return_sequences:
|
|
616
|
+
(nb_samples, output_dim)
|
|
617
|
+
if return_sequences:
|
|
618
|
+
(nb_samples, max_sample_length, output_dim)
|
|
619
|
+
|
|
620
|
+
References:
|
|
621
|
+
An Empirical Exploration of Recurrent Network Architectures
|
|
622
|
+
http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf
|
|
623
|
+
'''
|
|
624
|
+
def __init__(self, output_dim,
|
|
625
|
+
init='glorot_uniform', inner_init='orthogonal',
|
|
626
|
+
activation='tanh', inner_activation='sigmoid',
|
|
627
|
+
weights=None, truncate_gradient=-1, return_sequences=False,
|
|
628
|
+
input_dim=None, input_length=None, go_backwards=False, **kwargs):
|
|
629
|
+
self.output_dim = output_dim
|
|
630
|
+
self.init = initializations.get(init)
|
|
631
|
+
self.inner_init = initializations.get(inner_init)
|
|
632
|
+
self.activation = activations.get(activation)
|
|
633
|
+
self.inner_activation = activations.get(inner_activation)
|
|
634
|
+
self.truncate_gradient = truncate_gradient
|
|
635
|
+
self.return_sequences = return_sequences
|
|
636
|
+
self.initial_weights = weights
|
|
637
|
+
self.go_backwards = go_backwards
|
|
638
|
+
|
|
639
|
+
self.input_dim = input_dim
|
|
640
|
+
self.input_length = input_length
|
|
641
|
+
if self.input_dim:
|
|
642
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
643
|
+
super(JZS2, self).__init__(**kwargs)
|
|
644
|
+
|
|
645
|
+
def build(self):
|
|
646
|
+
input_dim = self.input_shape[2]
|
|
647
|
+
self.input = T.tensor3()
|
|
648
|
+
|
|
649
|
+
self.W_z = self.init((input_dim, self.output_dim))
|
|
650
|
+
self.U_z = self.inner_init((self.output_dim, self.output_dim))
|
|
651
|
+
self.b_z = shared_zeros((self.output_dim))
|
|
652
|
+
|
|
653
|
+
self.U_r = self.inner_init((self.output_dim, self.output_dim))
|
|
654
|
+
self.b_r = shared_zeros((self.output_dim))
|
|
655
|
+
|
|
656
|
+
self.W_h = self.init((input_dim, self.output_dim))
|
|
657
|
+
self.U_h = self.inner_init((self.output_dim, self.output_dim))
|
|
658
|
+
self.b_h = shared_zeros((self.output_dim))
|
|
659
|
+
|
|
660
|
+
# P_h used to project X onto different dimension, using sparse random projections
|
|
661
|
+
if input_dim == self.output_dim:
|
|
662
|
+
self.Pmat = theano.shared(np.identity(self.output_dim, dtype=theano.config.floatX), name=None)
|
|
663
|
+
else:
|
|
664
|
+
P = np.random.binomial(1, 0.5, size=(input_dim, self.output_dim)).astype(theano.config.floatX) * 2 - 1
|
|
665
|
+
P = 1 / np.sqrt(input_dim) * P
|
|
666
|
+
self.Pmat = theano.shared(P, name=None)
|
|
667
|
+
|
|
668
|
+
self.params = [
|
|
669
|
+
self.W_z, self.U_z, self.b_z,
|
|
670
|
+
self.U_r, self.b_r,
|
|
671
|
+
self.W_h, self.U_h, self.b_h,
|
|
672
|
+
self.Pmat
|
|
673
|
+
]
|
|
674
|
+
|
|
675
|
+
if self.initial_weights is not None:
|
|
676
|
+
self.set_weights(self.initial_weights)
|
|
677
|
+
del self.initial_weights
|
|
678
|
+
|
|
679
|
+
def _step(self,
|
|
680
|
+
xz_t, xr_t, xh_t, mask_tm1,
|
|
681
|
+
h_tm1,
|
|
682
|
+
u_z, u_r, u_h):
|
|
683
|
+
h_mask_tm1 = mask_tm1 * h_tm1
|
|
684
|
+
z = self.inner_activation(xz_t + T.dot(h_mask_tm1, u_z))
|
|
685
|
+
r = self.inner_activation(xr_t + T.dot(h_mask_tm1, u_r))
|
|
686
|
+
hh_t = self.activation(xh_t + T.dot(r * h_mask_tm1, u_h))
|
|
687
|
+
h_t = hh_t * z + h_mask_tm1 * (1 - z)
|
|
688
|
+
return h_t
|
|
689
|
+
|
|
690
|
+
def get_output(self, train=False):
|
|
691
|
+
X = self.get_input(train)
|
|
692
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
|
|
693
|
+
X = X.dimshuffle((1, 0, 2))
|
|
694
|
+
|
|
695
|
+
x_z = T.dot(X, self.W_z) + self.b_z
|
|
696
|
+
x_r = T.dot(X, self.Pmat) + self.b_r
|
|
697
|
+
x_h = T.dot(X, self.W_h) + self.b_h
|
|
698
|
+
outputs, updates = theano.scan(
|
|
699
|
+
self._step,
|
|
700
|
+
sequences=[x_z, x_r, x_h, padded_mask],
|
|
701
|
+
outputs_info=T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
|
|
702
|
+
non_sequences=[self.U_z, self.U_r, self.U_h],
|
|
703
|
+
truncate_gradient=self.truncate_gradient,
|
|
704
|
+
go_backwards=self.go_backwards)
|
|
705
|
+
|
|
706
|
+
if self.return_sequences:
|
|
707
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
708
|
+
return outputs[-1]
|
|
709
|
+
|
|
710
|
+
def get_config(self):
|
|
711
|
+
config = {"name": self.__class__.__name__,
|
|
712
|
+
"output_dim": self.output_dim,
|
|
713
|
+
"init": self.init.__name__,
|
|
714
|
+
"inner_init": self.inner_init.__name__,
|
|
715
|
+
"activation": self.activation.__name__,
|
|
716
|
+
"inner_activation": self.inner_activation.__name__,
|
|
717
|
+
"truncate_gradient": self.truncate_gradient,
|
|
718
|
+
"return_sequences": self.return_sequences,
|
|
719
|
+
"input_dim": self.input_dim,
|
|
720
|
+
"input_length": self.input_length,
|
|
721
|
+
"go_backwards": self.go_backwards}
|
|
722
|
+
base_config = super(JZS2, self).get_config()
|
|
723
|
+
return dict(list(base_config.items()) + list(config.items()))
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
class JZS3(Recurrent):
|
|
727
|
+
'''
|
|
728
|
+
Evolved recurrent neural network architectures from the evaluation of thousands
|
|
729
|
+
of models, serving as alternatives to LSTMs and GRUs. See Jozefowicz et al. 2015.
|
|
730
|
+
|
|
731
|
+
This corresponds to the `MUT3` architecture described in the paper.
|
|
732
|
+
|
|
733
|
+
Takes inputs with shape:
|
|
734
|
+
(nb_samples, max_sample_length (samples shorter than this are padded with zeros at the end), input_dim)
|
|
735
|
+
|
|
736
|
+
and returns outputs with shape:
|
|
737
|
+
if not return_sequences:
|
|
738
|
+
(nb_samples, output_dim)
|
|
739
|
+
if return_sequences:
|
|
740
|
+
(nb_samples, max_sample_length, output_dim)
|
|
741
|
+
|
|
742
|
+
References:
|
|
743
|
+
An Empirical Exploration of Recurrent Network Architectures
|
|
744
|
+
http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf
|
|
745
|
+
'''
|
|
746
|
+
def __init__(self, output_dim,
|
|
747
|
+
init='glorot_uniform', inner_init='orthogonal',
|
|
748
|
+
activation='tanh', inner_activation='sigmoid',
|
|
749
|
+
weights=None, truncate_gradient=-1, return_sequences=False,
|
|
750
|
+
input_dim=None, input_length=None, go_backwards=False, **kwargs):
|
|
751
|
+
self.output_dim = output_dim
|
|
752
|
+
self.init = initializations.get(init)
|
|
753
|
+
self.inner_init = initializations.get(inner_init)
|
|
754
|
+
self.activation = activations.get(activation)
|
|
755
|
+
self.inner_activation = activations.get(inner_activation)
|
|
756
|
+
self.truncate_gradient = truncate_gradient
|
|
757
|
+
self.return_sequences = return_sequences
|
|
758
|
+
self.initial_weights = weights
|
|
759
|
+
self.go_backwards = go_backwards
|
|
760
|
+
|
|
761
|
+
self.input_dim = input_dim
|
|
762
|
+
self.input_length = input_length
|
|
763
|
+
if self.input_dim:
|
|
764
|
+
kwargs['input_shape'] = (self.input_length, self.input_dim)
|
|
765
|
+
super(JZS3, self).__init__(**kwargs)
|
|
766
|
+
|
|
767
|
+
def build(self):
|
|
768
|
+
input_dim = self.input_shape[2]
|
|
769
|
+
self.input = T.tensor3()
|
|
770
|
+
|
|
771
|
+
self.W_z = self.init((input_dim, self.output_dim))
|
|
772
|
+
self.U_z = self.inner_init((self.output_dim, self.output_dim))
|
|
773
|
+
self.b_z = shared_zeros((self.output_dim))
|
|
774
|
+
|
|
775
|
+
self.W_r = self.init((input_dim, self.output_dim))
|
|
776
|
+
self.U_r = self.inner_init((self.output_dim, self.output_dim))
|
|
777
|
+
self.b_r = shared_zeros((self.output_dim))
|
|
778
|
+
|
|
779
|
+
self.W_h = self.init((input_dim, self.output_dim))
|
|
780
|
+
self.U_h = self.inner_init((self.output_dim, self.output_dim))
|
|
781
|
+
self.b_h = shared_zeros((self.output_dim))
|
|
782
|
+
|
|
783
|
+
self.params = [
|
|
784
|
+
self.W_z, self.U_z, self.b_z,
|
|
785
|
+
self.W_r, self.U_r, self.b_r,
|
|
786
|
+
self.W_h, self.U_h, self.b_h,
|
|
787
|
+
]
|
|
788
|
+
|
|
789
|
+
if self.initial_weights is not None:
|
|
790
|
+
self.set_weights(self.initial_weights)
|
|
791
|
+
del self.initial_weights
|
|
792
|
+
|
|
793
|
+
def _step(self,
|
|
794
|
+
xz_t, xr_t, xh_t, mask_tm1,
|
|
795
|
+
h_tm1,
|
|
796
|
+
u_z, u_r, u_h):
|
|
797
|
+
h_mask_tm1 = mask_tm1 * h_tm1
|
|
798
|
+
z = self.inner_activation(xz_t + T.dot(T.tanh(h_mask_tm1), u_z))
|
|
799
|
+
r = self.inner_activation(xr_t + T.dot(h_mask_tm1, u_r))
|
|
800
|
+
hh_t = self.activation(xh_t + T.dot(r * h_mask_tm1, u_h))
|
|
801
|
+
h_t = hh_t * z + h_mask_tm1 * (1 - z)
|
|
802
|
+
return h_t
|
|
803
|
+
|
|
804
|
+
def get_output(self, train=False):
|
|
805
|
+
X = self.get_input(train)
|
|
806
|
+
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
|
|
807
|
+
X = X.dimshuffle((1, 0, 2))
|
|
808
|
+
|
|
809
|
+
x_z = T.dot(X, self.W_z) + self.b_z
|
|
810
|
+
x_r = T.dot(X, self.W_r) + self.b_r
|
|
811
|
+
x_h = T.dot(X, self.W_h) + self.b_h
|
|
812
|
+
outputs, updates = theano.scan(
|
|
813
|
+
self._step,
|
|
814
|
+
sequences=[x_z, x_r, x_h, padded_mask],
|
|
815
|
+
outputs_info=T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
|
|
816
|
+
non_sequences=[self.U_z, self.U_r, self.U_h],
|
|
817
|
+
truncate_gradient=self.truncate_gradient,
|
|
818
|
+
go_backwards=self.go_backwards)
|
|
819
|
+
|
|
820
|
+
if self.return_sequences:
|
|
821
|
+
return outputs.dimshuffle((1, 0, 2))
|
|
822
|
+
return outputs[-1]
|
|
823
|
+
|
|
824
|
+
def get_config(self):
|
|
825
|
+
config = {"name": self.__class__.__name__,
|
|
826
|
+
"output_dim": self.output_dim,
|
|
827
|
+
"init": self.init.__name__,
|
|
828
|
+
"inner_init": self.inner_init.__name__,
|
|
829
|
+
"activation": self.activation.__name__,
|
|
830
|
+
"inner_activation": self.inner_activation.__name__,
|
|
831
|
+
"truncate_gradient": self.truncate_gradient,
|
|
832
|
+
"return_sequences": self.return_sequences,
|
|
833
|
+
"input_dim": self.input_dim,
|
|
834
|
+
"input_length": self.input_length,
|
|
835
|
+
"go_backwards": self.go_backwards}
|
|
836
|
+
base_config = super(JZS3, self).get_config()
|
|
837
|
+
return dict(list(base_config.items()) + list(config.items()))
|