-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathlayers.py
196 lines (179 loc) · 6.54 KB
/
layers.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
"""CapsGNN layers."""
import torch
from torch.autograd import Variable
class ListModule(torch.nn.Module):
"""
Abstract list layer class.
"""
def __init__(self, *args):
"""
Model initializing.
"""
super(ListModule, self).__init__()
idx = 0
for module in args:
self.add_module(str(idx), module)
idx += 1
def __getitem__(self, idx):
"""
Getting the indexed layer.
"""
if idx < 0 or idx >= len(self._modules):
raise IndexError('index {} is out of range'.format(idx))
it = iter(self._modules.values())
for _ in range(idx):
next(it)
return next(it)
def __iter__(self):
"""
Iterating on the layers.
"""
return iter(self._modules.values())
def __len__(self):
"""
Number of layers.
"""
return len(self._modules)
class PrimaryCapsuleLayer(torch.nn.Module):
"""
Primary Convolutional Capsule Layer class based on:
https://github.com/timomernick/pytorch-capsule.
"""
def __init__(self, in_units, in_channels, num_units, capsule_dimensions):
super(PrimaryCapsuleLayer, self).__init__()
"""
:param in_units: Number of input units (GCN layers).
:param in_channels: Number of channels.
:param num_units: Number of capsules.
:param capsule_dimensions: Number of neurons in capsule.
"""
self.num_units = num_units
self.units = []
for i in range(self.num_units):
unit = torch.nn.Conv1d(in_channels=in_channels,
out_channels=capsule_dimensions,
kernel_size=(in_units, 1),
stride=1,
bias=True)
self.add_module("unit_" + str(i), unit)
self.units.append(unit)
@staticmethod
def squash(s):
"""
Squash activations.
:param s: Signal.
:return s: Activated signal.
"""
mag_sq = torch.sum(s**2, dim=2, keepdim=True)
mag = torch.sqrt(mag_sq)
s = (mag_sq / (1.0 + mag_sq)) * (s / mag)
return s
def forward(self, x):
"""
Forward propagation pass.
:param x: Input features.
:return : Primary capsule features.
"""
u = [self.units[i](x) for i in range(self.num_units)]
u = torch.stack(u, dim=1)
u = u.view(x.size(0), self.num_units, -1)
return PrimaryCapsuleLayer.squash(u)
class SecondaryCapsuleLayer(torch.nn.Module):
"""
Secondary Convolutional Capsule Layer class based on this repostory:
https://github.com/timomernick/pytorch-capsule
"""
def __init__(self, in_units, in_channels, num_units, unit_size):
super(SecondaryCapsuleLayer, self).__init__()
"""
:param in_units: Number of input units (GCN layers).
:param in_channels: Number of channels.
:param num_units: Number of capsules.
:param capsule_dimensions: Number of neurons in capsule.
"""
self.in_units = in_units
self.in_channels = in_channels
self.num_units = num_units
self.W = torch.nn.Parameter(torch.randn(1, in_channels, num_units, unit_size, in_units))
@staticmethod
def squash(s):
"""
Squash activations.
:param s: Signal.
:return s: Activated signal.
"""
mag_sq = torch.sum(s**2, dim=2, keepdim=True)
mag = torch.sqrt(mag_sq)
s = (mag_sq / (1.0 + mag_sq)) * (s / mag)
return s
def forward(self, x):
"""
Forward propagation pass.
:param x: Input features.
:return : Capsule output.
"""
batch_size = x.size(0)
x = x.transpose(1, 2)
x = torch.stack([x] * self.num_units, dim=2).unsqueeze(4)
W = torch.cat([self.W] * batch_size, dim=0)
u_hat = torch.matmul(W, x)
b_ij = Variable(torch.zeros(1, self.in_channels, self.num_units, 1))
num_iterations = 3
for _ in range(num_iterations):
c_ij = torch.nn.functional.softmax(b_ij, dim=2)
c_ij = torch.cat([c_ij] * batch_size, dim=0).unsqueeze(4)
s_j = (c_ij * u_hat).sum(dim=1, keepdim=True)
v_j = SecondaryCapsuleLayer.squash(s_j)
v_j1 = torch.cat([v_j] * self.in_channels, dim=1)
u_vj1 = torch.matmul(u_hat.transpose(3, 4), v_j1).squeeze(4).mean(dim=0, keepdim=True)
b_ij = b_ij + u_vj1
# b_max = torch.max(b_ij, dim = 2, keepdim = True)
# b_ij = b_ij / b_max.values ## values can be zero so loss would be nan
return v_j.squeeze(1)
class Attention(torch.nn.Module):
"""
2 Layer Attention Module.
See the CapsGNN paper for details.
"""
def __init__(self, attention_size_1, attention_size_2):
super(Attention, self).__init__()
"""
:param attention_size_1: Number of neurons in 1st attention layer.
:param attention_size_2: Number of neurons in 2nd attention layer.
"""
self.attention_1 = torch.nn.Linear(attention_size_1, attention_size_2)
self.attention_2 = torch.nn.Linear(attention_size_2, attention_size_1)
def forward(self, x_in):
"""
Forward propagation pass.
:param x_in: Primary capsule output.
:param condensed_x: Attention normalized capsule output.
"""
attention_score_base = self.attention_1(x_in)
attention_score_base = torch.nn.functional.relu(attention_score_base)
attention_score = self.attention_2(attention_score_base)
attention_score = torch.nn.functional.softmax(attention_score, dim=0)
condensed_x = x_in *attention_score
return condensed_x
def margin_loss(scores, target, loss_lambda):
"""
The margin loss from the original paper. Based on:
https://github.com/timomernick/pytorch-capsule
:param scores: Capsule scores.
:param target: Target groundtruth.
:param loss_lambda: Regularization parameter.
:return L_c: Classification loss.
"""
scores = scores.squeeze()
v_mag = torch.sqrt((scores**2).sum(dim=1, keepdim=True))
zero = Variable(torch.zeros(1))
m_plus = 0.9
m_minus = 0.1
max_l = torch.max(m_plus - v_mag, zero).view(1, -1)**2
max_r = torch.max(v_mag - m_minus, zero).view(1, -1)**2
T_c = Variable(torch.zeros(v_mag.shape))
T_c = target
L_c = T_c * max_l + loss_lambda * (1.0 - T_c) * max_r
L_c = L_c.sum(dim=1)
L_c = L_c.mean()
return L_c