-
Notifications
You must be signed in to change notification settings - Fork 446
/
quaternion_neural_networks.py
647 lines (522 loc) · 24.2 KB
/
quaternion_neural_networks.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
##########################################################
# Quaternion Neural Networks
# Titouan Parcollet, Xinchi Qiu, Mirco Ravanelli
# University of Oxford and Mila, University of Montreal
# May 2020
##########################################################
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch.autograd import Variable
from torch.nn.utils.rnn import PackedSequence
from torch.nn import Module
import numpy as np
from scipy.stats import chi
from numpy.random import RandomState
from distutils.util import strtobool
import math
class QLSTM(nn.Module):
"""
This class implements a straightforward QLSTM as described
in "Quaternion Recurrent Neural Networks", Titouan P., ICLR 2019
Please note that the autograd parameter is usefull if you run out of
VRAM. Set it to False, and the model will use a custom QuaternionLinear
function that follows a custom backpropagation. The training will
be even slower but will consume 4 times less VRAM.
"""
def __init__(self, options,inp_dim):
super(QLSTM, self).__init__()
# Reading parameters
self.input_dim=inp_dim
self.lstm_lay=list(map(int, options['lstm_lay'].split(',')))
self.lstm_drop=list(map(float, options['lstm_drop'].split(',')))
self.lstm_act=options['lstm_act'].split(',')
self.bidir=strtobool(options['lstm_bidir'])
self.use_cuda=strtobool(options['use_cuda'])
self.autograd=strtobool(options['autograd'])
self.to_do=options['to_do']
if self.to_do=='train':
self.test_flag=False
else:
self.test_flag=True
# List initialization
self.wfx = nn.ModuleList([]) # Forget
self.ufh = nn.ModuleList([]) # Forget
self.wix = nn.ModuleList([]) # Input
self.uih = nn.ModuleList([]) # Input
self.wox = nn.ModuleList([]) # Output
self.uoh = nn.ModuleList([]) # Output
self.wcx = nn.ModuleList([]) # Cell state
self.uch = nn.ModuleList([]) # Cell state
self.act = nn.ModuleList([]) # Activations
self.N_lstm_lay=len(self.lstm_lay)
# Initialization of hidden layers
current_input=self.input_dim
for i in range(self.N_lstm_lay):
# Activations
self.act.append(act_fun(self.lstm_act[i]))
add_bias=True
# QuaternionLinearAutograd = Autograd (High VRAM consumption but faster)
# QuaternionLinear = Custom Backward (Low VRAM consumption but slower)
if(self.autograd):
# Feed-forward connections
self.wfx.append(QuaternionLinearAutograd(current_input, self.lstm_lay[i],bias=add_bias))
self.wix.append(QuaternionLinearAutograd(current_input, self.lstm_lay[i],bias=add_bias))
self.wox.append(QuaternionLinearAutograd(current_input, self.lstm_lay[i],bias=add_bias))
self.wcx.append(QuaternionLinearAutograd(current_input, self.lstm_lay[i],bias=add_bias))
# Recurrent connections
self.ufh.append(QuaternionLinearAutograd(self.lstm_lay[i], self.lstm_lay[i],bias=False))
self.uih.append(QuaternionLinearAutograd(self.lstm_lay[i], self.lstm_lay[i],bias=False))
self.uoh.append(QuaternionLinearAutograd(self.lstm_lay[i], self.lstm_lay[i],bias=False))
self.uch.append(QuaternionLinearAutograd(self.lstm_lay[i], self.lstm_lay[i],bias=False))
else:
# Feed-forward connections
self.wfx.append(QuaternionLinear(current_input, self.lstm_lay[i],bias=add_bias))
self.wix.append(QuaternionLinear(current_input, self.lstm_lay[i],bias=add_bias))
self.wox.append(QuaternionLinear(current_input, self.lstm_lay[i],bias=add_bias))
self.wcx.append(QuaternionLinear(current_input, self.lstm_lay[i],bias=add_bias))
# Recurrent connections
self.ufh.append(QuaternionLinear(self.lstm_lay[i], self.lstm_lay[i],bias=False))
self.uih.append(QuaternionLinear(self.lstm_lay[i], self.lstm_lay[i],bias=False))
self.uoh.append(QuaternionLinear(self.lstm_lay[i], self.lstm_lay[i],bias=False))
self.uch.append(QuaternionLinear(self.lstm_lay[i], self.lstm_lay[i],bias=False))
if self.bidir:
current_input=2*self.lstm_lay[i]
else:
current_input=self.lstm_lay[i]
self.out_dim=self.lstm_lay[i]+self.bidir*self.lstm_lay[i]
def forward(self, x):
for i in range(self.N_lstm_lay):
# Initial state and concatenation
if self.bidir:
h_init = torch.zeros(2*x.shape[1], self.lstm_lay[i])
x=torch.cat([x,flip(x,0)],1)
else:
h_init = torch.zeros(x.shape[1],self.lstm_lay[i])
# Drop mask initilization (same mask for all time steps)
if self.test_flag==False:
drop_mask=torch.bernoulli(torch.Tensor(h_init.shape[0],h_init.shape[1]).fill_(1-self.lstm_drop[i]))
else:
drop_mask=torch.FloatTensor([1-self.lstm_drop[i]])
if self.use_cuda:
h_init=h_init.cuda()
drop_mask=drop_mask.cuda()
# Feed-forward affine transformations (all steps in parallel)
wfx_out=self.wfx[i](x)
wix_out=self.wix[i](x)
wox_out=self.wox[i](x)
wcx_out=self.wcx[i](x)
# Processing time steps
hiddens = []
ct=h_init
ht=h_init
for k in range(x.shape[0]):
# LSTM equations
ft=torch.sigmoid(wfx_out[k]+self.ufh[i](ht))
it=torch.sigmoid(wix_out[k]+self.uih[i](ht))
ot=torch.sigmoid(wox_out[k]+self.uoh[i](ht))
ct=it*self.act[i](wcx_out[k]+self.uch[i](ht))*drop_mask+ft*ct
ht=ot*self.act[i](ct)
hiddens.append(ht)
# Stacking hidden states
h=torch.stack(hiddens)
# Bidirectional concatenations
if self.bidir:
h_f=h[:,0:int(x.shape[1]/2)]
h_b=flip(h[:,int(x.shape[1]/2):x.shape[1]].contiguous(),0)
h=torch.cat([h_f,h_b],2)
# Setup x for the next hidden layer
x=h
return x
#
# From this point, the defined functions are PyTorch modules extending
# linear layers to the quaternion domain.
#
class QuaternionLinearAutograd(Module):
r"""Applies a quaternion linear transformation to the incoming data.
The backward process follows the Autograd scheme.
"""
def __init__(self, in_features, out_features, bias=True,
init_criterion='glorot', weight_init='quaternion',
seed=None):
super(QuaternionLinearAutograd, self).__init__()
self.in_features = in_features//4
self.out_features = out_features//4
self.r_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
self.i_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
self.j_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
self.k_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
if bias:
self.bias = Parameter(torch.Tensor(self.out_features*4))
else:
self.bias = torch.zeros(self.out_features*4)
self.init_criterion = init_criterion
self.weight_init = weight_init
self.seed = seed if seed is not None else np.random.randint(0,1234)
self.rng = RandomState(self.seed)
self.reset_parameters()
def reset_parameters(self):
winit = {'quaternion': quaternion_init, 'unitary': unitary_init, 'random': random_init}[self.weight_init]
if self.bias is not None:
self.bias.data.fill_(0)
affect_init(self.r_weight, self.i_weight, self.j_weight, self.k_weight, winit,
self.rng, self.init_criterion)
def forward(self, input):
return quaternion_linear(input, self.r_weight, self.i_weight, self.j_weight, self.k_weight, self.bias)
def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'in_features=' + str(self.in_features) \
+ ', out_features=' + str(self.out_features) \
+ ', bias=' + str(self.bias is not None) \
+ ', init_criterion=' + str(self.init_criterion) \
+ ', weight_init=' + str(self.weight_init) \
+ ', seed=' + str(self.seed) + ')'
class QuaternionLinear(Module):
r"""A custom Autograd function is call to drastically reduce the VRAM consumption.
Nonetheless, computing time is increased compared to QuaternionLinearAutograd().
"""
def __init__(self, in_features, out_features, bias=True,
init_criterion='glorot', weight_init='quaternion',
seed=None):
super(QuaternionLinear, self).__init__()
self.in_features = in_features//4
self.out_features = out_features//4
self.r_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
self.i_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
self.j_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
self.k_weight = Parameter(torch.Tensor(self.in_features, self.out_features))
if bias:
self.bias = Parameter(torch.Tensor(self.out_features*4))
else:
self.register_parameter('bias', None)
self.init_criterion = init_criterion
self.weight_init = weight_init
self.seed = seed if seed is not None else np.random.randint(0,1234)
self.rng = RandomState(self.seed)
self.reset_parameters()
def reset_parameters(self):
winit = {'quaternion': quaternion_init,
'unitary': unitary_init}[self.weight_init]
if self.bias is not None:
self.bias.data.fill_(0)
affect_init(self.r_weight, self.i_weight, self.j_weight, self.k_weight, winit,
self.rng, self.init_criterion)
def forward(self, input):
# See the autograd section for explanation of what happens here.
if input.dim() == 3:
T, N, C = input.size()
input = input.view(T * N, C)
output = QuaternionLinearFunction.apply(input, self.r_weight, self.i_weight, self.j_weight, self.k_weight, self.bias)
output = output.view(T, N, output.size(1))
elif input.dim() == 2:
output = QuaternionLinearFunction.apply(input, self.r_weight, self.i_weight, self.j_weight, self.k_weight, self.bias)
else:
raise NotImplementedError
return output
def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'in_features=' + str(self.in_features) \
+ ', out_features=' + str(self.out_features) \
+ ', bias=' + str(self.bias is not None) \
+ ', init_criterion=' + str(self.init_criterion) \
+ ', weight_init=' + str(self.weight_init) \
+ ', seed=' + str(self.seed) + ')'
#
# Thereafter are utility functions needed by the above classes
#
def flip(x, dim):
xsize = x.size()
dim = x.dim() + dim if dim < 0 else dim
x = x.contiguous()
x = x.view(-1, *xsize[dim:])
x = x.view(x.size(0), x.size(1), -1)[:, getattr(torch.arange(x.size(1)-1, -1, -1), ('cpu','cuda')[x.is_cuda])().long(), :]
return x.view(xsize)
def act_fun(act_type):
if act_type=="relu":
return nn.ReLU()
if act_type=="prelu":
return nn.PReLU()
if act_type=="tanh":
return nn.Tanh()
if act_type=="sigmoid":
return nn.Sigmoid()
if act_type=="hardtanh":
return nn.Hardtanh()
if act_type=="leaky_relu":
return nn.LeakyReLU(0.2)
if act_type=="elu":
return nn.ELU()
if act_type=="softmax":
return nn.LogSoftmax(dim=1)
if act_type=="linear":
return nn.LeakyReLU(1) # initializzed like this, but not used in forward!
def check_input(input):
if input.dim() not in {2, 3}:
raise RuntimeError(
"quaternion linear accepts only input of dimension 2 or 3."
" input.dim = " + str(input.dim())
)
nb_hidden = input.size()[-1]
if nb_hidden % 4 != 0:
raise RuntimeError(
"Quaternion Tensors must be divisible by 4."
" input.size()[1] = " + str(nb_hidden)
)
#
# Quaternion getters!
#
def get_r(input):
check_input(input)
nb_hidden = input.size()[-1]
if input.dim() == 2:
return input.narrow(1, 0, nb_hidden // 4)
elif input.dim() == 3:
return input.narrow(2, 0, nb_hidden // 4)
def get_i(input):
check_input(input)
nb_hidden = input.size()[-1]
if input.dim() == 2:
return input.narrow(1, nb_hidden // 4, nb_hidden // 4)
if input.dim() == 3:
return input.narrow(2, nb_hidden // 4, nb_hidden // 4)
def get_j(input):
check_input(input)
nb_hidden = input.size()[-1]
if input.dim() == 2:
return input.narrow(1, nb_hidden // 2, nb_hidden // 4)
if input.dim() == 3:
return input.narrow(2, nb_hidden // 2, nb_hidden // 4)
def get_k(input):
check_input(input)
nb_hidden = input.size()[-1]
if input.dim() == 2:
return input.narrow(1, nb_hidden - nb_hidden // 4, nb_hidden // 4)
if input.dim() == 3:
return input.narrow(2, nb_hidden - nb_hidden // 4, nb_hidden // 4)
def quaternion_linear(input, r_weight, i_weight, j_weight, k_weight, bias):
"""
Applies a quaternion linear transformation to the incoming data:
It is important to notice that the forward phase of a QNN is defined
as W * Inputs (with * equal to the Hamilton product). The constructed
cat_kernels_4_quaternion is a modified version of the quaternion representation
so when we do torch.mm(Input,W) it's equivalent to W * Inputs.
"""
cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=0)
cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0)
cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=0)
cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0)
cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=1)
if input.dim() == 2 :
if bias is not None:
return torch.addmm(bias, input, cat_kernels_4_quaternion)
else:
return torch.mm(input, cat_kernels_4_quaternion)
else:
output = torch.matmul(input, cat_kernels_4_quaternion)
if bias is not None:
return output+bias
else:
return output
# Custom AUTOGRAD for lower VRAM consumption
class QuaternionLinearFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, r_weight, i_weight, j_weight, k_weight, bias=None):
ctx.save_for_backward(input, r_weight, i_weight, j_weight, k_weight, bias)
check_input(input)
cat_kernels_4_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=0)
cat_kernels_4_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0)
cat_kernels_4_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=0)
cat_kernels_4_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0)
cat_kernels_4_quaternion = torch.cat([cat_kernels_4_r, cat_kernels_4_i, cat_kernels_4_j, cat_kernels_4_k], dim=1)
if input.dim() == 2 :
if bias is not None:
return torch.addmm(bias, input, cat_kernels_4_quaternion)
else:
return torch.mm(input, cat_kernels_4_quaternion)
else:
output = torch.matmul(input, cat_kernels_4_quaternion)
if bias is not None:
return output+bias
else:
return output
# This function has only a single output, so it gets only one gradient
@staticmethod
def backward(ctx, grad_output):
input, r_weight, i_weight, j_weight, k_weight, bias = ctx.saved_tensors
grad_input = grad_weight_r = grad_weight_i = grad_weight_j = grad_weight_k = grad_bias = None
input_r = torch.cat([r_weight, -i_weight, -j_weight, -k_weight], dim=0)
input_i = torch.cat([i_weight, r_weight, -k_weight, j_weight], dim=0)
input_j = torch.cat([j_weight, k_weight, r_weight, -i_weight], dim=0)
input_k = torch.cat([k_weight, -j_weight, i_weight, r_weight], dim=0)
cat_kernels_4_quaternion_T = Variable(torch.cat([input_r, input_i, input_j, input_k], dim=1).permute(1,0), requires_grad=False)
r = get_r(input)
i = get_i(input)
j = get_j(input)
k = get_k(input)
input_r = torch.cat([r, -i, -j, -k], dim=0)
input_i = torch.cat([i, r, -k, j], dim=0)
input_j = torch.cat([j, k, r, -i], dim=0)
input_k = torch.cat([k, -j, i, r], dim=0)
input_mat = Variable(torch.cat([input_r, input_i, input_j, input_k], dim=1), requires_grad=False)
r = get_r(grad_output)
i = get_i(grad_output)
j = get_j(grad_output)
k = get_k(grad_output)
input_r = torch.cat([r, i, j, k], dim=1)
input_i = torch.cat([-i, r, k, -j], dim=1)
input_j = torch.cat([-j, -k, r, i], dim=1)
input_k = torch.cat([-k, j, -i, r], dim=1)
grad_mat = torch.cat([input_r, input_i, input_j, input_k], dim=0)
if ctx.needs_input_grad[0]:
grad_input = grad_output.mm(cat_kernels_4_quaternion_T)
if ctx.needs_input_grad[1]:
grad_weight = grad_mat.permute(1,0).mm(input_mat).permute(1,0)
unit_size_x = r_weight.size(0)
unit_size_y = r_weight.size(1)
grad_weight_r = grad_weight.narrow(0,0,unit_size_x).narrow(1,0,unit_size_y)
grad_weight_i = grad_weight.narrow(0,0,unit_size_x).narrow(1,unit_size_y,unit_size_y)
grad_weight_j = grad_weight.narrow(0,0,unit_size_x).narrow(1,unit_size_y*2,unit_size_y)
grad_weight_k = grad_weight.narrow(0,0,unit_size_x).narrow(1,unit_size_y*3,unit_size_y)
if ctx.needs_input_grad[5]:
grad_bias = grad_output.sum(0).squeeze(0)
return grad_input, grad_weight_r, grad_weight_i, grad_weight_j, grad_weight_k, grad_bias
#
# PARAMETERS INITIALIZATION
#
def unitary_init(in_features, out_features, rng, kernel_size=None, criterion='he'):
if kernel_size is not None:
receptive_field = np.prod(kernel_size)
fan_in = in_features * receptive_field
fan_out = out_features * receptive_field
else:
fan_in = in_features
fan_out = out_features
if criterion == 'glorot':
s = 1. / np.sqrt(2*(fan_in + fan_out))
elif criterion == 'he':
s = 1. / np.sqrt(2*fan_in)
else:
raise ValueError('Invalid criterion: ' + criterion)
if kernel_size is None:
kernel_shape = (in_features, out_features)
else:
if type(kernel_size) is int:
kernel_shape = (out_features, in_features) + tuple((kernel_size,))
else:
kernel_shape = (out_features, in_features) + (*kernel_size,)
s = np.sqrt(3.0) * s
number_of_weights = np.prod(kernel_shape)
v_r = np.random.uniform(-s,s,number_of_weights)
v_i = np.random.uniform(-s,s,number_of_weights)
v_j = np.random.uniform(-s,s,number_of_weights)
v_k = np.random.uniform(-s,s,number_of_weights)
# Unitary quaternion
for i in range(0, number_of_weights):
norm = np.sqrt(v_r[i]**2 + v_i[i]**2 + v_j[i]**2 + v_k[i]**2)+0.0001
v_r[i]/= norm
v_i[i]/= norm
v_j[i]/= norm
v_k[i]/= norm
v_r = v_r.reshape(kernel_shape)
v_i = v_i.reshape(kernel_shape)
v_j = v_j.reshape(kernel_shape)
v_k = v_k.reshape(kernel_shape)
return (v_r, v_i, v_j, v_k)
def random_init(in_features, out_features, rng, kernel_size=None, criterion='glorot'):
if kernel_size is not None:
receptive_field = np.prod(kernel_size)
fan_in = in_features * receptive_field
fan_out = out_features * receptive_field
else:
fan_in = in_features
fan_out = out_features
if criterion == 'glorot':
s = 1. / np.sqrt(2*(fan_in + fan_out))
elif criterion == 'he':
s = 1. / np.sqrt(2*fan_in)
else:
raise ValueError('Invalid criterion: ' + criterion)
if kernel_size is None:
kernel_shape = (in_features, out_features)
else:
if type(kernel_size) is int:
kernel_shape = (out_features, in_features) + tuple((kernel_size,))
else:
kernel_shape = (out_features, in_features) + (*kernel_size,)
number_of_weights = np.prod(kernel_shape)
v_r = np.random.uniform(0.0,1.0,number_of_weights)
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)
v_r = v_r.reshape(kernel_shape)
v_i = v_i.reshape(kernel_shape)
v_j = v_j.reshape(kernel_shape)
v_k = v_k.reshape(kernel_shape)
weight_r = v_r * s
weight_i = v_i * s
weight_j = v_j * s
weight_k = v_k * s
return (weight_r, weight_i, weight_j, weight_k)
def quaternion_init(in_features, out_features, rng, kernel_size=None, criterion='glorot'):
if kernel_size is not None:
receptive_field = np.prod(kernel_size)
fan_in = in_features * receptive_field
fan_out = out_features * receptive_field
else:
fan_in = in_features
fan_out = out_features
if criterion == 'glorot':
s = 1. / np.sqrt(2*(fan_in + fan_out))
elif criterion == 'he':
s = 1. / np.sqrt(2*fan_in)
else:
raise ValueError('Invalid criterion: ' + criterion)
rng = RandomState(np.random.randint(1,1234))
# Generating randoms and purely imaginary quaternions :
if kernel_size is None:
kernel_shape = (in_features, out_features)
else:
if type(kernel_size) is int:
kernel_shape = (out_features, in_features) + tuple((kernel_size,))
else:
kernel_shape = (out_features, in_features) + (*kernel_size,)
modulus = chi.rvs(4,loc=0,scale=s,size=kernel_shape)
number_of_weights = np.prod(kernel_shape)
v_i = np.random.normal(0,1.0,number_of_weights)
v_j = np.random.normal(0,1.0,number_of_weights)
v_k = np.random.normal(0,1.0,number_of_weights)
# 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 +0.0001)
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)
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)
return (weight_r, weight_i, weight_j, weight_k)
def affect_init(r_weight, i_weight, j_weight, k_weight, init_func, rng, init_criterion):
if r_weight.size() != i_weight.size() or r_weight.size() != j_weight.size() or \
r_weight.size() != k_weight.size() :
raise ValueError('The real and imaginary weights '
'should have the same size . Found: r:'
+ str(r_weight.size()) +' i:'
+ str(i_weight.size()) +' j:'
+ str(j_weight.size()) +' k:'
+ str(k_weight.size()))
elif r_weight.dim() != 2:
raise Exception('affect_init accepts only matrices. Found dimension = '
+ str(r_weight.dim()))
kernel_size = None
r, i, j, k = init_func(r_weight.size(0), r_weight.size(1), rng, kernel_size, init_criterion)
r, i, j, k = torch.from_numpy(r), torch.from_numpy(i), torch.from_numpy(j), torch.from_numpy(k)
r_weight.data = r.type_as(r_weight.data)
i_weight.data = i.type_as(i_weight.data)
j_weight.data = j.type_as(j_weight.data)
k_weight.data = k.type_as(k_weight.data)