-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathquaternion_layers.py
More file actions
275 lines (233 loc) · 12.2 KB
/
Copy pathquaternion_layers.py
File metadata and controls
275 lines (233 loc) · 12.2 KB
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
##########################################################
# pytorch-qnn v1.0
# Titouan Parcollet
# LIA, Université d'Avignon et des Pays du Vaucluse
# ORKIS, Aix-en-provence
# October 2018
##########################################################
import numpy as np
from numpy.random import RandomState
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
from torch.nn.parameter import Parameter
from torch.nn.utils.rnn import PackedSequence
from torch.nn import Module
from torch.nn._functions.rnn import Recurrent, variable_recurrent_factory
from quaternion_ops import *
import math
import sys
class QuaternionTransposeConv(Module):
r"""Applies a Quaternion Transposed Convolution (or Deconvolution) to the incoming data.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
dilatation=1, padding=0, output_padding=0, groups=1, bias=True, init_criterion='glorot',
weight_init='quaternion', seed=None, operation='convolution2d', rotation=False):
super(QuaternionTransposeConv, self).__init__()
self.in_channels = in_channels // 4
self.out_channels = out_channels // 4
self.stride = stride
self.padding = padding
self.output_padding = output_padding
self.groups = groups
self.dilatation = dilatation
self.init_criterion = init_criterion
self.weight_init = weight_init
self.seed = seed if seed is not None else 1234
self.rng = RandomState(self.seed)
self.operation = operation
self.rotation = rotation
self.winit = {'quaternion': quaternion_init,
'unitary' : unitary_init,
'random' : random_init}[self.weight_init]
(self.kernel_size, self.w_shape) = get_kernel_and_weight_shape( self.operation,
self.out_channels, self.in_channels, kernel_size )
self.r_weight = Parameter(torch.Tensor(*self.w_shape))
self.i_weight = Parameter(torch.Tensor(*self.w_shape))
self.j_weight = Parameter(torch.Tensor(*self.w_shape))
self.k_weight = Parameter(torch.Tensor(*self.w_shape))
if bias:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
affect_init_conv(self.r_weight, self.i_weight, self.j_weight, self.k_weight,
self.kernel_size, self.winit, self.rng, self.init_criterion)
if self.bias is not None:
self.bias.data.zero_()
def forward(self, input):
if self.rotation:
return quaternion_tranpose_conv_rotation(input, self.r_weight, self.i_weight,
self.j_weight, self.k_weight, self.bias, self.stride, self.padding,
self.output_padding, self.groups, self.dilatation)
else:
return quaternion_transpose_conv(input, self.r_weight, self.i_weight, self.j_weight,
self.k_weight, self.bias, self.stride, self.padding, self.output_padding,
self.groups, self.dilatation)
def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'in_channels=' + str(self.in_channels) \
+ ', out_channels=' + str(self.out_channels) \
+ ', bias=' + str(self.bias is not None) \
+ ', kernel_size=' + str(self.kernel_size) \
+ ', stride=' + str(self.stride) \
+ ', padding=' + str(self.padding) \
+ ', dilation=' + str(self.dilation) \
+ ', init_criterion=' + str(self.init_criterion) \
+ ', weight_init=' + str(self.weight_init) \
+ ', seed=' + str(self.seed) \
+ ', operation=' + str(self.operation) + ')'
class QuaternionConv(Module):
r"""Applies a Quaternion Convolution to the incoming data.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride,
dilatation=1, padding=0, groups=1, bias=True, init_criterion='glorot',
weight_init='quaternion', seed=None, operation='convolution2d', rotation=False):
super(QuaternionConv, self).__init__()
self.in_channels = in_channels // 4
self.out_channels = out_channels // 4
self.stride = stride
self.padding = padding
self.groups = groups
self.dilatation = dilatation
self.init_criterion = init_criterion
self.weight_init = weight_init
self.seed = seed if seed is not None else 1234
self.rng = RandomState(self.seed)
self.operation = operation
self.rotation = rotation
self.winit = {'quaternion': quaternion_init,
'unitary' : unitary_init,
'random' : random_init}[self.weight_init]
(self.kernel_size, self.w_shape) = get_kernel_and_weight_shape( self.operation,
self.in_channels, self.out_channels, kernel_size )
self.r_weight = Parameter(torch.Tensor(*self.w_shape))
self.i_weight = Parameter(torch.Tensor(*self.w_shape))
self.j_weight = Parameter(torch.Tensor(*self.w_shape))
self.k_weight = Parameter(torch.Tensor(*self.w_shape))
if bias:
self.bias = Parameter(torch.Tensor(out_channels))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
affect_init_conv(self.r_weight, self.i_weight, self.j_weight, self.k_weight,
self.kernel_size, self.winit, self.rng, self.init_criterion)
if self.bias is not None:
self.bias.data.zero_()
def forward(self, input):
if self.rotation:
return quaternion_conv_rotation(input, self.r_weight, self.i_weight, self.j_weight,
self.k_weight, self.bias, self.stride, self.padding, selfn.groups, self.dilatation)
else:
return quaternion_conv(input, self.r_weight, self.i_weight, self.j_weight,
self.k_weight, self.bias, self.stride, self.padding, self.groups, self.dilatation)
def __repr__(self):
return self.__class__.__name__ + '(' \
+ 'in_channels=' + str(self.in_channels) \
+ ', out_channels=' + str(self.out_channels) \
+ ', bias=' + str(self.bias is not None) \
+ ', kernel_size=' + str(self.kernel_size) \
+ ', stride=' + str(self.stride) \
+ ', padding=' + str(self.padding) \
+ ', dilation=' + str(self.dilation) \
+ ', init_criterion=' + str(self.init_criterion) \
+ ', weight_init=' + str(self.weight_init) \
+ ', seed=' + str(self.seed) \
+ ', operation=' + str(self.operation) + ')'
class QuaternionLinearAutograd(Module):
r"""Applies a quaternion linear transformation to the incoming data. A custom
Autograd function is call to drastically reduce the VRAM consumption. Nonetheless, computing
time is also slower compared to QuaternionLinear().
"""
def __init__(self, in_features, out_features, bias=True,
init_criterion='glorot', weight_init='quaternion',
seed=None, rotation=False):
super(QuaternionLinearAutograd, self).__init__()
self.in_features = in_features//4
self.out_features = out_features//4
self.rotation = rotation
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 1337
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):
# See the autograd section for explanation of what happens here.
if self.rotation:
return quaternion_linear_rotation(input, self.r_weight, self.i_weight, self.j_weight, self.k_weight, self.bias)
else:
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"""Applies a quaternion linear transformation to the incoming data.
"""
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 1337
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) + ')'