-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathquaternionops.py
394 lines (332 loc) · 15 KB
/
quaternionops.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
from numpy.random import RandomState
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)
)
#
# 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 get_modulus(input, vector_form=False):
check_input(input)
r = get_r(input)
i = get_i(input)
j = get_j(input)
k = get_k(input)
if vector_form:
return torch.sqrt(r * r + i * i + j * j + k * k)
else:
return torch.sqrt((r * r + i * i + j * j + k * k).sum(dim=0))
def get_normalized(input, eps=0.0001):
check_input(input)
data_modulus = get_modulus(input)
if input.dim() == 2:
data_modulus_repeated = data_modulus.repeat(1, 4)
elif input.dim() == 3:
data_modulus_repeated = data_modulus.repeat(1, 1, 4)
return input / (data_modulus_repeated.expand_as(input) + eps)
def quaternion_linear(input, r_weight, i_weight, j_weight, k_weight, bias=True):
"""
Applies a quaternion linear transformation to the incoming data:
Shape:
- Input: (batch_size, nb_quaternion_elements_in * 4)
- real_weight: (nb_quaternion_elements, nb_quaternion_elements_out)
- imag_weight: (nb_quaternion_elements, nb_quaternion_elements_out)
- Bias: (nb_quaternion_elements_out * 4)
- Output: (batch_size, nb_quaternion_elements_out * 4)
code:
"""
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
# Inherit from Function
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
class LinearFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, bias=None):
ctx.save_for_backward(input, weight, bias)
if input.dim()==3:
output = input.matmul(weight.t())
if bias is not None:
output += bias.unsqueeze(0).expand_as(output)
return output
else:
output = input.mm(weight.t())
if bias is not None:
output += bias.unsqueeze(0).expand_as(output)
return output
@staticmethod
def backward(ctx, grad_output):
input, weight, bias = ctx.saved_tensors
grad_input = grad_weight = grad_bias = None
weight = Variable(weight, requires_grad=False)
input = Variable(input, requires_grad=False)
if input.dim() == 3:
if ctx.needs_input_grad[0]:
grad_input = grad_output.matmul(weight).sum(0)
if ctx.needs_input_grad[1]:
grad_weight = grad_output.permute(0,2,1).matmul(input).sum(0)
if bias is not None and ctx.needs_input_grad[2]:
grad_bias = grad_output.sum(1).sum(0).squeeze(0)
else:
if ctx.needs_input_grad[0]:
grad_input = grad_output.mm(weight)
if ctx.needs_input_grad[1]:
grad_weight = grad_output.t().mm(input)
if bias is not None and ctx.needs_input_grad[2]:
grad_bias = grad_output.sum(0).squeeze(0)
return grad_input, grad_weight, grad_bias
def hamilton_product(q0, q1):
"""
Applies a Hamilton product q0 * q1:
Shape:
- q0, q1 should be (batch_size, quaternion_number)
(rr' - xx' - yy' - zz') +
(rx' + xr' + yz' - zy')i +
(ry' - xz' + yr' + zx')j +
(rz' + xy' - yx' + zr')k +
"""
q1_r = get_r(q1)
q1_i = get_i(q1)
q1_j = get_j(q1)
q1_k = get_k(q1)
# rr', xx', yy', and zz'
r_base = torch.mul(q0, q1)
# (rr' - xx' - yy' - zz')
r = get_r(r_base) - get_i(r_base) - get_j(r_base) - get_k(r_base)
# rx', xr', yz', and zy'
i_base = torch.mul(q0, torch.cat([q1_i, q1_r, q1_k, q1_j], dim=1))
# (rx' + xr' + yz' - zy')
i = get_r(i_base) + get_i(i_base) + get_j(i_base) - get_k(i_base)
# ry', xz', yr', and zx'
j_base = torch.mul(q0, torch.cat([q1_j, q1_k, q1_r, q1_i], dim=1))
# (rx' + xr' + yz' - zy')
j = get_r(j_base) - get_i(j_base) + get_j(j_base) + get_k(j_base)
# rz', xy', yx', and zr'
k_base = torch.mul(q0, torch.cat([q1_k, q1_j, q1_i, q1_r], dim=1))
# (rx' + xr' + yz' - zy')
k = get_r(k_base) + get_i(k_base) - get_j(k_base) + get_k(k_base)
return torch.cat([r, i, j, k], dim=1)
def unitary_init(in_features, out_features, rng, criterion='glorot'):
if criterion == 'glorot':
s = 1. / np.sqrt(2*(in_features + out_features))
elif criterion == 'he':
s = 1. / np.sqrt(2*in_features)
else:
raise ValueError('Invalid criterion: ' + criterion)
kernel_shape = (in_features, out_features)
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)
# 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)
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, criterion='glorot'):
if criterion == 'glorot':
s = 1. / np.sqrt(2*(in_features + out_features))
elif criterion == 'he':
s = 1. / np.sqrt(2*in_features)
else:
raise ValueError('Invalid criterion: ' + criterion)
rng = RandomState(123)
# Generating randoms and purely imaginary quaternions :
kernel_shape = (in_features, out_features)
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)
# 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)
modulus = rng.uniform(low=-s, high=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)
return (weight_r, weight_i, weight_j, weight_k)
def create_dropout_mask(dropout_p, size, rng, as_type, operation='linear'):
if operation == 'linear':
mask = rng.binomial(n=1, p=1-dropout_p, size=size)
return Variable(torch.from_numpy(mask).type(as_type))
else:
raise Exception("create_dropout_mask accepts only 'linear'. Found operation = "
+ str(operation))
def apply_quaternion_mask(input, mask, dropout_type='quaternion', operation='linear'):
if dropout_type == 'quaternion':
input_r_masked = get_real(input, input_type=operation) * mask
input_i_masked = get_imag(input, input_type=operation) * mask
input_j_masked = get_imag(input, input_type=operation) * mask
input_k_masked = get_imag(input, input_type=operation) * mask
return torch.cat([input_r_masked, input_i_masked, input_j_masked, input_k_masked], dim=1)
elif dropout_type == 'regular':
return input * mask
else:
raise Exception("dropout_type accepts only 'complex' or 'regular'. Found dropout_type = "
+ str(dropout_type))
def apply_quaternion_dropout(input, dropout_p, rng, do_dropout=True, dropout_type='quaternion', operation='linear'):
size = input.data.size()
s = []
for i in range(input.dim()):
s.append(size[i])
if dropout_type == 'quaternion':
s[1] = s[1] // 4
elif dropout_type != 'regular':
raise Exception("dropout_type accepts only 'quaternion' or 'regular'. Found dropout_type = "
+ str(dropout_type))
s = tuple(s)
mask = create_dropout_mask(dropout_p, s, rng, input.data.type(), operation)
return apply_complex_mask(input, mask, dropout_type, operation) / (1 - dropout_p) if do_dropout else input
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()))
r, i, j, k = init_func(r_weight.size(0), r_weight.size(1), rng, 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)