-
Notifications
You must be signed in to change notification settings - Fork 14
/
quaternion_models.py
157 lines (144 loc) · 6.91 KB
/
quaternion_models.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
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 quaternionops import *
import math
import sys
class QuaternionLinearAutograd(Module):
r"""Applies a quaternion linear transformation to the incoming data.
Args:
in_features: size of each quaternion input sample. The effective number
of hidden units for each of the real and imaginary inputs.
The total effective number of input hidden units is 4 x in_features.
out_features: size of each quaternion output sample. The effective number
of hidden units for each of the real and imaginary outputs.
The total effective number of output hidden units is 4 x out_features.
bias: If set to False, the layer will not learn an additive bias.
Default: True
Shape:
- Input: (N, 4 * in_features)
- Output: (N, 4 * out_features)
Attributes:
weight: the learnable weights of the module of shape
(out_features x in_features)
bias: the learnable bias of the module of shape (2 * out_features)
Examples::
>>> m = QuaternionLinear(20, 30)
>>> input = Variable(torch.randn(128, 80))
>>> output = m(input)
>>> print(output.size())
"""
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.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.
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.
Args:
in_features: size of each quaternion input sample. The effective number
of hidden units for each of the real and imaginary inputs.
The total effective number of input hidden units is 4 x in_features.
out_features: size of each quaternion output sample. The effective number
of hidden units for each of the real and imaginary outputs.
The total effective number of output hidden units is 4 x out_features.
bias: If set to False, the layer will not learn an additive bias.
Default: True
Shape:
- Input: (N, 4 * in_features)
- Output: (N, 4 * out_features)
Attributes:
weight: the learnable weights of the module of shape
(out_features x in_features)
bias: the learnable bias of the module of shape (2 * out_features)
Examples::
>>> m = QuaternionLinear(20, 30)
>>> input = Variable(torch.randn(128, 80))
>>> output = m(input)
>>> print(output.size())
"""
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) + ')'