-
Notifications
You must be signed in to change notification settings - Fork 1
/
ALSTM.py
396 lines (333 loc) · 16.3 KB
/
ALSTM.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
import numpy as np
import theano
from theano import tensor as T
from keras.engine import Layer, InputSpec
from keras import backend as K
from keras import activations, initializations, regularizers
def time_distributed_dense(x, w, b=None, dropout=None,
input_dim=None, output_dim=None, timesteps=None, activation='linear'):
'''Apply y.w + b for every temporal slice y of x.
'''
activation = activations.get(activation)
if not input_dim:
# won't work with TensorFlow
input_dim = K.shape(x)[2]
if not timesteps:
# won't work with TensorFlow
timesteps = K.shape(x)[1]
if not output_dim:
# won't work with TensorFlow
output_dim = K.shape(w)[1]
if dropout is not None and 0. < dropout < 1.:
# apply the same dropout pattern at every timestep
ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim)))
dropout_matrix = K.dropout(ones, dropout)
expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps)
x = K.in_train_phase(x * expanded_dropout_matrix, x)
# collapse time dimension and batch dimension together
x = K.reshape(x, (-1, input_dim))
x = K.dot(x, w)
if b:
x = x + b
# reshape to 3D tensor
x = K.reshape(activation(x), (-1, timesteps, output_dim))
return x
class ARecurrent(Layer):
def __init__(self, weights=None,
return_sequences=False, go_backwards=False, stateful=False,
unroll=False, consume_less='cpu',
input_dim=None, input_length=None, **kwargs):
self.return_sequences = return_sequences
self.initial_weights = weights
self.go_backwards = go_backwards
self.stateful = stateful
self.unroll = unroll
self.consume_less = consume_less
self.supports_masking = True
self.input_spec = [InputSpec(ndim=4)]
self.input_dim = input_dim
self.input_length = input_length
if self.input_dim:
kwargs['input_shape'] = (self.input_length, self.input_dim)
super(ARecurrent, self).__init__(**kwargs)
def get_output_shape_for(self, input_shape):
if self.return_sequences:
return (input_shape[0], input_shape[1], self.output_dim)
else:
return (input_shape[0], self.output_dim)
def compute_mask(self, input, mask):
if self.return_sequences:
return mask
else:
return None
def step(self, x, states):
raise NotImplementedError
def get_constants(self, x):
return []
def get_initial_states(self, x):
# build an all-zero tensor of shape (samples, output_dim)
initial_state = K.zeros_like(x[:,:,0,:]) # (samples, timesteps, prev_timesteps, input_dim)
initial_state = K.sum(initial_state, axis=1) # (samples, prev_timesteps, input_dim)
reducer = K.zeros((self.input_dim, self.output_dim))
initial_state = K.dot(initial_state, reducer) # (samples, output_dim)
initial_states = [initial_state for _ in range(len(self.states))]
return initial_states
def preprocess_input(self, x):
return x
def call(self, x, mask=None):
# input shape: (nb_samples, time (padded with zeros), input_dim)
# note that the .build() method of subclasses MUST define
# self.input_spec with a complete input shape.
input_shape = self.input_spec[0].shape
if K._BACKEND == 'tensorflow':
if not input_shape[1]:
raise Exception('When using TensorFlow, you should define '
'explicitly the number of timesteps of '
'your sequences.\n'
'If your first layer is an Embedding, '
'make sure to pass it an "input_length" '
'argument. Otherwise, make sure '
'the first layer has '
'an "input_shape" or "batch_input_shape" '
'argument, including the time axis. '
'Found input shape at layer ' + self.name +
': ' + str(input_shape))
if self.stateful:
initial_states = self.states
else:
initial_states = self.get_initial_states(x)
constants = self.get_constants(x)
preprocessed_input = self.preprocess_input(x)
last_output, outputs, states = K.rnn(self.step, preprocessed_input,
initial_states,
go_backwards=self.go_backwards,
mask=mask,
constants=constants,
unroll=self.unroll,
input_length=input_shape[1])
if self.stateful:
self.updates = []
for i in range(len(states)):
self.updates.append((self.states[i], states[i]))
if self.return_sequences:
return outputs
else:
return last_output
def get_config(self):
config = {'return_sequences': self.return_sequences,
'go_backwards': self.go_backwards,
'stateful': self.stateful,
'unroll': self.unroll,
'consume_less': self.consume_less}
if self.stateful:
config['batch_input_shape'] = self.input_spec[0].shape
else:
config['input_dim'] = self.input_dim
config['input_length'] = self.input_length
base_config = super(ARecurrent, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class ALSTM(ARecurrent):
def __init__(self, output_dim,
init='glorot_uniform', inner_init='orthogonal',
forget_bias_init='one', activation='tanh',
inner_activation='hard_sigmoid',
W_regularizer=None, U_regularizer=None, b_regularizer=None,
dropout_W=0., dropout_U=0., **kwargs):
self.output_dim = output_dim
self.init = initializations.get(init)
self.inner_init = initializations.get(inner_init)
self.forget_bias_init = initializations.get(forget_bias_init)
self.activation = activations.get(activation)
self.inner_activation = activations.get(inner_activation)
self.W_regularizer = regularizers.get(W_regularizer)
self.U_regularizer = regularizers.get(U_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.dropout_W, self.dropout_U = dropout_W, dropout_U
if self.dropout_W or self.dropout_U:
self.uses_learning_phase = True
super(ALSTM, self).__init__(**kwargs)
def build(self, input_shape):
#assert self.output_dim == input_shape[-1]
self.input_spec = [InputSpec(shape=input_shape)]
self.middle_length = input_shape[2]
input_dim = input_shape[3]
# Attention
self.W_a = self.init((input_dim + self.output_dim, self.output_dim),
name='{}_W_a'.format(self.name))
self.b_a = K.zeros((self.output_dim,), name='{}_b_a'.format(self.name))
# Regular LSTM
self.input_dim = input_dim
if self.stateful:
self.reset_states()
else:
# initial states: 2 all-zero tensors of shape (output_dim)
self.states = [None, None]
self.W_i = self.init((input_dim, self.output_dim),
name='{}_W_i'.format(self.name))
self.U_i = self.inner_init((self.output_dim, self.output_dim),
name='{}_U_i'.format(self.name))
self.b_i = K.zeros((self.output_dim,), name='{}_b_i'.format(self.name))
self.W_f = self.init((input_dim, self.output_dim),
name='{}_W_f'.format(self.name))
self.U_f = self.inner_init((self.output_dim, self.output_dim),
name='{}_U_f'.format(self.name))
self.b_f = self.forget_bias_init((self.output_dim,),
name='{}_b_f'.format(self.name))
self.W_c = self.init((input_dim, self.output_dim),
name='{}_W_c'.format(self.name))
self.U_c = self.inner_init((self.output_dim, self.output_dim),
name='{}_U_c'.format(self.name))
self.b_c = K.zeros((self.output_dim,), name='{}_b_c'.format(self.name))
self.W_o = self.init((input_dim, self.output_dim),
name='{}_W_o'.format(self.name))
self.U_o = self.inner_init((self.output_dim, self.output_dim),
name='{}_U_o'.format(self.name))
self.b_o = K.zeros((self.output_dim,), name='{}_b_o'.format(self.name))
self.regularizers = []
if self.W_regularizer:
self.W_regularizer.set_param(K.concatenate([self.W_i,
self.W_f,
self.W_c,
self.W_o]))
self.regularizers.append(self.W_regularizer)
if self.U_regularizer:
self.U_regularizer.set_param(K.concatenate([self.U_i,
self.U_f,
self.U_c,
self.U_o]))
self.regularizers.append(self.U_regularizer)
if self.b_regularizer:
self.b_regularizer.set_param(K.concatenate([self.b_i,
self.b_f,
self.b_c,
self.b_o]))
self.regularizers.append(self.b_regularizer)
self.trainable_weights = [self.W_i, self.U_i, self.b_i,
self.W_c, self.U_c, self.b_c,
self.W_f, self.U_f, self.b_f,
self.W_o, self.U_o, self.b_o,
self.W_a, self.b_a]
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def reset_states(self):
assert self.stateful, 'Layer must be stateful.'
input_shape = self.input_spec[0].shape
if not input_shape[0]:
raise Exception('If a RNN is stateful, a complete ' +
'input_shape must be provided (including batch size).')
if hasattr(self, 'states'):
K.set_value(self.states[0],
np.zeros((input_shape[0], self.output_dim)))
K.set_value(self.states[1],
np.zeros((input_shape[0], self.output_dim)))
else:
self.states = [K.zeros((input_shape[0], self.output_dim)),
K.zeros((input_shape[0], self.output_dim))]
def preprocess_input(self, x):
return x
def step(self, x, states):
# ALTM
h_tm1 = states[0]
exp_h_tm1 = h_tm1.dimshuffle((0, 'x', 1))
exp_h_tm1 = T.extra_ops.repeat(exp_h_tm1, x.shape[1], axis=1)
con = K.concatenate((x,exp_h_tm1), axis=-1)
tdense = time_distributed_dense(con, self.W_a, self.b_a, None, self.input_dim+self.output_dim, self.output_dim, x.shape[1])
d_sum = K.sum(tdense, axis=-1)
sm = K.softmax(d_sum)
sm = sm.dimshuffle((0, 1, 'x'))
sm = T.extra_ops.repeat(sm, self.input_dim, axis=-1)
new_x = sm * x
new_x = K.sum(new_x, axis=1)
# LSTM
#h_tm1 = states[0]
c_tm1 = states[1]
B_U = states[2]
B_W = states[3]
x_i = K.dot(new_x * B_W[0], self.W_i) + self.b_i
x_f = K.dot(new_x * B_W[1], self.W_f) + self.b_f
x_c = K.dot(new_x * B_W[2], self.W_c) + self.b_c
x_o = K.dot(new_x * B_W[3], self.W_o) + self.b_o
i = self.inner_activation(x_i + K.dot(h_tm1 * B_U[0], self.U_i))
f = self.inner_activation(x_f + K.dot(h_tm1 * B_U[1], self.U_f))
c = f * c_tm1 + i * self.activation(x_c + K.dot(h_tm1 * B_U[2], self.U_c))
o = self.inner_activation(x_o + K.dot(h_tm1 * B_U[3], self.U_o))
h = o * self.activation(c)
return h, [h, c]
def get_constants(self, x):
constants = []
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
return constants
def get_config(self):
config = {"output_dim": self.output_dim,
"init": self.init.__name__,
"inner_init": self.inner_init.__name__,
"forget_bias_init": self.forget_bias_init.__name__,
"activation": self.activation.__name__,
"inner_activation": self.inner_activation.__name__,
"W_regularizer": self.W_regularizer.get_config() if self.W_regularizer else None,
"U_regularizer": self.U_regularizer.get_config() if self.U_regularizer else None,
"b_regularizer": self.b_regularizer.get_config() if self.b_regularizer else None,
"dropout_W": self.dropout_W,
"dropout_U": self.dropout_U}
base_config = super(ALSTM, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class RepeatTimeDistributedVector(Layer):
def __init__(self, n, **kwargs):
self.n = n
self.input_spec = [InputSpec(ndim=3)]
super(RepeatTimeDistributedVector, self).__init__(**kwargs)
def get_output_shape_for(self, input_shape):
return (input_shape[0], self.n, input_shape[1], input_shape[2])
def call(self, x, mask=None):
x = x.dimshuffle((0, 'x', 1, 2))
return T.extra_ops.repeat(x,self.n, axis=1)
def get_config(self):
config = {'n': self.n}
base_config = super(RepeatTimeDistributedVector, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def time_distributed_softmax(x):
xshape = x.shape
X = x.reshape((xshape[0] * xshape[1], xshape[2]))
return T.nnet.softmax(X).reshape(xshape)
class HierarchicalSoftmax(Layer):
def __init__(self, levels=2, init='glorot_uniform', weights=None, **kwargs):
self.levels = levels
self.init = initializations.get(init)
self.initial_weights = weights
super(HierarchicalSoftmax, self).__init__(**kwargs)
def build(self, input_shape):
self.output_dim = input_shape[1]
self.level_size = np.ceil(np.power(self.output_dim,1/self.levels))
self.W_shape = (self.output_dim, self.level_size)
self.W_list = []
self.b_list = []
for i in range(self.levels):
self.W_list.append(self.init(self.W_shape, name='W_{}'.format(i)))
self.b_list.append(K.zeros((self.level_size,), name='b_{}'.format(i)))
self.trainable_weights = self.W_list + self.b_list
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def call(self, x, mask=None):
h_levels = []
for i in range(self.levels):
h_levels.append(K.softmax(K.dot(x, self.W_list[i]) + self.b_list[i]))
def _path_probas(idx):
results = []
for i in range(self.levels-1):
lev1_vec = h_levels[0][idx] if i == 0 else results[-1]
lev2_vec = h_levels[i+1][idx]
result, _ = theano.scan(fn=lambda k, array_: k * array_,
sequences=lev1_vec,
non_sequences=lev2_vec)
results.append(result.flatten())
return K.concatenate(results)
output, _ = theano.scan(fn=_path_probas, sequences=T.arange(x.shape[0]))
output = output[:, :self.output_dim]
return output
def get_config(self):
config = {'init': self.init.__name__}
base_config = super(HierarchicalSoftmax, self).get_config()
return dict(list(base_config.items()) + list(config.items()))