-
Notifications
You must be signed in to change notification settings - Fork 58
/
layers.py
525 lines (435 loc) · 20.8 KB
/
layers.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import numpy as np
import keras.backend as K
import tensorflow as tf
from keras import activations, initializations, regularizers
import keras.layers as keras_layers
from keras.layers.recurrent import Recurrent
from keras.engine import Layer, InputSpec
from .layers_utils import highway_bias_initializer
from .layers_utils import layer_normalization
from .layers_utils import multiplicative_integration_init
from .layers_utils import multiplicative_integration
from .layers_utils import zoneout
from .initializers import k_init
import logging
class LayerNormalization(Layer):
'''Normalize from all of the summed inputs to the neurons in a layer on
a single training case. Unlike batch normalization, layer normalization
performs exactly the same computation at training and tests time.
# Arguments
epsilon: small float > 0. Fuzz parameter
num_var: how many tensor are condensed in the input
weights: Initialization weights.
List of 2 Numpy arrays, with shapes:
`[(input_shape,), (input_shape,)]`
Note that the order of this list is [gain, bias]
gain_init: name of initialization function for gain parameter
(see [initializations](../initializations.md)), or alternatively,
Theano/TensorFlow function to use for weights initialization.
This parameter is only relevant if you don't pass a `weights`
argument.
bias_init: name of initialization function for bias parameter
(see [initializations](../initializations.md)), or alternatively,
Theano/TensorFlow function to use for weights initialization.
This parameter is only relevant if you don't pass a `weights`
argument.
# Input shape
# Output shape
Same shape as input.
# References
- [Layer Normalization](https://arxiv.org/abs/1607.06450)
'''
def __init__(self, epsilon=1e-5, weights=None, gain_init='one',
bias_init='zero', **kwargs):
self.epsilon = epsilon
self.gain_init = initializations.get(gain_init)
self.bias_init = initializations.get(bias_init)
self.initial_weights = weights
self._logger = logging.getLogger('%s.%s' % (__name__,
self.__class__.__name__))
super(LayerNormalization, self).__init__(**kwargs)
def build(self, input_shape):
self.input_spec = [InputSpec(shape=input_shape)]
shape = (input_shape[-1],)
self.g = self.gain_init(shape, name='{}_gain'.format(self.name))
self.b = self.bias_init(shape, name='{}_bias'.format(self.name))
self.trainable_weights = [self.g, self.b]
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
self.built = True
def call(self, x, mask=None):
return LN(x, self.g, self.b, epsilon=self.epsilon)
def get_config(self):
config = {"epsilon": self.epsilon,
'num_var': self.num_var,
'gain_init': self.gain_init.__name__,
'bias_init': self.bias_init.__name__}
base_config = super(LayerNormalization, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class RHN(Recurrent):
'''Recurrent Highway Network - Julian Georg Zilly, Rupesh Kumar Srivastava,
Jan Koutník, Jürgen Schmidhuber - 2016.
For a step-by-step description of the network, see
[this paper](https://arxiv.org/abs/1607.03474).
# Arguments
output_dim: dimension of the internal projections and the final output.
depth: recurrency depth size.
init: weight initialization function.
Can be the name of an existing function (str),
or a Theano function (see:
[initializations](../initializations.md)).
inner_init: initialization function of the inner cells.
bias_init: initialization function of the bias.
(see [this
post](http://people.idsia.ch/~rupesh/very_deep_learning/)
for more information)
activation: activation function.
Can be the name of an existing function (str),
or a Theano function (see: [activations](../activations.md)).
inner_activation: activation function for the inner cells.
coupling: if True, carry gate will be coupled to the transform gate,
i.e., c = 1 - t
W_regularizer: instance of [WeightRegularizer](../regularizers.md)
(eg. L1 or L2 regularization), applied to the input weights
matrices.
U_regularizer: instance of [WeightRegularizer](../regularizers.md)
(eg. L1 or L2 regularization), applied to the recurrent weights
matrices.
b_regularizer: instance of [WeightRegularizer](../regularizers.md),
applied to the bias.
dropout_W: float between 0 and 1. Fraction of the input units to drop
for input gates.
dropout_U: float between 0 and 1. Fraction of the input units to drop
for recurrent connections.
# References
- [Recurrent Highway Networks](https://arxiv.org/abs/1607.03474)
(original paper)
- [Layer Normalization](https://arxiv.org/abs/1607.06450)
- [A Theoretically Grounded Application of Dropout in Recurrent Neural
Networks](http://arxiv.org/abs/1512.05287)
# TODO: different dropout rates for each layer
'''
def __init__(self, output_dim, depth=1,
init='glorot_uniform', inner_init='orthogonal',
bias_init=highway_bias_initializer,
activation='tanh', inner_activation='hard_sigmoid',
coupling=True, layer_norm=False, ln_gain_init='one',
ln_bias_init='zero', mi=False,
W_regularizer=None, U_regularizer=None,
b_regularizer=None, dropout_W=0., dropout_U=0., **kwargs):
self.output_dim = output_dim
self.depth = depth
self.init = initializations.get(init)
self.inner_init = initializations.get(inner_init)
self.bias_init = initializations.get(bias_init)
self.activation = activations.get(activation)
self.inner_activation = activations.get(inner_activation)
self.coupling = coupling
self.has_layer_norm = layer_norm
self.ln_gain_init = initializations.get(ln_gain_init)
self.ln_bias_init = initializations.get(ln_bias_init)
self.mi = mi
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
self._logger = logging.getLogger('%s.%s' % (__name__,
self.__class__.__name__))
if self.dropout_W or self.dropout_U:
self.uses_learning_phase = True
super(RHN, self).__init__(**kwargs)
if not self.consume_less == "gpu":
self._logger.warning("Ignoring consume_less=%s. Setting to 'gpu'." % self.consume_less)
def build(self, input_shape):
self.input_spec = [InputSpec(shape=input_shape)]
self.input_dim = input_shape[2]
if self.stateful:
self.reset_states()
else:
self.states = [None]
self.W = self.init((self.input_dim, (2 + (not self.coupling)) *
self.output_dim), name='{}_W'.format(self.name))
self.Us = [self.inner_init(
(self.output_dim, (2 + (not self.coupling)) * self.output_dim),
name='%s_%d_U' % (self.name, i)) for i in xrange(self.depth)]
bias_init_value = K.get_value(self.bias_init((self.output_dim,)))
b = [np.zeros(self.output_dim),
np.copy(bias_init_value)]
if not self.coupling:
b.append(np.copy(bias_init_value))
self.bs = [K.variable(np.hstack(b),
name='%s_%d_b' % (self.name, i)) for i in
xrange(self.depth)]
self.trainable_weights = [self.W] + self.Us + self.bs
if self.mi:
self.mi_params = [multiplicative_integration_init(
((2 + (not self.coupling)) * self.output_dim,),
name='%s_%d' % (self.name, i),
has_input=(i == 0)) for i in xrange(self.depth)]
for p in self.mi_params:
if type(p) in {list, tuple}:
self.trainable_weights += p
else:
self.trainable_weights += [p]
if self.has_layer_norm:
self.ln_weights = []
ln_names = ['h', 't', 'c']
for l in xrange(self.depth):
ln_gains = [self.ln_gain_init(
(self.output_dim,), name='%s_%d_ln_gain_%s' %
(self.name, l, ln_names[i])) for i in xrange(1)]
ln_biases = [self.ln_bias_init(
(self.output_dim,), name='%s_%d_ln_bias_%s' %
(self.name, l, ln_names[i])) for i in xrange(1)]
self.ln_weights.append([ln_gains, ln_biases])
self.trainable_weights += ln_gains + ln_biases
self.regularizers = []
if self.W_regularizer:
self.W_regularizer.set_param(self.W)
self.regularizers.append(self.W_regularizer)
if self.U_regularizer:
self.U_regularizer.set_param(self.U)
self.regularizers.append(self.U_regularizer)
if self.b_regularizer:
self.b_regularizer.set_param(self.b)
self.regularizers.append(self.b_regularizer)
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)))
else:
self.states = [K.zeros((input_shape[0], self.output_dim))]
def step(self, x, states):
s_tm1 = states[0]
for layer in xrange(self.depth):
B_U = states[layer + 1][0]
U, b = self.Us[layer], self.bs[layer]
if layer == 0:
B_W = states[layer + 1][1]
Wx = K.dot(x * B_W, self.W)
else:
Wx = 0
Us = K.dot(s_tm1 * B_U, U)
if self.mi:
a = multiplicative_integration(Wx, Us,
self.mi_params[layer]) + b
else:
a = Wx + Us + b
a0 = a[:, :self.output_dim]
a1 = a[:, self.output_dim: 2 * self.output_dim]
if not self.coupling:
a2 = a[:, 2 * self.output_dim:]
if self.has_layer_norm:
ln_gains, ln_biases = self.ln_weights[layer]
a0 = LN(a0, ln_gains[0], ln_biases[0])
# a1 = LN(a1, ln_gains[1], ln_biases[1])
# if not self.coupling:
# a2 = LN(a2, ln_gains[2], ln_biases[2])
# Equation 7
h = self.activation(a0)
# Equation 8
t = self.inner_activation(a1)
# Equation 9
if not self.coupling:
c = self.inner_activation(a2)
else:
c = 1 - t # carry gate was coupled to the transform gate
s = h * t + s_tm1 * c
s_tm1 = s
return s, [s]
def get_constants(self, x):
constants = []
for layer in xrange(self.depth):
constant = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.output_dim))
B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones)
constant.append(B_U)
else:
constant.append(K.cast_to_floatx(1.))
if layer == 0:
if 0 < self.dropout_W < 1:
input_shape = self.input_spec[0].shape
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, input_dim))
B_W = K.in_train_phase(K.dropout(ones,
self.dropout_W), ones)
constant.append(B_W)
else:
constant.append(K.cast_to_floatx(1.))
constants.append(constant)
return constants
def get_config(self):
config = {'output_dim': self.output_dim,
'depth': self.depth,
'init': self.init.__name__,
'inner_init': self.inner_init.__name__,
'bias_init': self.bias_init.__name__,
'activation': self.activation.__name__,
'inner_activation': self.inner_activation.__name__,
'coupling': self.coupling,
'layer_norm': self.has_layer_norm,
'ln_gain_init': self.ln_gain_init.__name__,
'ln_bias_init': self.ln_bias_init.__name__,
'mi': self.mi,
'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(RHN, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class LSTM(keras_layers.LSTM):
"""
# Arguments
ln: None, list of float or list of list of floats. Determines whether will apply LN or not. If list of floats, the same init will be applied to every LN; otherwise will be individual
mi: list of floats or None. If list of floats, the multiplicative integration will be active and initialized with these values.
zoneout_h: float between 0 and 1. Fraction of the hidden/output units to maintain their previous values.
zoneout_c: float between 0 and 1. Fraction of the cell units to maintain their previous values.
# References
- [Zoneout: Regularizing RNNs by Randomly Preserving Hidden Activations](https://arxiv.org/abs/1606.01305)
"""
def __init__(self, output_dim, zoneout_h=0., zoneout_c=0.,
layer_norm=None, mi=None, **kwargs):
super(LSTM, self).__init__(output_dim, **kwargs)
self._logger = logging.getLogger('%s.%s' % (__name__,
self.__class__.__name__))
self.layer_norm = layer_norm
self.mi = mi
self.zoneout_c = zoneout_c
self.zoneout_h = zoneout_h
if self.zoneout_h or self.zoneout_c:
self.uses_learning_phase = True
if self.consume_less != 'gpu':
self._logger.warn("Invalid option for `consume_less`. Falling back \
to option `gpu`.")
self.consume_less = 'gpu'
def build(self, input_shape):
super(LSTM, self).build(input_shape)
if self.mi is not None:
alpha_init, beta1_init, beta2_init = self.mi
self.mi_alpha = self.add_weight(
(4 * self.output_dim, ),
initializer=k_init(alpha_init),
name='{}_mi_alpha'.format(self.name))
self.mi_beta1 = self.add_weight(
(4 * self.output_dim, ),
initializer=k_init(beta1_init),
name='{}_mi_beta1'.format(self.name))
self.mi_beta2 = self.add_weight(
(4 * self.output_dim, ),
initializer=k_init(beta2_init),
name='{}_mi_beta2'.format(self.name))
if self.layer_norm is not None:
ln_gain_init, ln_bias_init = self.layer_norm
self.layer_norm_params = {}
for n, i in {'Uh': 4, 'Wx': 4, 'new_c': 1}.items():
gain = self.add_weight(
(i*self.output_dim, ),
initializer=k_init(ln_gain_init),
name='%s_ln_gain_%s' % (self.name, n))
bias = self.add_weight(
(i*self.output_dim, ),
initializer=k_init(ln_bias_init),
name='%s_ln_bias_%s' % (self.name, n))
self.layer_norm_params[n] = [gain, bias]
def _layer_norm(self, x, param_name):
if self.layer_norm is None:
return x
gain, bias = self.layer_norm_params[param_name]
return layer_normalization(x, gain, bias)
def step(self, x, states):
h_tm1 = states[0]
c_tm1 = states[1]
B_U = states[2]
B_W = states[3]
Uh = self._layer_norm(K.dot(h_tm1 * B_U[0], self.U), 'Uh')
Wx = self._layer_norm(K.dot(x * B_W[0], self.W), 'Wx')
if self.mi is not None:
z = self.mi_alpha * Wx * Uh + self.mi_beta1 * Uh + \
self.mi_beta2 * Wx + self.b
else:
z = Wx + Uh + self.b
z_i = z[:, :self.output_dim]
z_f = z[:, self.output_dim: 2 * self.output_dim]
z_c = z[:, 2 * self.output_dim: 3 * self.output_dim]
z_o = z[:, 3 * self.output_dim:]
i = self.inner_activation(z_i)
f = self.inner_activation(z_f)
c = f * c_tm1 + i * self.activation(z_c)
o = self.inner_activation(z_o)
if 0 < self.zoneout_c < 1:
c = zoneout(self.zoneout_c, c_tm1, c,
noise_shape=(self.output_dim,))
# this is returning a lot of nan
new_c = self._layer_norm(c, 'new_c')
h = o * self.activation(new_c)
if 0 < self.zoneout_h < 1:
h = zoneout(self.zoneout_h, h_tm1, h,
noise_shape=(self.output_dim,))
return h, [h, c]
def get_config(self):
config = {'layer_norm': self.layer_norm,
'mi': self.mi,
'zoneout_h': self.zoneout_h,
'zoneout_c': self.zoneout_c
}
base_config = super(LSTM, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def recurrent(output_dim, model='keras_lstm', activation='tanh',
regularizer=None, dropout=0., **kwargs):
if model == 'rnn':
return keras_layers.SimpleRNN(output_dim, activation=activation,
W_regularizer=regularizer,
U_regularizer=regularizer,
dropout_W=dropout, dropout_U=dropout, consume_less='gpu',
**kwargs)
if model == 'gru':
return keras_layers.GRU(output_dim, activation=activation,
W_regularizer=regularizer,
U_regularizer=regularizer, dropout_W=dropout,
dropout_U=dropout,
consume_less='gpu', **kwargs)
if model == 'keras_lstm':
return keras_layers.LSTM(output_dim, activation=activation,
W_regularizer=regularizer,
U_regularizer=regularizer,
dropout_W=dropout, dropout_U=dropout,
consume_less='gpu', **kwargs)
if model == 'rhn':
return RHN(output_dim, depth=1,
bias_init=highway_bias_initializer,
activation=activation, layer_norm=False, ln_gain_init='one',
ln_bias_init='zero', mi=False,
W_regularizer=regularizer, U_regularizer=regularizer,
dropout_W=dropout, dropout_U=dropout, consume_less='gpu',
**kwargs)
if model == 'lstm':
return LSTM(output_dim, activation=activation,
W_regularizer=regularizer, U_regularizer=regularizer,
dropout_W=dropout, dropout_U=dropout,
consume_less='gpu', **kwargs)
raise ValueError('model %s was not recognized' % model)
if __name__ == "__main__":
from keras.models import Sequential
from keras.utils.visualize_util import plot
model = Sequential()
model.add(RHN(10, input_dim=2, depth=2, layer_norm=True))
# plot(model)