Blame view

complexnn/init.py 15.8 KB
8a1d43c41   Parcollet Titouan   V1
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
  #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  # Contributors: Titouan Parcollet
  # Authors: Chiheb Trabelsi
  
  import numpy as np
  from numpy.random import RandomState
  from random import gauss
  import keras.backend as K
  from keras import initializers
  from keras.initializers import Initializer
  from keras.utils.generic_utils import (serialize_keras_object,
  		deserialize_keras_object)
  
  
  #####################################################################
  #                   Quaternion Implementations                      #
  #####################################################################
  
  class QuaternionIndependentFilters(Initializer):
  	# This initialization constructs quaternion-valued kernels
  	# that are independent as much as possible from each other
  	# while respecting either the He or the Glorot criterion.
  	def __init__(self, kernel_size, input_dim,
  			weight_dim, nb_filters=None,
  			criterion='glorot', seed=None):
  
  		# `weight_dim` is used as a parameter for sanity check
  		# as we should not pass an integer as kernel_size when
  		# the weight dimension is >= 2.
  		# nb_filters == 0 if weights are not convolutional (matrix instead of filters)
  		# then in such a case, weight_dim = 2.
  		# (in case of 2D input):
  		#     nb_filters == None and len(kernel_size) == 2 and_weight_dim == 2
  		# conv1D: len(kernel_size) == 1 and weight_dim == 1
  		# conv2D: len(kernel_size) == 2 and weight_dim == 2
  		# conv3d: len(kernel_size) == 3 and weight_dim == 3
  
  		assert len(kernel_size) == weight_dim and weight_dim in {0, 1, 2, 3}
  		self.nb_filters = nb_filters
  		self.kernel_size = kernel_size
  		self.input_dim = input_dim
  		self.weight_dim = weight_dim
  		self.criterion = criterion
  		self.seed = 1337 if seed is None else seed
  
  	def __call__(self, shape, dtype=None):
  
  		if self.nb_filters is not None:
  			num_rows = self.nb_filters * self.input_dim
  			num_cols = np.prod(self.kernel_size)
  		else:
  			# in case it is the kernel is a matrix and not a filter
  			# which is the case of 2D input (No feature maps).
  			num_rows = self.input_dim
  			num_cols = self.kernel_size[-1]
  
  		flat_shape = (int(num_rows), int(num_cols))
  		rng = RandomState(self.seed)
  		r = rng.uniform(size=flat_shape)
  		i = rng.uniform(size=flat_shape)
  		z = r + 1j * i
  		u, _, v = np.linalg.svd(z)
  		unitary_z = np.dot(u, np.dot(np.eye(int(num_rows), int(num_cols)), np.conjugate(v).T))
  		real_unitary = unitary_z.real
  		imag_unitary = unitary_z.imag
  		if self.nb_filters is not None:
  			indep_real = np.reshape(real_unitary, (num_rows,) + tuple(self.kernel_size))
  			indep_imag = np.reshape(imag_unitary, (num_rows,) + tuple(self.kernel_size))
  			fan_in, fan_out = initializers._compute_fans(
  					tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
  					)
  		else:
  			indep_real = real_unitary
  			indep_imag = imag_unitary
  			fan_in, fan_out = (int(self.input_dim), self.kernel_size[-1])
  
  		if self.criterion == 'glorot':
  			desired_var = 1. / (fan_in + fan_out)
  		elif self.criterion == 'he':
  			desired_var = 1. / (fan_in)
  		else:
  			raise ValueError('Invalid criterion: ' + self.criterion)
  
  		multip_real = np.sqrt(desired_var / np.var(indep_real))
  		multip_imag = np.sqrt(desired_var / np.var(indep_imag))
  		scaled_real = multip_real * indep_real
  		scaled_imag = multip_imag * indep_imag
  
  		if self.weight_dim == 2 and self.nb_filters is None:
  			weight_real = scaled_real
  			weight_imag = scaled_imag
  		else:
  			kernel_shape = tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
  			if self.weight_dim == 1:
  				transpose_shape = (1, 0)
  			elif self.weight_dim == 2 and self.nb_filters is not None:
  				transpose_shape = (1, 2, 0)
  			elif self.weight_dim == 3 and self.nb_filters is not None:
  				transpose_shape = (1, 2, 3, 0)
  
  			weight_real = np.transpose(scaled_real, transpose_shape)
  			weight_imag = np.transpose(scaled_imag, transpose_shape)
  			weight_real = np.reshape(weight_real, kernel_shape)
  			weight_imag = np.reshape(weight_imag, kernel_shape)
  		weight = np.concatenate([weight_real, weight_imag], axis=-1)
  
  		return weight
  
  	def get_config(self):
  		return {'nb_filters': self.nb_filters,
  				'kernel_size': self.kernel_size,
  				'input_dim': self.input_dim,
  				'weight_dim': self.weight_dim,
  				'criterion': self.criterion,
  				'seed': self.seed}
  
  
  class QuaternionInit(Initializer):
  	# The standard complex initialization using
  	# either the He or the Glorot criterion.
  	def __init__(self, kernel_size, input_dim,
  			weight_dim, nb_filters=None,
  			criterion='glorot', seed=None):
  
  		# `weight_dim` is used as a parameter for sanity check
  		# as we should not pass an integer as kernel_size when
  		# the weight dimension is >= 2.
  		# nb_filters == 0 if weights are not convolutional (matrix instead of filters)
  		# then in such a case, weight_dim = 2.
  		# (in case of 2D input):
  		#     nb_filters == None and len(kernel_size) == 2 and_weight_dim == 2
  		# conv1D: len(kernel_size) == 1 and weight_dim == 1
  		# conv2D: len(kernel_size) == 2 and weight_dim == 2
  		# conv3d: len(kernel_size) == 3 and weight_dim == 3
  
  		assert len(kernel_size) == weight_dim and weight_dim in {0, 1, 2, 3}
  		self.nb_filters = nb_filters
  		self.kernel_size = kernel_size
  		self.input_dim = input_dim
  		self.weight_dim = weight_dim
  		self.criterion = criterion
  		self.seed = 1337 if seed is None else seed
  
  	def __call__(self, shape, dtype=None):
  
  		if self.nb_filters is not None:
  			kernel_shape = tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
  		else:
  			kernel_shape = (int(self.input_dim), self.kernel_size[-1])
  
  		fan_in, fan_out = initializers._compute_fans(
  				tuple(self.kernel_size) + (self.input_dim, self.nb_filters)
  				)
  
  		# Quaternion operations start here
  
  		if self.criterion == 'glorot':
  			s = 1. / np.sqrt(2*(fan_in + fan_out))
  		elif self.criterion == 'he':
  			s = 1. / np.sqrt(2*fan_in)
  		else:
  			raise ValueError('Invalid criterion: ' + self.criterion)
  
  		#Generating randoms and purely imaginary quaternions :
  		number_of_weights = np.prod(kernel_shape) 
  		v_i = np.random.uniform(0.0,1.0,number_of_weights)
  		v_j = np.random.uniform(0.0,1.0,number_of_weights)
  		v_k = np.random.uniform(0.0,1.0,number_of_weights)
  		#Make these purely imaginary quaternions unitary
  		for i in range(0, number_of_weights):
  			norm = np.sqrt(v_i[i]**2 + v_j[i]**2 + v_k[i]**2)
  			v_i[i]/= norm
  			v_j[i]/= norm
  			v_k[i]/= norm
  		v_i = v_i.reshape(kernel_shape)
  		v_j = v_j.reshape(kernel_shape)
  		v_k = v_k.reshape(kernel_shape)
  
  		rng = RandomState(self.seed)
  		modulus = rng.rayleigh(scale=s, size=kernel_shape)
  		phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape)
  		
  		weight_r = modulus * np.cos(phase)
  		weight_i = modulus * v_i*np.sin(phase)
  		weight_j = modulus * v_j*np.sin(phase)
  		weight_k = modulus * v_k*np.sin(phase)
  		weight = np.concatenate([weight_r, weight_i, weight_j, weight_k], axis=-1)
  
  		return weight
  
  
  #####################################################################
  #                     Complex Implementations                       #
  #####################################################################
  
  
  class IndependentFilters(Initializer):
  	# This initialization constructs real-valued kernels
  	# that are independent as much as possible from each other
  	# while respecting either the He or the Glorot criterion. 
  	def __init__(self, kernel_size, input_dim,
  			weight_dim, nb_filters=None,
  			criterion='glorot', seed=None):
  
  		# `weight_dim` is used as a parameter for sanity check
  		# as we should not pass an integer as kernel_size when
  		# the weight dimension is >= 2.
  		# nb_filters == 0 if weights are not convolutional (matrix instead of filters)
  		# then in such a case, weight_dim = 2.
  		# (in case of 2D input):
  		#     nb_filters == None and len(kernel_size) == 2 and_weight_dim == 2
  		# conv1D: len(kernel_size) == 1 and weight_dim == 1
  		# conv2D: len(kernel_size) == 2 and weight_dim == 2
  		# conv3d: len(kernel_size) == 3 and weight_dim == 3
  
  		assert len(kernel_size) == weight_dim and weight_dim in {0, 1, 2, 3}
  		self.nb_filters = nb_filters
  		self.kernel_size = kernel_size
  		self.input_dim = input_dim
  		self.weight_dim = weight_dim
  		self.criterion = criterion
  		self.seed = 1337 if seed is None else seed
  
  	def __call__(self, shape, dtype=None):
  
  		if self.nb_filters is not None:
  			num_rows = self.nb_filters * self.input_dim
  			num_cols = np.prod(self.kernel_size)
  		else:
  			# in case it is the kernel is a matrix and not a filter
  			# which is the case of 2D input (No feature maps).
  			num_rows = self.input_dim
  			num_cols = self.kernel_size[-1]
  
  		flat_shape = (num_rows, num_cols)
  		rng = RandomState(self.seed)
  		x = rng.uniform(size=flat_shape)
  		u, _, v = np.linalg.svd(x)
  		orthogonal_x = np.dot(u, np.dot(np.eye(num_rows, num_cols), v.T))
  		if self.nb_filters is not None:
  			independent_filters = np.reshape(orthogonal_x, (num_rows,) + tuple(self.kernel_size))
  			fan_in, fan_out = initializers._compute_fans(
  					tuple(self.kernel_size) + (self.input_dim, self.nb_filters)
  					)
  		else:
  			independent_filters = orthogonal_x
  			fan_in, fan_out = (self.input_dim, self.kernel_size[-1])
  
  		if self.criterion == 'glorot':
  			desired_var = 2. / (fan_in + fan_out)
  		elif self.criterion == 'he':
  			desired_var = 2. / fan_in
  		else:
  			raise ValueError('Invalid criterion: ' + self.criterion)
  
  		multip_constant = np.sqrt (desired_var / np.var(independent_filters))
  		scaled_indep = multip_constant * independent_filters
  
  		if self.weight_dim == 2 and self.nb_filters is None:
  			weight_real = scaled_real
  			weight_imag = scaled_imag
  		else:
  			kernel_shape = tuple(self.kernel_size) + (self.input_dim, self.nb_filters)
  			if self.weight_dim == 1:
  				transpose_shape = (1, 0)
  			elif self.weight_dim == 2 and self.nb_filters is not None:
  				transpose_shape = (1, 2, 0)
  			elif self.weight_dim == 3 and self.nb_filters is not None:
  				transpose_shape = (1, 2, 3, 0)
  			weight = np.transpose(scaled_indep, transpose_shape)
  			weight = np.reshape(weight, kernel_shape)
  
  		return weight
  
  	def get_config(self):
  		return {'nb_filters': self.nb_filters,
  				'kernel_size': self.kernel_size,
  				'input_dim': self.input_dim,
  				'weight_dim': self.weight_dim,
  				'criterion': self.criterion,
  				'seed': self.seed}
  
  
  class ComplexIndependentFilters(Initializer):
  	# This initialization constructs complex-valued kernels
  	# that are independent as much as possible from each other
  	# while respecting either the He or the Glorot criterion.
  	def __init__(self, kernel_size, input_dim,
  			weight_dim, nb_filters=None,
  			criterion='glorot', seed=None):
  
  		# `weight_dim` is used as a parameter for sanity check
  		# as we should not pass an integer as kernel_size when
  		# the weight dimension is >= 2.
  		# nb_filters == 0 if weights are not convolutional (matrix instead of filters)
  		# then in such a case, weight_dim = 2.
  		# (in case of 2D input):
  		#     nb_filters == None and len(kernel_size) == 2 and_weight_dim == 2
  		# conv1D: len(kernel_size) == 1 and weight_dim == 1
  		# conv2D: len(kernel_size) == 2 and weight_dim == 2
  		# conv3d: len(kernel_size) == 3 and weight_dim == 3
  
  		assert len(kernel_size) == weight_dim and weight_dim in {0, 1, 2, 3}
  		self.nb_filters = nb_filters
  		self.kernel_size = kernel_size
  		self.input_dim = input_dim
  		self.weight_dim = weight_dim
  		self.criterion = criterion
  		self.seed = 1337 if seed is None else seed
  
  	def __call__(self, shape, dtype=None):
  
  		if self.nb_filters is not None:
  			num_rows = self.nb_filters * self.input_dim
  			num_cols = np.prod(self.kernel_size)
  		else:
  			# in case it is the kernel is a matrix and not a filter
  			# which is the case of 2D input (No feature maps).
  			num_rows = self.input_dim
  			num_cols = self.kernel_size[-1]
  
  		flat_shape = (int(num_rows), int(num_cols))
  		rng = RandomState(self.seed)
  		r = rng.uniform(size=flat_shape)
  		i = rng.uniform(size=flat_shape)
  		z = r + 1j * i
  		u, _, v = np.linalg.svd(z)
  		unitary_z = np.dot(u, np.dot(np.eye(int(num_rows), int(num_cols)), np.conjugate(v).T))
  		real_unitary = unitary_z.real
  		imag_unitary = unitary_z.imag
  		if self.nb_filters is not None:
  			indep_real = np.reshape(real_unitary, (num_rows,) + tuple(self.kernel_size))
  			indep_imag = np.reshape(imag_unitary, (num_rows,) + tuple(self.kernel_size))
  			fan_in, fan_out = initializers._compute_fans(
  					tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
  					)
  		else:
  			indep_real = real_unitary
  			indep_imag = imag_unitary
  			fan_in, fan_out = (int(self.input_dim), self.kernel_size[-1])
  
  		if self.criterion == 'glorot':
  			desired_var = 1. / (fan_in + fan_out)
  		elif self.criterion == 'he':
  			desired_var = 1. / (fan_in)
  		else:
  			raise ValueError('Invalid criterion: ' + self.criterion)
  
  		multip_real = np.sqrt(desired_var / np.var(indep_real))
  		multip_imag = np.sqrt(desired_var / np.var(indep_imag))
  		scaled_real = multip_real * indep_real
  		scaled_imag = multip_imag * indep_imag
  
  		if self.weight_dim == 2 and self.nb_filters is None:
  			weight_real = scaled_real
  			weight_imag = scaled_imag
  		else:
  			kernel_shape = tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
  			if self.weight_dim == 1:
  				transpose_shape = (1, 0)
  			elif self.weight_dim == 2 and self.nb_filters is not None:
  				transpose_shape = (1, 2, 0)
  			elif self.weight_dim == 3 and self.nb_filters is not None:
  				transpose_shape = (1, 2, 3, 0)
  
  			weight_real = np.transpose(scaled_real, transpose_shape)
  			weight_imag = np.transpose(scaled_imag, transpose_shape)
  			weight_real = np.reshape(weight_real, kernel_shape)
  			weight_imag = np.reshape(weight_imag, kernel_shape)
  		weight = np.concatenate([weight_real, weight_imag], axis=-1)
  
  		return weight
  
  	def get_config(self):
  		return {'nb_filters': self.nb_filters,
  				'kernel_size': self.kernel_size,
  				'input_dim': self.input_dim,
  				'weight_dim': self.weight_dim,
  				'criterion': self.criterion,
  				'seed': self.seed}
  
  
  class ComplexInit(Initializer):
  	# The standard complex initialization using
  	# either the He or the Glorot criterion.
  	def __init__(self, kernel_size, input_dim,
  			weight_dim, nb_filters=None,
  			criterion='glorot', seed=None):
  
  		# `weight_dim` is used as a parameter for sanity check
  		# as we should not pass an integer as kernel_size when
  		# the weight dimension is >= 2.
  		# nb_filters == 0 if weights are not convolutional (matrix instead of filters)
  		# then in such a case, weight_dim = 2.
  		# (in case of 2D input):
  		#     nb_filters == None and len(kernel_size) == 2 and_weight_dim == 2
  		# conv1D: len(kernel_size) == 1 and weight_dim == 1
  		# conv2D: len(kernel_size) == 2 and weight_dim == 2
  		# conv3d: len(kernel_size) == 3 and weight_dim == 3
  
  		assert len(kernel_size) == weight_dim and weight_dim in {0, 1, 2, 3}
  		self.nb_filters = nb_filters
  		self.kernel_size = kernel_size
  		self.input_dim = input_dim
  		self.weight_dim = weight_dim
  		self.criterion = criterion
  		self.seed = 1337 if seed is None else seed
  
  	def __call__(self, shape, dtype=None):
  
  		if self.nb_filters is not None:
  			kernel_shape = tuple(self.kernel_size) + (int(self.input_dim), self.nb_filters)
  		else:
  			kernel_shape = (int(self.input_dim), self.kernel_size[-1])
  
  		fan_in, fan_out = initializers._compute_fans(
  				tuple(self.kernel_size) + (self.input_dim, self.nb_filters)
  				)
  
  		if self.criterion == 'glorot':
  			s = 1. / (fan_in + fan_out)
  		elif self.criterion == 'he':
  			s = 1. / fan_in
  		else:
  			raise ValueError('Invalid criterion: ' + self.criterion)
  		rng = RandomState(self.seed)
  		modulus = rng.rayleigh(scale=s, size=kernel_shape)
  		phase = rng.uniform(low=-np.pi, high=np.pi, size=kernel_shape)
  		weight_real = modulus * np.cos(phase)
  		weight_imag = modulus * np.sin(phase)
  		#print (weight_real.shape)
  		weight = np.concatenate([weight_real, weight_imag], axis=-1)
  
  		return weight
  
  
  class SqrtInit(Initializer):
  	def __call__(self, shape, dtype=None):
  		return K.constant(1 / K.sqrt(2), shape=shape, dtype=dtype)
  
  
  # Aliases:
  sqrt_init = SqrtInit
  independent_filters = IndependentFilters
  quaternion_independent_filters = QuaternionIndependentFilters
  complex_init = ComplexInit
  quaternion_init = QuaternionInit