-
Notifications
You must be signed in to change notification settings - Fork 280
/
bn.py
459 lines (413 loc) · 21 KB
/
bn.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Authors: Chiheb Trabelsi, Olexa Bilaniuk
#
# Note: The implementation of complex Batchnorm is based on
# the Keras implementation of batch Normalization
# available here:
# https://github.com/fchollet/keras/blob/master/keras/layers/normalization.py
import numpy as np
from keras.layers import Layer, InputSpec
from keras import initializers, regularizers, constraints
import keras.backend as K
def sqrt_init(shape, dtype=None):
value = (1 / K.sqrt(2)) * K.ones(shape)
return value
def sanitizedInitGet(init):
if init in ["sqrt_init"]:
return sqrt_init
else:
return initializers.get(init)
def sanitizedInitSer(init):
if init in [sqrt_init]:
return "sqrt_init"
else:
return initializers.serialize(init)
def complex_standardization(input_centred, Vrr, Vii, Vri,
layernorm=False, axis=-1):
ndim = K.ndim(input_centred)
input_dim = K.shape(input_centred)[axis] // 2
variances_broadcast = [1] * ndim
variances_broadcast[axis] = input_dim
if layernorm:
variances_broadcast[0] = K.shape(input_centred)[0]
# We require the covariance matrix's inverse square root. That first requires
# square rooting, followed by inversion (I do this in that order because during
# the computation of square root we compute the determinant we'll need for
# inversion as well).
# tau = Vrr + Vii = Trace. Guaranteed >= 0 because SPD
tau = Vrr + Vii
# delta = (Vrr * Vii) - (Vri ** 2) = Determinant. Guaranteed >= 0 because SPD
delta = (Vrr * Vii) - (Vri ** 2)
s = np.sqrt(delta) # Determinant of square root matrix
t = np.sqrt(tau + 2 * s)
# The square root matrix could now be explicitly formed as
# [ Vrr+s Vri ]
# (1/t) [ Vir Vii+s ]
# https://en.wikipedia.org/wiki/Square_root_of_a_2_by_2_matrix
# but we don't need to do this immediately since we can also simultaneously
# invert. We can do this because we've already computed the determinant of
# the square root matrix, and can thus invert it using the analytical
# solution for 2x2 matrices
# [ A B ] [ D -B ]
# inv( [ C D ] ) = (1/det) [ -C A ]
# http://mathworld.wolfram.com/MatrixInverse.html
# Thus giving us
# [ Vii+s -Vri ]
# (1/s)(1/t)[ -Vir Vrr+s ]
# So we proceed as follows:
inverse_st = 1.0 / (s * t)
Wrr = (Vii + s) * inverse_st
Wii = (Vrr + s) * inverse_st
Wri = -Vri * inverse_st
# And we have computed the inverse square root matrix W = sqrt(V)!
# Normalization. We multiply, x_normalized = W.x.
# The returned result will be a complex standardized input
# where the real and imaginary parts are obtained as follows:
# x_real_normed = Wrr * x_real_centred + Wri * x_imag_centred
# x_imag_normed = Wri * x_real_centred + Wii * x_imag_centred
broadcast_Wrr = K.reshape(Wrr, variances_broadcast)
broadcast_Wri = K.reshape(Wri, variances_broadcast)
broadcast_Wii = K.reshape(Wii, variances_broadcast)
cat_W_4_real = K.concatenate([broadcast_Wrr, broadcast_Wii], axis=axis)
cat_W_4_imag = K.concatenate([broadcast_Wri, broadcast_Wri], axis=axis)
if (axis == 1 and ndim != 3) or ndim == 2:
centred_real = input_centred[:, :input_dim]
centred_imag = input_centred[:, input_dim:]
elif ndim == 3:
centred_real = input_centred[:, :, :input_dim]
centred_imag = input_centred[:, :, input_dim:]
elif axis == -1 and ndim == 4:
centred_real = input_centred[:, :, :, :input_dim]
centred_imag = input_centred[:, :, :, input_dim:]
elif axis == -1 and ndim == 5:
centred_real = input_centred[:, :, :, :, :input_dim]
centred_imag = input_centred[:, :, :, :, input_dim:]
else:
raise ValueError(
'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
)
rolled_input = K.concatenate([centred_imag, centred_real], axis=axis)
output = cat_W_4_real * input_centred + cat_W_4_imag * rolled_input
# Wrr * x_real_centered | Wii * x_imag_centered
# + Wri * x_imag_centered | Wri * x_real_centered
# -----------------------------------------------
# = output
return output
def ComplexBN(input_centred, Vrr, Vii, Vri, beta,
gamma_rr, gamma_ri, gamma_ii, scale=True,
center=True, layernorm=False, axis=-1):
ndim = K.ndim(input_centred)
input_dim = K.shape(input_centred)[axis] // 2
if scale:
gamma_broadcast_shape = [1] * ndim
gamma_broadcast_shape[axis] = input_dim
if center:
broadcast_beta_shape = [1] * ndim
broadcast_beta_shape[axis] = input_dim * 2
if scale:
standardized_output = complex_standardization(
input_centred, Vrr, Vii, Vri,
layernorm,
axis=axis
)
# Now we perform th scaling and Shifting of the normalized x using
# the scaling parameter
# [ gamma_rr gamma_ri ]
# Gamma = [ gamma_ri gamma_ii ]
# and the shifting parameter
# Beta = [beta_real beta_imag].T
# where:
# x_real_BN = gamma_rr * x_real_normed + gamma_ri * x_imag_normed + beta_real
# x_imag_BN = gamma_ri * x_real_normed + gamma_ii * x_imag_normed + beta_imag
broadcast_gamma_rr = K.reshape(gamma_rr, gamma_broadcast_shape)
broadcast_gamma_ri = K.reshape(gamma_ri, gamma_broadcast_shape)
broadcast_gamma_ii = K.reshape(gamma_ii, gamma_broadcast_shape)
cat_gamma_4_real = K.concatenate([broadcast_gamma_rr, broadcast_gamma_ii], axis=axis)
cat_gamma_4_imag = K.concatenate([broadcast_gamma_ri, broadcast_gamma_ri], axis=axis)
if (axis == 1 and ndim != 3) or ndim == 2:
centred_real = standardized_output[:, :input_dim]
centred_imag = standardized_output[:, input_dim:]
elif ndim == 3:
centred_real = standardized_output[:, :, :input_dim]
centred_imag = standardized_output[:, :, input_dim:]
elif axis == -1 and ndim == 4:
centred_real = standardized_output[:, :, :, :input_dim]
centred_imag = standardized_output[:, :, :, input_dim:]
elif axis == -1 and ndim == 5:
centred_real = standardized_output[:, :, :, :, :input_dim]
centred_imag = standardized_output[:, :, :, :, input_dim:]
else:
raise ValueError(
'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
)
rolled_standardized_output = K.concatenate([centred_imag, centred_real], axis=axis)
if center:
broadcast_beta = K.reshape(beta, broadcast_beta_shape)
return cat_gamma_4_real * standardized_output + cat_gamma_4_imag * rolled_standardized_output + broadcast_beta
else:
return cat_gamma_4_real * standardized_output + cat_gamma_4_imag * rolled_standardized_output
else:
if center:
broadcast_beta = K.reshape(beta, broadcast_beta_shape)
return input_centred + broadcast_beta
else:
return input_centred
class ComplexBatchNormalization(Layer):
"""Complex version of the real domain
Batch normalization layer (Ioffe and Szegedy, 2014).
Normalize the activations of the previous complex layer at each batch,
i.e. applies a transformation that maintains the mean of a complex unit
close to the null vector, the 2 by 2 covariance matrix of a complex unit close to identity
and the 2 by 2 relation matrix, also called pseudo-covariance, close to the
null matrix.
# Arguments
axis: Integer, the axis that should be normalized
(typically the features axis).
For instance, after a `Conv2D` layer with
`data_format="channels_first"`,
set `axis=2` in `ComplexBatchNormalization`.
momentum: Momentum for the moving statistics related to the real and
imaginary parts.
epsilon: Small float added to each of the variances related to the
real and imaginary parts in order to avoid dividing by zero.
center: If True, add offset of `beta` to complex normalized tensor.
If False, `beta` is ignored.
(beta is formed by real_beta and imag_beta)
scale: If True, multiply by the `gamma` matrix.
If False, `gamma` is not used.
beta_initializer: Initializer for the real_beta and the imag_beta weight.
gamma_diag_initializer: Initializer for the diagonal elements of the gamma matrix.
which are the variances of the real part and the imaginary part.
gamma_off_initializer: Initializer for the off-diagonal elements of the gamma matrix.
moving_mean_initializer: Initializer for the moving means.
moving_variance_initializer: Initializer for the moving variances.
moving_covariance_initializer: Initializer for the moving covariance of
the real and imaginary parts.
beta_regularizer: Optional regularizer for the beta weights.
gamma_regularizer: Optional regularizer for the gamma weights.
beta_constraint: Optional constraint for the beta weights.
gamma_constraint: Optional constraint for the gamma weights.
# Input shape
Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.
# Output shape
Same shape as input.
# References
- [Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift](https://arxiv.org/abs/1502.03167)
"""
def __init__(self,
axis=-1,
momentum=0.9,
epsilon=1e-4,
center=True,
scale=True,
beta_initializer='zeros',
gamma_diag_initializer='sqrt_init',
gamma_off_initializer='zeros',
moving_mean_initializer='zeros',
moving_variance_initializer='sqrt_init',
moving_covariance_initializer='zeros',
beta_regularizer=None,
gamma_diag_regularizer=None,
gamma_off_regularizer=None,
beta_constraint=None,
gamma_diag_constraint=None,
gamma_off_constraint=None,
**kwargs):
super(ComplexBatchNormalization, self).__init__(**kwargs)
self.supports_masking = True
self.axis = axis
self.momentum = momentum
self.epsilon = epsilon
self.center = center
self.scale = scale
self.beta_initializer = sanitizedInitGet(beta_initializer)
self.gamma_diag_initializer = sanitizedInitGet(gamma_diag_initializer)
self.gamma_off_initializer = sanitizedInitGet(gamma_off_initializer)
self.moving_mean_initializer = sanitizedInitGet(moving_mean_initializer)
self.moving_variance_initializer = sanitizedInitGet(moving_variance_initializer)
self.moving_covariance_initializer = sanitizedInitGet(moving_covariance_initializer)
self.beta_regularizer = regularizers.get(beta_regularizer)
self.gamma_diag_regularizer = regularizers.get(gamma_diag_regularizer)
self.gamma_off_regularizer = regularizers.get(gamma_off_regularizer)
self.beta_constraint = constraints .get(beta_constraint)
self.gamma_diag_constraint = constraints .get(gamma_diag_constraint)
self.gamma_off_constraint = constraints .get(gamma_off_constraint)
def build(self, input_shape):
ndim = len(input_shape)
dim = input_shape[self.axis]
if dim is None:
raise ValueError('Axis ' + str(self.axis) + ' of '
'input tensor should have a defined dimension '
'but the layer received an input with shape ' +
str(input_shape) + '.')
self.input_spec = InputSpec(ndim=len(input_shape),
axes={self.axis: dim})
param_shape = (input_shape[self.axis] // 2,)
if self.scale:
self.gamma_rr = self.add_weight(shape=param_shape,
name='gamma_rr',
initializer=self.gamma_diag_initializer,
regularizer=self.gamma_diag_regularizer,
constraint=self.gamma_diag_constraint)
self.gamma_ii = self.add_weight(shape=param_shape,
name='gamma_ii',
initializer=self.gamma_diag_initializer,
regularizer=self.gamma_diag_regularizer,
constraint=self.gamma_diag_constraint)
self.gamma_ri = self.add_weight(shape=param_shape,
name='gamma_ri',
initializer=self.gamma_off_initializer,
regularizer=self.gamma_off_regularizer,
constraint=self.gamma_off_constraint)
self.moving_Vrr = self.add_weight(shape=param_shape,
initializer=self.moving_variance_initializer,
name='moving_Vrr',
trainable=False)
self.moving_Vii = self.add_weight(shape=param_shape,
initializer=self.moving_variance_initializer,
name='moving_Vii',
trainable=False)
self.moving_Vri = self.add_weight(shape=param_shape,
initializer=self.moving_covariance_initializer,
name='moving_Vri',
trainable=False)
else:
self.gamma_rr = None
self.gamma_ii = None
self.gamma_ri = None
self.moving_Vrr = None
self.moving_Vii = None
self.moving_Vri = None
if self.center:
self.beta = self.add_weight(shape=(input_shape[self.axis],),
name='beta',
initializer=self.beta_initializer,
regularizer=self.beta_regularizer,
constraint=self.beta_constraint)
self.moving_mean = self.add_weight(shape=(input_shape[self.axis],),
initializer=self.moving_mean_initializer,
name='moving_mean',
trainable=False)
else:
self.beta = None
self.moving_mean = None
self.built = True
def call(self, inputs, training=None):
input_shape = K.int_shape(inputs)
ndim = len(input_shape)
reduction_axes = list(range(ndim))
del reduction_axes[self.axis]
input_dim = input_shape[self.axis] // 2
mu = K.mean(inputs, axis=reduction_axes)
broadcast_mu_shape = [1] * len(input_shape)
broadcast_mu_shape[self.axis] = input_shape[self.axis]
broadcast_mu = K.reshape(mu, broadcast_mu_shape)
if self.center:
input_centred = inputs - broadcast_mu
else:
input_centred = inputs
centred_squared = input_centred ** 2
if (self.axis == 1 and ndim != 3) or ndim == 2:
centred_squared_real = centred_squared[:, :input_dim]
centred_squared_imag = centred_squared[:, input_dim:]
centred_real = input_centred[:, :input_dim]
centred_imag = input_centred[:, input_dim:]
elif ndim == 3:
centred_squared_real = centred_squared[:, :, :input_dim]
centred_squared_imag = centred_squared[:, :, input_dim:]
centred_real = input_centred[:, :, :input_dim]
centred_imag = input_centred[:, :, input_dim:]
elif self.axis == -1 and ndim == 4:
centred_squared_real = centred_squared[:, :, :, :input_dim]
centred_squared_imag = centred_squared[:, :, :, input_dim:]
centred_real = input_centred[:, :, :, :input_dim]
centred_imag = input_centred[:, :, :, input_dim:]
elif self.axis == -1 and ndim == 5:
centred_squared_real = centred_squared[:, :, :, :, :input_dim]
centred_squared_imag = centred_squared[:, :, :, :, input_dim:]
centred_real = input_centred[:, :, :, :, :input_dim]
centred_imag = input_centred[:, :, :, :, input_dim:]
else:
raise ValueError(
'Incorrect Batchnorm combination of axis and dimensions. axis should be either 1 or -1. '
'axis: ' + str(self.axis) + '; ndim: ' + str(ndim) + '.'
)
if self.scale:
Vrr = K.mean(
centred_squared_real,
axis=reduction_axes
) + self.epsilon
Vii = K.mean(
centred_squared_imag,
axis=reduction_axes
) + self.epsilon
# Vri contains the real and imaginary covariance for each feature map.
Vri = K.mean(
centred_real * centred_imag,
axis=reduction_axes,
)
elif self.center:
Vrr = None
Vii = None
Vri = None
else:
raise ValueError('Error. Both scale and center in batchnorm are set to False.')
input_bn = ComplexBN(
input_centred, Vrr, Vii, Vri,
self.beta, self.gamma_rr, self.gamma_ri,
self.gamma_ii, self.scale, self.center,
axis=self.axis
)
if training in {0, False}:
return input_bn
else:
update_list = []
if self.center:
update_list.append(K.moving_average_update(self.moving_mean, mu, self.momentum))
if self.scale:
update_list.append(K.moving_average_update(self.moving_Vrr, Vrr, self.momentum))
update_list.append(K.moving_average_update(self.moving_Vii, Vii, self.momentum))
update_list.append(K.moving_average_update(self.moving_Vri, Vri, self.momentum))
self.add_update(update_list, inputs)
def normalize_inference():
if self.center:
inference_centred = inputs - K.reshape(self.moving_mean, broadcast_mu_shape)
else:
inference_centred = inputs
return ComplexBN(
inference_centred, self.moving_Vrr, self.moving_Vii,
self.moving_Vri, self.beta, self.gamma_rr, self.gamma_ri,
self.gamma_ii, self.scale, self.center, axis=self.axis
)
# Pick the normalized form corresponding to the training phase.
return K.in_train_phase(input_bn,
normalize_inference,
training=training)
def get_config(self):
config = {
'axis': self.axis,
'momentum': self.momentum,
'epsilon': self.epsilon,
'center': self.center,
'scale': self.scale,
'beta_initializer': sanitizedInitSer(self.beta_initializer),
'gamma_diag_initializer': sanitizedInitSer(self.gamma_diag_initializer),
'gamma_off_initializer': sanitizedInitSer(self.gamma_off_initializer),
'moving_mean_initializer': sanitizedInitSer(self.moving_mean_initializer),
'moving_variance_initializer': sanitizedInitSer(self.moving_variance_initializer),
'moving_covariance_initializer': sanitizedInitSer(self.moving_covariance_initializer),
'beta_regularizer': regularizers.serialize(self.beta_regularizer),
'gamma_diag_regularizer': regularizers.serialize(self.gamma_diag_regularizer),
'gamma_off_regularizer': regularizers.serialize(self.gamma_off_regularizer),
'beta_constraint': constraints .serialize(self.beta_constraint),
'gamma_diag_constraint': constraints .serialize(self.gamma_diag_constraint),
'gamma_off_constraint': constraints .serialize(self.gamma_off_constraint),
}
base_config = super(ComplexBatchNormalization, self).get_config()
return dict(list(base_config.items()) + list(config.items()))