forked from dido1998/Recurrent-Independent-Mechanisms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRIM.py
682 lines (547 loc) · 27.7 KB
/
RIM.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
import torch
import torch.nn as nn
import math
import numpy as np
import torch.multiprocessing as mp
from torch.nn.utils.rnn import PackedSequence
class blocked_grad(torch.autograd.Function):
@staticmethod
def forward(ctx, x, mask):
ctx.save_for_backward(x, mask)
return x
@staticmethod
def backward(ctx, grad_output):
x, mask = ctx.saved_tensors
return grad_output * mask, mask * 0.0
class AlphaFix(torch.autograd.Function):
"""
given: attention_probs, alpha
perform: a fix on the probs
"""
@staticmethod
def forward(ctx, attention_probs, alpha):
not_null_probs = attention_probs[:,:,0:-1] * alpha.reshape(1,-1,1)
null_probs = 1 - alpha.reshape(1,-1) + alpha.reshape(1,-1) * attention_probs[:,:,-1]
out_probs = torch.cat((not_null_probs, null_probs.unsqueeze(2)), 2)
ctx.save_for_backward(not_null_probs, null_probs, alpha)
return out_probs
@staticmethod
def backward(ctx, grad_output): # grad_output means the gradient w.r.t. output
not_null_probs, null_probs, alpha = ctx.saved_tensors
grad_alpha = torch.cat((not_null_probs, (-1+null_probs).unsqueeze(2)), 2)
grad_probs = alpha.reshape(1,-1)
return grad_output * grad_probs, grad_output * grad_alpha
class GroupLinearLayer(nn.Module):
'''
for num_blocks blocks, do linear transformations independently
self.w: (num_blocks, din, dout)
x: (batch_size, num_blocks, din)
-> permute: (num_blocks, batch_size, din)
-> bmm with self.w: (num_blocks, batch_size, din) (bmm) (num_blocks, din, dout)
for each block in range(num_blocks):
do (batch_size, din) mat_mul (din, dout)
concatenate
result (num_blocks, batch_size, dout)
-> permute: (batch_size, num_blocks, dout)
'''
def __init__(self, din, dout, num_blocks):
super(GroupLinearLayer, self).__init__()
self.w = nn.Parameter(0.01 * torch.randn(num_blocks,din,dout))
def forward(self,x):
x = x.permute(1,0,2)
x = torch.bmm(x,self.w)
return x.permute(1,0,2)
class GroupLSTMCell(nn.Module):
"""
GroupLSTMCell can compute the operation of N LSTM Cells at once.
"""
def __init__(self, inp_size, hidden_size, num_lstms):
super().__init__()
self.inp_size = inp_size
self.hidden_size = hidden_size
self.i2h = GroupLinearLayer(inp_size, 4 * hidden_size, num_lstms)
self.h2h = GroupLinearLayer(hidden_size, 4 * hidden_size, num_lstms)
self.reset_parameters()
def reset_parameters(self):
stdv = 1.0 / math.sqrt(self.hidden_size)
for weight in self.parameters():
weight.data.uniform_(-stdv, stdv)
def forward(self, x, hid_state):
"""
input: x (batch_size, num_lstms, input_size)
hid_state (tuple of length 2 with each element of size (batch_size, num_lstms, hidden_state))
output: h (batch_size, num_lstms, hidden_state)
c ((batch_size, num_lstms, hidden_state))
"""
h, c = hid_state
preact = self.i2h(x) + self.h2h(h)
gates = preact[:, :, :3 * self.hidden_size].sigmoid()
g_t = preact[:, :, 3 * self.hidden_size:].tanh()
i_t = gates[:, :, :self.hidden_size]
f_t = gates[:, :, self.hidden_size:2 * self.hidden_size]
o_t = gates[:, :, -self.hidden_size:]
c_t = torch.mul(c, f_t) + torch.mul(i_t, g_t)
h_t = torch.mul(o_t, c_t.tanh())
return h_t, c_t
class GroupGRUCell(nn.Module):
"""
GroupGRUCell can compute the operation of N GRU Cells at once.
"""
def __init__(self, input_size, hidden_size, num_grus):
super(GroupGRUCell, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.x2h = GroupLinearLayer(input_size, 3 * hidden_size, num_grus)
self.h2h = GroupLinearLayer(hidden_size, 3 * hidden_size, num_grus)
self.reset_parameters()
def reset_parameters(self):
std = 1.0 / math.sqrt(self.hidden_size)
for w in self.parameters():
w.data = torch.ones(w.data.size())#.uniform_(-std, std)
def forward(self, x, hidden):
"""
input: x (batch_size, num_grus, input_size)
hidden (batch_size, num_grus, hidden_size)
output: hidden (batch_size, num_grus, hidden_size)
"""
gate_x = self.x2h(x)
gate_h = self.h2h(hidden)
i_r, i_i, i_n = gate_x.chunk(3, 2)
h_r, h_i, h_n = gate_h.chunk(3, 2)
resetgate = torch.sigmoid(i_r + h_r)
inputgate = torch.sigmoid(i_i + h_i)
newgate = torch.tanh(i_n + (resetgate * h_n))
hy = newgate + inputgate * (hidden - newgate)
return hy
class GroupTorchGRU(nn.Module):
'''
Calculate num_units GRU cells in parallel
'''
def __init__(self, input_size, hidden_size, num_units):
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.num_units = num_units
gru_list = [nn.GRU(input_size=self.input_size,
hidden_size=self.hidden_size,
num_layers=1,
batch_first=True,
bidirectional=False) for _ in range(num_units)]
self.grus = nn.ModuleList(gru_list)
def forward(self, inputs, hidden):
"""
input: x (batch_size, num_units, input_size)
hidden (batch_size, num_units, hidden_size)
output: hidden (batch_size, num_units, hidden_size)
"""
hidden_list = [gru(inputs[:,i,:].unsqueeze(1), hidden[:,i,:].unsqueeze(0).contiguous())[1].squeeze(0) for i, gru in enumerate(self.grus)]
# hidden_list: list of (batch_size, hidden_size)
hidden_new = torch.stack(hidden_list, dim=1)
return hidden_new
class RIMCell(nn.Module):
def __init__(self,
device, input_size, hidden_size, num_units, k, rnn_cell, input_key_size = 64, input_value_size = 400, input_query_size = 64,
num_input_heads = 1, input_dropout = 0.1, comm_key_size = 32, comm_value_size = 100, comm_query_size = 32, num_comm_heads = 4, comm_dropout = 0.1
):
super().__init__()
if comm_value_size != hidden_size:
#print('INFO: Changing communication value size to match hidden_size')
comm_value_size = hidden_size
self.device = device
self.hidden_size = hidden_size
self.num_units =num_units
self.rnn_cell = rnn_cell
self.key_size = input_key_size
self.k = k
self.num_input_heads = num_input_heads
self.num_comm_heads = num_comm_heads
self.input_key_size = input_key_size
self.input_query_size = input_query_size
assert input_key_size == input_query_size, "Key and query should be of same size, no? " # they must be equal!
self.input_value_size = input_value_size
self.comm_key_size = comm_key_size
self.comm_query_size = comm_query_size
self.comm_value_size = comm_value_size
# inp_attn transformations
self.key = nn.Linear(input_size, num_input_heads * input_query_size, bias=False)
self.value = nn.Linear(input_size, num_input_heads * input_value_size, bias=False)
self.query = GroupLinearLayer(hidden_size, input_key_size * num_input_heads, self.num_units)
if self.rnn_cell == 'GRU':
# self.rnn = GroupGRUCell(input_value_size, hidden_size, num_units)
self.rnn = GroupTorchGRU(input_value_size, hidden_size, num_units)
else:
self.rnn = GroupLSTMCell(input_value_size, hidden_size, num_units)
# comm_attn transformations
self.query_ =GroupLinearLayer(hidden_size, comm_query_size * num_comm_heads, self.num_units)
self.key_ = GroupLinearLayer(hidden_size, comm_key_size * num_comm_heads, self.num_units)
self.value_ = GroupLinearLayer(hidden_size, comm_value_size * num_comm_heads, self.num_units)
self.comm_attention_output = GroupLinearLayer(num_comm_heads * comm_value_size, comm_value_size, self.num_units)
self.input_dropout = nn.Dropout(p =input_dropout)
self.comm_dropout = nn.Dropout(p =comm_dropout)
def transpose_for_scores(self, x, num_attention_heads, attention_head_size):
new_x_shape = x.size()[:-1] + (num_attention_heads, attention_head_size)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def input_attention_mask(self, x, h):
"""
Input : x (batch_size, 2, input_size) [The null input is appended along the first dimension]
h (batch_size, num_units, hidden_size)
Output: inputs (list of size num_units with each element of shape (batch_size, input_value_size))
mask_ binary array of shape (batch_size, num_units) where 1 indicates active and 0 indicates inactive
"""
key_layer = self.key(x) # input size 1 or fullsize??
value_layer = self.value(x)
query_layer = self.query(h)
key_layer = self.transpose_for_scores(key_layer, self.num_input_heads, self.input_key_size)
value_layer = torch.mean(self.transpose_for_scores(value_layer, self.num_input_heads, self.input_value_size), dim = 1)
query_layer = self.transpose_for_scores(query_layer, self.num_input_heads, self.input_query_size)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) / math.sqrt(self.input_key_size)
attention_scores = torch.mean(attention_scores, dim = 1)
mask_ = torch.zeros(x.size(0), self.num_units).to(self.device)
not_null_scores = attention_scores[:,:, 0]
topk1 = torch.topk(not_null_scores,self.k, dim = 1)
batch_indices = torch.arange(x.shape[0]).unsqueeze(1)
row_to_activate = batch_indices.repeat((1,self.k)) # repeat to the same shape as topk1.indices
mask_[row_to_activate.view(-1), topk1.indices.view(-1)] = 1
self.nan_hook(attention_scores)
self.inf_hook(attention_scores)
attention_probs = self.input_dropout(nn.Softmax(dim = -1)(attention_scores))
inputs = torch.matmul(attention_probs, value_layer) * mask_.unsqueeze(2)
return inputs, mask_
def communication_attention(self, h, mask):
"""
Input : h (batch_size, num_units, hidden_size)
mask obtained from the input_attention_mask() function
Output: context_layer (batch_size, num_units, hidden_size). New hidden states after communication
"""
query_layer = []
key_layer = []
value_layer = []
query_layer = self.query_(h)
key_layer = self.key_(h)
value_layer = self.value_(h)
query_layer = self.transpose_for_scores(query_layer, self.num_comm_heads, self.comm_query_size)
key_layer = self.transpose_for_scores(key_layer, self.num_comm_heads, self.comm_key_size)
value_layer = self.transpose_for_scores(value_layer, self.num_comm_heads, self.comm_value_size)
# query_layer = torch.clamp(query_layer, min=-1e6, max=1e6)
# key_layer = torch.clamp(key_layer, min=-1e6, max=1e6)
# value_layer = torch.clamp(value_layer, min=-1e6, max=1e6)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
# attention_scores = torch.clamp(attention_scores, min=-1e7, max=1e7)
attention_scores = attention_scores / math.sqrt(self.comm_key_size)
self.inf_hook(attention_scores)
attention_probs = nn.Softmax(dim=-1)(attention_scores)
mask = [mask for _ in range(attention_probs.size(1))]
mask = torch.stack(mask, dim = 1) # repeat activation mask for each head
attention_probs = attention_probs * mask.unsqueeze(3) # inactive modules have zero-value query -> no context for them
self.nan_hook(attention_probs)
self.inf_hook(attention_probs)
attention_probs = self.comm_dropout(attention_probs)
context_layer = torch.matmul(attention_probs, value_layer)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
new_context_layer_shape = context_layer.size()[:-2] + (self.num_comm_heads * self.comm_value_size,)
context_layer = context_layer.view(*new_context_layer_shape) # concatenate all heads
context_layer = self.comm_attention_output(context_layer) # linear
context_layer = context_layer + h
return context_layer
def nan_hook(self, out):
nan_mask = torch.isnan(out)
if nan_mask.any():
print("In", self.__class__.__name__)
raise RuntimeError(f"Found NAN in output: ", nan_mask.nonzero(), "where:", out[nan_mask.nonzero()[:, 0].unique(sorted=True)])
def inf_hook(self, _tensor):
inf_mask = torch.isinf(_tensor)
if inf_mask.any():
raise RuntimeError(f"Found NAN in {self.__class__.__name__}: ", inf_mask.nonzero(), "where:", _tensor[inf_mask.nonzero()[:, 0].unique(sorted=True)])
def forward(self, x, hs, cs = None):
"""
Input : x (batch_size, input_size)
hs (batch_size, num_units, hidden_size)
cs (batch_size, num_units, hidden_size)
Output: new hs, cs for LSTM
new hs for GRU
"""
size = x.size()
null_input = torch.zeros(size[0], 1, size[1]).float().to(self.device)
x = torch.cat((x.unsqueeze(1), null_input), dim = 1)
# Compute input attention
inputs, mask = self.input_attention_mask(x, hs)
h_old = hs * 1.0
if cs is not None:
c_old = cs * 1.0
self.nan_hook(inputs)
# Compute RNN(LSTM or GRU) output
if cs is not None:
hs, cs = self.rnn(inputs, (hs, cs))
else:
hs = self.rnn(inputs, hs)
self.nan_hook(hs)
# Block gradient through inactive units
mask = mask.unsqueeze(2).detach()
h_new = blocked_grad.apply(hs, mask)
# Compute communication attention
h_new = self.communication_attention(h_new, mask.squeeze(2))
# Prepare the context/intermediate value
ctx = {
"input_mask": mask.squeeze(),
}
# Update hs and cs and return them
hs = mask * h_new + (1 - mask) * h_old
if cs is not None:
cs = mask * cs + (1 - mask) * c_old
return hs, cs, None, mask
self.nan_hook(hs)
return hs, None, None, ctx
class RIM(nn.Module):
def __init__(self, device, input_size, hidden_size, num_units, k, rnn_cell, n_layers, bidirectional, **kwargs):
super().__init__()
if device == 'cuda':
self.device = torch.device('cuda')
else:
self.device = torch.device('cpu')
self.n_layers = n_layers
self.num_directions = 2 if bidirectional else 1
self.rnn_cell = rnn_cell
self.num_units = num_units
self.hidden_size = hidden_size
if self.num_directions == 2:
self.rimcell = nn.ModuleList([RIMCell(self.device, input_size, hidden_size, num_units, k, rnn_cell, **kwargs).to(self.device) if i < 2 else
RIMCell(self.device, 2 * hidden_size * self.num_units, hidden_size, num_units, k, rnn_cell, **kwargs).to(self.device) for i in range(self.n_layers * self.num_directions)])
else:
self.rimcell = nn.ModuleList([RIMCell(self.device, input_size, hidden_size, num_units, k, rnn_cell, **kwargs).to(self.device) if i == 0 else
RIMCell(self.device, hidden_size * self.num_units, hidden_size, num_units, k, rnn_cell, **kwargs).to(self.device) for i in range(self.n_layers)])
def layer(self, rim_layer, x, h, c = None, direction = 0):
batch_size = x.size(1)
xs = list(torch.split(x, 1, dim = 0))
if direction == 1: xs.reverse()
hs = h.squeeze(0).view(batch_size, self.num_units, -1)
cs = None
if c is not None:
cs = c.squeeze(0).view(batch_size, self.num_units, -1)
outputs = []
for x in xs:
x = x.squeeze(0)
hs, cs = rim_layer(x.unsqueeze(1), hs, cs)
outputs.append(hs.view(1, batch_size, -1))
if direction == 1: outputs.reverse()
outputs = torch.cat(outputs, dim = 0)
if c is not None:
return outputs, hs.view(batch_size, -1), cs.view(batch_size, -1)
else:
return outputs, hs.view(batch_size, -1)
def forward(self, x, h = None, c = None):
"""
Input: x (seq_len, batch_size, feature_size
h (num_layers * num_directions, batch_size, hidden_size * num_units)
c (num_layers * num_directions, batch_size, hidden_size * num_units)
Output: outputs (batch_size, seqlen, hidden_size * num_units * num-directions)
h(and c) (num_layer * num_directions, batch_size, hidden_size* num_units)
"""
hs = torch.split(h, 1, 0) if h is not None else torch.split(torch.randn(self.n_layers * self.num_directions, x.size(1), self.hidden_size * self.num_units).to(self.device), 1, 0)
hs = list(hs)
cs = None
if self.rnn_cell == 'LSTM':
cs = torch.split(c, 1, 0) if c is not None else torch.split(torch.randn(self.n_layers * self.num_directions, x.size(1), self.hidden_size * self.num_units).to(self.device), 1, 0)
cs = list(cs)
for n in range(self.n_layers):
idx = n * self.num_directions
if cs is not None:
x_fw, hs[idx], cs[idx] = self.layer(self.rimcell[idx], x, hs[idx], cs[idx])
else:
x_fw, hs[idx] = self.layer(self.rimcell[idx], x, hs[idx], c = None)
if self.num_directions == 2:
idx = n * self.num_directions + 1
if cs is not None:
x_bw, hs[idx], cs[idx] = self.layer(self.rimcell[idx], x, hs[idx], cs[idx], direction = 1)
else:
x_bw, hs[idx] = self.layer(self.rimcell[idx], x, hs[idx], c = None, direction = 1)
x = torch.cat((x_fw, x_bw), dim = 2)
else:
x = x_fw
hs = torch.stack(hs, dim = 0)
if cs is not None:
cs = torch.stack(cs, dim = 0)
return x, hs, cs
return x, hs
# modified part
class OmegaLoss(nn.Module):
def __init__(self, c, eta_0, nu_0):
super().__init__()
self.c = c
self.eta_0 = eta_0
self.nu_0 = nu_0
# nu: BATCH x K, eta_0: scaler, nu_0: scalser
# maar, nu should be the same for the whole batch (it's parameter)
def forward(self, eta, nu):
omega_part_1 = -torch.sum(torch.lgamma(eta-nu+1)-torch.lgamma(nu+1),) #first term, sum over k
omega_part_2 = torch.sum((eta-nu-self.eta_0+self.nu_0)*(torch.digamma(eta-nu+1)-torch.digamma(eta+2)))
omega_part_3 = torch.sum((nu-self.nu_0)*(torch.digamma(nu+1)-torch.digamma(eta+2)))
Omega_c = self.c * (omega_part_1+omega_part_2+omega_part_3)
return Omega_c
class SparseRIMCell(nn.Module):
def __init__(self,
device, input_size, hidden_size, num_units, k, rnn_cell, input_key_size = 64, input_value_size = 400, input_query_size = 64,
num_input_heads = 1, input_dropout = 0.1, comm_key_size = 32, comm_value_size = 100, comm_query_size = 32, num_comm_heads = 4, comm_dropout = 0.1,
a=1, b=3, threshold=0.5
):
super().__init__()
if comm_value_size != hidden_size:
#print('INFO: Changing communication value size to match hidden_size')
comm_value_size = hidden_size
self.device = device
self.hidden_size = hidden_size
self.num_units =num_units
self.rnn_cell = rnn_cell
self.key_size = input_key_size
self.k = num_units # full activation
self.num_input_heads = num_input_heads
self.num_comm_heads = num_comm_heads
self.input_key_size = input_key_size
self.input_query_size = input_query_size
self.input_value_size = input_value_size
self.comm_key_size = comm_key_size
self.comm_query_size = comm_query_size
self.comm_value_size = comm_value_size
self.eta_0 = a+b-1
self.nu_0 = b-1
self.threshold = threshold
self.eta = self.eta_0
self.nu = nn.Parameter(self.nu_0*torch.ones(num_units))
self.key = nn.Linear(input_size, num_input_heads * input_query_size).to(self.device)
self.value = nn.Linear(input_size, num_input_heads * input_value_size).to(self.device)
if self.rnn_cell == 'GRU':
self.rnn = GroupGRUCell(input_value_size, hidden_size, num_units)
self.query = GroupLinearLayer(hidden_size, input_key_size * num_input_heads, self.num_units)
else:
self.rnn = GroupLSTMCell(input_value_size, hidden_size, num_units)
self.query = GroupLinearLayer(hidden_size, input_key_size * num_input_heads, self.num_units)
self.query_ =GroupLinearLayer(hidden_size, comm_query_size * num_comm_heads, self.num_units)
self.key_ = GroupLinearLayer(hidden_size, comm_key_size * num_comm_heads, self.num_units)
self.value_ = GroupLinearLayer(hidden_size, comm_value_size * num_comm_heads, self.num_units)
self.comm_attention_output = GroupLinearLayer(num_comm_heads * comm_value_size, comm_value_size, self.num_units)
self.comm_dropout = nn.Dropout(p =input_dropout)
self.input_dropout = nn.Dropout(p =comm_dropout)
def transpose_for_scores(self, x, num_attention_heads, attention_head_size):
new_x_shape = x.size()[:-1] + (num_attention_heads, attention_head_size)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def input_attention_mask(self, x, h):
"""
Input : x (batch_size, 2, input_size) [The null input is appended along the first dimension]
h (batch_size, num_units, hidden_size)
Output: inputs (list of size num_units with each element of shape (batch_size, input_value_size))
mask_ binary array of shape (batch_size, num_units) where 1 indicates active and 0 indicates inactive
"""
key_layer = self.key(x) # input size 1 or fullsize??
value_layer = self.value(x)
query_layer = self.query(h)
key_layer = self.transpose_for_scores(key_layer, self.num_input_heads, self.input_key_size)
value_layer = torch.mean(self.transpose_for_scores(value_layer, self.num_input_heads, self.input_value_size), dim = 1)
query_layer = self.transpose_for_scores(query_layer, self.num_input_heads, self.input_query_size)
self.eta = x.shape[0] + self.eta_0
alpha = (self.eta-self.nu+1)/(self.eta+2)
alpha = alpha.reshape(1,-1)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2)) / math.sqrt(self.input_key_size)
attention_scores = torch.mean(attention_scores, dim = 1)
mask_att = torch.zeros(x.size(0), self.num_units).to(self.device)
mask_alpha = torch.zeros_like(mask_att)
not_null_scores = attention_scores[:,:, 0]
topk1 = torch.topk(not_null_scores,self.k, dim = 1)
row_index = np.arange(x.size(0))
row_index = np.repeat(row_index, self.k)
mask_att[row_index, topk1.indices.view(-1)] = 1
attention_probs = nn.Softmax(dim = -1)(attention_scores)
not_null_probs = 1 - attention_probs[:,:,-1]
# PERFORM CUSTOM ALPHA FIX FUNCTION
# attention_probs = AlphaFix.apply(attention_probs, alpha)
fixed_probs = torch.zeros_like(attention_probs)
fixed_probs[:,:,0:-1] = attention_probs[:,:,0:-1] * alpha.reshape(1,-1,1)
fixed_probs[:,:,-1] = 1 - not_null_probs * alpha.reshape(1,-1)
not_null_probs = 1 - fixed_probs[:,:,-1]
mask_alpha = torch.ceil(not_null_probs-self.threshold)
mask = mask_att * mask_alpha
fixed_probs = self.input_dropout(fixed_probs)
inputs = torch.matmul(fixed_probs, value_layer) * mask.unsqueeze(2)
return inputs, mask
def communication_attention(self, h, mask):
"""
Input : h (batch_size, num_units, hidden_size)
mask obtained from the input_attention_mask() function
Output: context_layer (batch_size, num_units, hidden_size). New hidden states after communication
"""
query_layer = []
key_layer = []
value_layer = []
query_layer = self.query_(h)
key_layer = self.key_(h)
value_layer = self.value_(h)
query_layer = self.transpose_for_scores(query_layer, self.num_comm_heads, self.comm_query_size)
key_layer = self.transpose_for_scores(key_layer, self.num_comm_heads, self.comm_key_size)
value_layer = self.transpose_for_scores(value_layer, self.num_comm_heads, self.comm_value_size)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.comm_key_size)
attention_probs = nn.Softmax(dim=-1)(attention_scores)
mask = [mask for _ in range(attention_probs.size(1))]
mask = torch.stack(mask, dim = 1)
attention_probs = attention_probs * mask.unsqueeze(3)
attention_probs = self.comm_dropout(attention_probs)
context_layer = torch.matmul(attention_probs, value_layer)
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
new_context_layer_shape = context_layer.size()[:-2] + (self.num_comm_heads * self.comm_value_size,)
context_layer = context_layer.view(*new_context_layer_shape)
context_layer = self.comm_attention_output(context_layer)
context_layer = context_layer + h
return context_layer
def forward(self, x, hs, cs = None):
"""
Input : x (batch_size, 1 , input_size)
hs (batch_size, num_units, hidden_size)
cs (batch_size, num_units, hidden_size)
Output: new hs, cs for LSTM
new hs for GRU
"""
size = x.size()
null_input = torch.zeros(size[0], 1, size[1]).float().to(self.device)
x = torch.cat((x.unsqueeze(1), null_input), dim = 1)
# Compute input attention
inputs, mask = self.input_attention_mask(x, hs)
h_old = hs * 1.0
if cs is not None:
c_old = cs * 1.0
# Compute RNN(LSTM or GRU) output
if cs is not None:
hs, cs = self.rnn(inputs, (hs, cs))
else:
hs = self.rnn(inputs, hs)
# Block gradient through inactive units
mask = mask.unsqueeze(2)
h_new = blocked_grad.apply(hs, mask)
# Compute communication attention
h_new = self.communication_attention(h_new, mask.squeeze(2))
hs = mask * h_new + (1 - mask) * h_old
if cs is not None:
cs = mask * cs + (1 - mask) * c_old
return hs, cs, self.nu
return hs, None, self.nu
class LayerNorm(nn.Module):
def __init__(self):
super(LayerNorm, self).__init__()
self.layernorm = nn.functional.layer_norm
def forward(self, x):
x = self.layernorm(x, list(x.size()[1:]))
return x
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class UnFlatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), 64, 8, 8)
class Interpolate(nn.Module):
def __init__(self, scale_factor, mode):
super(Interpolate, self).__init__()
self.interp = nn.functional.interpolate
self.scale_factor = scale_factor
self.mode = mode
def forward(self, x):
x = self.interp(x, scale_factor=self.scale_factor, mode=self.mode, align_corners=False)
return x