forked from gtfintechlab/HYPHEN-ACL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nets.py
313 lines (292 loc) · 10.2 KB
/
nets.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
import itertools
import torch.nn
import torch.nn.functional
import math
import geoopt.manifolds.stereographic.math as pmath
import geoopt
def mobius_linear(
input,
weight,
bias=None,
hyperbolic_input=True,
hyperbolic_bias=True,
nonlin=None,
k=1.0,
):
if hyperbolic_input:
output = pmath.mobius_matvec(weight, input, k=k)
else:
output = torch.nn.functional.linear(input, weight)
output = pmath.expmap0(output, k=k)
if bias is not None:
if not hyperbolic_bias:
bias = pmath.expmap0(bias, k=k)
output = pmath.mobius_add(output, bias, k=k)
if nonlin is not None:
output = pmath.mobius_fn_apply(nonlin, output, k=k)
output = pmath.project(output, k=k)
return output
def one_rnn_transform(W, h, U, x, b, c):
W_otimes_h = pmath.mobius_matvec(W, h, k=c)
U_otimes_x = pmath.mobius_matvec(U, x, k=c)
Wh_plus_Ux = pmath.mobius_add(W_otimes_h, U_otimes_x, k=c)
return pmath.mobius_add(Wh_plus_Ux, b, k=c)
def mobius_gru_cell(
input: torch.Tensor,
hx: torch.Tensor,
weight_ih: torch.Tensor,
weight_hh: torch.Tensor,
bias: torch.Tensor,
c: torch.Tensor,
nonlin=None,
):
W_ir, W_ih, W_iz = weight_ih.chunk(3)
b_r, b_h, b_z = bias
W_hr, W_hh, W_hz = weight_hh.chunk(3)
# print ('Inside GRU Cell: ')
# print ('W_hz: ', W_hz.shape)
# print ('hx: ', hx.shape)
# print ('W_iz: ', W_iz.shape)
# print ('input: ', input.shape)
z_t = pmath.logmap0(one_rnn_transform(W_hz, hx, W_iz, input, b_z, c), k=c).sigmoid()
r_t = pmath.logmap0(one_rnn_transform(W_hr, hx, W_ir, input, b_r, c), k=c).sigmoid()
rh_t = pmath.mobius_pointwise_mul(r_t, hx, k=c)
h_tilde = one_rnn_transform(W_hh, rh_t, W_ih, input, b_h, c)
if nonlin is not None:
h_tilde = pmath.mobius_fn_apply(nonlin, h_tilde, k=c)
delta_h = pmath.mobius_add(-hx, h_tilde, k=c)
h_out = pmath.mobius_add(hx, pmath.mobius_pointwise_mul(z_t, delta_h, k=c), k=c)
return h_out
def mobius_gru_loop(
input: torch.Tensor,
h0: torch.Tensor,
weight_ih: torch.Tensor,
weight_hh: torch.Tensor,
bias: torch.Tensor,
c: torch.Tensor,
batch_sizes=None,
hyperbolic_input: bool = False,
hyperbolic_hidden_state0: bool = False,
nonlin=None,
):
if not hyperbolic_hidden_state0:
hx = pmath.expmap0(h0, k=c)
else:
hx = h0
if not hyperbolic_input:
input = pmath.expmap0(input, k=c)
outs = []
if batch_sizes is None:
input_unbinded = input.unbind(0)
for t in range(input.size(0)):
# print ('Inside GRU loop T: ', t)
hx = mobius_gru_cell(
input=input_unbinded[t],
hx=hx,
weight_ih=weight_ih,
weight_hh=weight_hh,
bias=bias,
nonlin=nonlin,
c=c,
)
outs.append(hx)
outs = torch.stack(outs)
h_last = hx
else:
h_last = []
T = len(batch_sizes) - 1
for i, t in enumerate(range(batch_sizes.size(0))):
ix, input = input[: batch_sizes[t]], input[batch_sizes[t] :]
hx = mobius_gru_cell(
input=ix,
hx=hx,
weight_ih=weight_ih,
weight_hh=weight_hh,
bias=bias,
nonlin=nonlin,
c=c,
)
outs.append(hx)
if t < T:
hx, ht = hx[: batch_sizes[t+1]], hx[batch_sizes[t+1]:]
h_last.append(ht)
else:
h_last.append(hx)
h_last.reverse()
h_last = torch.cat(h_last)
outs = torch.cat(outs)
return outs, h_last
class MobiusLinear(torch.nn.Linear):
def __init__(
self,
*args,
hyperbolic_input=True,
hyperbolic_bias=True,
nonlin=None,
k=1.0,
**kwargs
):
super().__init__(*args, **kwargs)
if self.bias is not None:
if hyperbolic_bias:
self.ball = manifold = geoopt.PoincareBall(k=k)
print(self.ball.device,'deviceeee')
self.bias = geoopt.ManifoldParameter(self.bias, manifold=manifold)
with torch.no_grad():
self.bias.set_(pmath.expmap0(self.bias.normal_() / 4, k=k))
with torch.no_grad():
self.weight.normal_(std=1e-2)
self.hyperbolic_bias = hyperbolic_bias
self.hyperbolic_input = hyperbolic_input
self.nonlin = nonlin
def forward(self, input):
return mobius_linear(
input,
weight=self.weight,
bias=self.bias,
hyperbolic_input=self.hyperbolic_input,
nonlin=self.nonlin,
hyperbolic_bias=self.hyperbolic_bias,
k=self.ball.c,
)
def extra_repr(self):
info = super().extra_repr()
info += "k={}, hyperbolic_input={}".format(self.ball.c, self.hyperbolic_input)
if self.bias is not None:
info = ", hyperbolic_bias={}".format(self.hyperbolic_bias)
return info
class MobiusDist2Hyperplane(torch.nn.Module):
def __init__(self, in_features, out_features, k=1.0):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.ball = ball = geoopt.PoincareBall(k=k)
self.sphere = sphere = geoopt.manifolds.Sphere()
self.scale = torch.nn.Parameter(torch.zeros(out_features))
point = torch.randn(out_features, in_features) / 4
point = pmath.expmap0(point, k=k)
tangent = torch.randn(out_features, in_features)
self.point = geoopt.ManifoldParameter(point, manifold=ball)
with torch.no_grad():
self.tangent = geoopt.ManifoldParameter(tangent, manifold=sphere).proj_()
def forward(self, input):
input = input.unsqueeze(-2)
distance = pmath.dist2plane(
x=input, p=self.point, a=self.tangent, k=self.ball.c, signed=True
)
return distance * self.scale.exp()
def extra_repr(self):
return (
"in_features={in_features}, out_features={out_features}, "
"k={ball.c}".format(
**self.__dict__
)
)
class MobiusGRU(torch.nn.Module):
def __init__(
self,
input_size,
hidden_size,
num_layers=1,
bias=True,
nonlin=None,
hyperbolic_input=True,
hyperbolic_hidden_state0=True,
k=1.0,
):
super().__init__()
self.ball = geoopt.PoincareBall(k)
self.input_size = input_size
self.hidden_size = hidden_size
self.num_layers = num_layers
self.bias = bias
self.weight_ih = torch.nn.ParameterList(
[
torch.nn.Parameter(
torch.Tensor(3 * hidden_size, input_size if i == 0 else hidden_size)
)
for i in range(num_layers)
]
)
self.weight_hh = torch.nn.ParameterList(
[
torch.nn.Parameter(torch.Tensor(3 * hidden_size, hidden_size))
for _ in range(num_layers)
]
)
if bias:
biases = []
for i in range(num_layers):
bias = torch.randn(3, hidden_size) * 1e-5
bias = bias.to('cuda')
# print(self.ball.c.device,'ball_device')
# print(bias.device,'bias')
bias = geoopt.ManifoldParameter(
pmath.expmap0(bias, k=self.ball.c), manifold=self.ball
)
biases.append(bias)
self.bias = torch.nn.ParameterList(biases).to('cuda')
else:
self.register_buffer("bias", None)
self.nonlin = nonlin
self.hyperbolic_input = hyperbolic_input
self.hyperbolic_hidden_state0 = hyperbolic_hidden_state0
self.reset_parameters()
def reset_parameters(self):
stdv = 1.0 / math.sqrt(self.hidden_size)
for weight in itertools.chain.from_iterable([self.weight_ih, self.weight_hh]):
torch.nn.init.uniform_(weight, -stdv, stdv)
def forward(self, input: torch.Tensor, h0=None):
# input shape: seq_len, batch, input_size
# hx shape: batch, hidden_size
is_packed = isinstance(input, torch.nn.utils.rnn.PackedSequence)
if is_packed:
input, batch_sizes = input[:2]
max_batch_size = int(batch_sizes[0])
else:
batch_sizes = None
max_batch_size = input.size(1)
if h0 is None:
h0 = input.new_zeros(
self.num_layers, max_batch_size, self.hidden_size, requires_grad=False
)
h0 = h0.unbind(0)
if self.bias is not None:
biases = self.bias
else:
biases = (None,) * self.num_layers
outputs = []
last_states = []
out = input
for i in range(self.num_layers):
out, h_last = mobius_gru_loop(
input=out,
h0=h0[i],
weight_ih=self.weight_ih[i],
weight_hh=self.weight_hh[i],
bias=biases[i],
c=self.ball.c,
hyperbolic_hidden_state0=self.hyperbolic_hidden_state0 or i > 0,
hyperbolic_input=self.hyperbolic_input or i > 0,
nonlin=self.nonlin,
batch_sizes=batch_sizes,
)
outputs.append(out)
last_states.append(h_last)
if is_packed:
out = torch.nn.utils.rnn.PackedSequence(out, batch_sizes)
ht = torch.stack(last_states)
# default api assumes
# out: (seq_len, batch, num_directions * hidden_size)
# ht: (num_layers * num_directions, batch, hidden_size)
# if packed:
# out: (sum(seq_len), num_directions * hidden_size)
# ht: (num_layers * num_directions, batch, hidden_size)
return out, ht
def extra_repr(self):
return (
"{input_size}, {hidden_size}, {num_layers}, bias={bias}, "
"hyperbolic_input={hyperbolic_input}, "
"hyperbolic_hidden_state0={hyperbolic_hidden_state0}, "
"k={self.ball.c}"
).format(**self.__dict__, self=self, bias=self.bias is not None)