-
Notifications
You must be signed in to change notification settings - Fork 6
/
MHDPA.py
58 lines (45 loc) · 2.2 KB
/
MHDPA.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
from utilities import *
import torch.nn.functional as F
class MultiHeadAttention(Module):
def __init__(self, heads, entity_dimensionality, rounds=1, residual=True,
layers=2):
super().__init__()
self.entity_dimensionality = entity_dimensionality
self.heads = heads
assert entity_dimensionality%heads == 0,\
"dimensionality of entities must be divisible by number of heads"
# Dimensionality of each head
self.d = entity_dimensionality//heads
self.Q = nn.Linear(entity_dimensionality, entity_dimensionality)
self.V = nn.Linear(entity_dimensionality, entity_dimensionality)
self.K = nn.Linear(entity_dimensionality, entity_dimensionality)
self.output = nn.Sequential(*[ layer
for _ in range(layers)
for layer in [nn.Linear(entity_dimensionality, entity_dimensionality),
nn.ReLU()]])
self.rounds = rounds
self.residual = residual
self.finalize()
def forward(self, entities):
"""
entities: (# entities)x(entity_dimensionality)
returns: (# entities)x(entity_dimensionality)
"""
for _ in range(self.rounds):
# query, values, and keys should all be of size HxExD
q = self.Q(entities).view(entities.size(0), self.heads, self.d).permute(1,0,2)
v = self.V(entities).view(entities.size(0), self.heads, self.d).permute(1,0,2)
k = self.K(entities).view(entities.size(0), self.heads, self.d).permute(1,0,2)
# attention[i,j] = q_i . k_j
# i.e., amount that object I is attending to object J
attention = F.softmax(q@(k.permute(0,2,1))/(self.d**0.5), dim=-1)
# Mix together values
o = (attention@v).transpose(0,1).contiguous().view(entities.size(0), self.entity_dimensionality)
# Apply output transformation
o = self.output(o)
# residual connection
if self.residual:
entities = entities + o
else:
entities = o
return entities