-
Notifications
You must be signed in to change notification settings - Fork 0
/
attention.py
254 lines (218 loc) · 10.4 KB
/
attention.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
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class MaskedEdgeAttention(nn.Module):
def __init__(self, input_dim, max_seq_len, no_cuda):
"""
Method to compute the edge weights, as in Equation 1. in the paper.
attn_type = 'attn1' refers to the equation in the paper.
For slightly different attention mechanisms refer to attn_type = 'attn2' or attn_type = 'attn3'
"""
super(MaskedEdgeAttention, self).__init__()
self.input_dim = input_dim
self.max_seq_len = max_seq_len
self.scalar = nn.Linear(self.input_dim, self.max_seq_len, bias=False)
self.matchatt = MatchingAttention(self.input_dim, self.input_dim, att_type='general2')
self.simpleatt = SimpleAttention(self.input_dim)
self.att = Attention(self.input_dim, score_function='mlp')
self.no_cuda = no_cuda
def forward(self, M, lengths, edge_ind):
"""
M -> (seq_len, batch, vector)
lengths -> length of the sequences in the batch
"""
attn_type = 'attn1'
if attn_type == 'attn1':
scale = self.scalar(M)
alpha = F.softmax(scale, dim=0).permute(1, 2, 0)
if not self.no_cuda:
mask = Variable(torch.ones(alpha.size()) * 1e-10).detach().cuda()
mask_copy = Variable(torch.zeros(alpha.size())).detach().cuda()
else:
mask = Variable(torch.ones(alpha.size()) * 1e-10).detach()
mask_copy = Variable(torch.zeros(alpha.size())).detach()
edge_ind_ = []
for i, j in enumerate(edge_ind):
for x in j:
edge_ind_.append([i, x[0], x[1]])
edge_ind_ = np.array(edge_ind_).transpose()
mask[edge_ind_] = 1
mask_copy[edge_ind_] = 1
masked_alpha = alpha * mask
_sums = masked_alpha.sum(-1, keepdim=True)
scores = masked_alpha.div(_sums) * mask_copy
return scores
elif attn_type == 'attn2':
scores = torch.zeros(M.size(1), self.max_seq_len, self.max_seq_len, requires_grad=True)
if not self.no_cuda:
scores = scores.cuda()
for j in range(M.size(1)):
ei = np.array(edge_ind[j])
for node in range(lengths[j]):
neighbour = ei[ei[:, 0] == node, 1]
M_ = M[neighbour, j, :].unsqueeze(1)
t = M[node, j, :].unsqueeze(0)
_, alpha_ = self.simpleatt(M_, t)
scores[j, node, neighbour] = alpha_
elif attn_type == 'attn3':
scores = torch.zeros(M.size(1), self.max_seq_len, self.max_seq_len, requires_grad=True)
if not self.no_cuda:
scores = scores.cuda()
for j in range(M.size(1)):
ei = np.array(edge_ind[j])
for node in range(lengths[j]):
neighbour = ei[ei[:, 0] == node, 1]
M_ = M[neighbour, j, :].unsqueeze(1).transpose(0, 1)
t = M[node, j, :].unsqueeze(0).unsqueeze(0).repeat(len(neighbour), 1, 1).transpose(0, 1)
_, alpha_ = self.att(M_, t)
scores[j, node, neighbour] = alpha_[0, :, 0]
return scores
class Attention(nn.Module):
def __init__(self, embed_dim, hidden_dim=None, out_dim=None, n_head=1, score_function='dot_product', dropout=0):
''' Attention Mechanism
:param embed_dim:
:param hidden_dim:
:param out_dim:
:param n_head: num of head (Multi-Head Attention)
:param score_function: scaled_dot_product / mlp (concat) / bi_linear (general dot)
:return (?, q_len, out_dim,)
'''
super(Attention, self).__init__()
if hidden_dim is None:
hidden_dim = embed_dim // n_head
if out_dim is None:
out_dim = embed_dim
self.embed_dim = embed_dim
self.hidden_dim = hidden_dim
self.n_head = n_head
self.score_function = score_function
self.w_k = nn.Linear(embed_dim, n_head * hidden_dim)
self.w_q = nn.Linear(embed_dim, n_head * hidden_dim)
self.proj = nn.Linear(n_head * hidden_dim, out_dim)
self.dropout = nn.Dropout(dropout)
if score_function == 'mlp':
self.weight = nn.Parameter(torch.Tensor(hidden_dim*2))
elif self.score_function == 'bi_linear':
self.weight = nn.Parameter(torch.Tensor(hidden_dim, hidden_dim))
else: # dot_product / scaled_dot_product
self.register_parameter('weight', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.hidden_dim)
if self.weight is not None:
self.weight.data.uniform_(-stdv, stdv)
def forward(self, k, q):
if len(q.shape) == 2: # q_len missing
q = torch.unsqueeze(q, dim=1)
if len(k.shape) == 2: # k_len missing
k = torch.unsqueeze(k, dim=1)
mb_size = k.shape[0] # ?
k_len = k.shape[1]
q_len = q.shape[1]
# k: (?, k_len, embed_dim,)
# q: (?, q_len, embed_dim,)
# kx: (n_head*?, k_len, hidden_dim)
# qx: (n_head*?, q_len, hidden_dim)
# score: (n_head*?, q_len, k_len,)
# output: (?, q_len, out_dim,)
kx = self.w_k(k).view(mb_size, k_len, self.n_head, self.hidden_dim)
kx = kx.permute(2, 0, 1, 3).contiguous().view(-1, k_len, self.hidden_dim)
qx = self.w_q(q).view(mb_size, q_len, self.n_head, self.hidden_dim)
qx = qx.permute(2, 0, 1, 3).contiguous().view(-1, q_len, self.hidden_dim)
if self.score_function == 'dot_product':
kt = kx.permute(0, 2, 1)
score = torch.bmm(qx, kt)
elif self.score_function == 'scaled_dot_product':
kt = kx.permute(0, 2, 1)
qkt = torch.bmm(qx, kt)
score = torch.div(qkt, math.sqrt(self.hidden_dim))
elif self.score_function == 'mlp':
kxx = torch.unsqueeze(kx, dim=1).expand(-1, q_len, -1, -1)
qxx = torch.unsqueeze(qx, dim=2).expand(-1, -1, k_len, -1)
kq = torch.cat((kxx, qxx), dim=-1) # (n_head*?, q_len, k_len, hidden_dim*2)
# kq = torch.unsqueeze(kx, dim=1) + torch.unsqueeze(qx, dim=2)
score = torch.tanh(torch.matmul(kq, self.weight))
elif self.score_function == 'bi_linear':
qw = torch.matmul(qx, self.weight)
kt = kx.permute(0, 2, 1)
score = torch.bmm(qw, kt)
else:
raise RuntimeError('invalid score_function')
# score = F.softmax(score, dim=-1)
score = F.softmax(score, dim=0)
# print (score)
# print (sum(score))
output = torch.bmm(score, kx) # (n_head*?, q_len, hidden_dim)
output = torch.cat(torch.split(output, mb_size, dim=0), dim=-1) # (?, q_len, n_head*hidden_dim)
output = self.proj(output) # (?, q_len, out_dim)
output = self.dropout(output)
return output, score
class SimpleAttention(nn.Module):
def __init__(self, input_dim):
super(SimpleAttention, self).__init__()
self.input_dim = input_dim
self.scalar = nn.Linear(self.input_dim, 1, bias=False)
def forward(self, M, x=None):
"""
:param M: (seq_len, batch, vector)
:param x: dummy argument for the compatibility with MatchingAttention
"""
scale = self.scalar(M) # (seq_len, batch, 1)
alpha = F.softmax(scale, dim=0).permute(1, 2, 0) # (batch, 1, seq_len)
attn_pool = torch.bmm(alpha, M.transpose(0, 1))[:, 0, :] # batch, vector
return attn_pool, alpha
class MatchingAttention(nn.Module):
def __init__(self, mem_dim, cand_dim, alpha_dim=None, att_type='general'):
super(MatchingAttention, self).__init__()
assert att_type != 'concat' or alpha_dim is not None
assert att_type != 'dot' or mem_dim == cand_dim
self.mem_dim = mem_dim
self.cand_dim = cand_dim
self.att_type = att_type
if att_type == 'general':
self.transform = nn.Linear(cand_dim, mem_dim, bias=False)
if att_type == 'general2':
self.transform = nn.Linear(cand_dim, mem_dim, bias=True)
# torch.nn.init.normal_(self.transform.weight,std=0.01)
elif att_type == 'concat':
self.transform = nn.Linear(cand_dim + mem_dim, alpha_dim, bias=False)
self.vector_prod = nn.Linear(alpha_dim, 1, bias=False)
def forward(self, M, x, mask=None):
""""
M -> (seq_len, batch, mem_dim)
x -> (batch, cand_dim)
mask -> (batch, seq_len)
"""
if type(mask) == type(None):
mask = torch.ones(M.size(1), M.size(0)).type(M.type())
if self.att_type == 'dot':
# vector = cand_dim = mem_dim
M_ = M.permute(1, 2, 0) # batch, vector, seqlen
x_ = x.unsqueeze(1) # batch, 1, vector
alpha = F.softmax(torch.bmm(x_, M_), dim=2) # batch, 1, seqlen
elif self.att_type == 'general':
M_ = M.permute(1, 2, 0) # batch, mem_dim, seqlen
x_ = self.transform(x).unsqueeze(1) # batch, 1, mem_dim
alpha = F.softmax(torch.bmm(x_, M_), dim=2) # batch, 1, seq len
elif self.att_type == 'general2':
M_ = M.permute(1, 2, 0) # batch, mem_dim, seq len
x_ = self.transform(x).unsqueeze(1) # batch, 1, mem_dim
mask_ = mask.unsqueeze(2).repeat(1, 1, self.mem_dim).transpose(1, 2) # batch, seq_len, mem_dim
M_ = M_ * mask_
alpha_ = torch.bmm(x_, M_) * mask.unsqueeze(1)
alpha_ = torch.tanh(alpha_)
alpha_ = F.softmax(alpha_, dim=2)
alpha_masked = alpha_ * mask.unsqueeze(1) # batch, 1, seqlen
alpha_sum = torch.sum(alpha_masked, dim=2, keepdim=True) # batch, 1, 1
alpha = alpha_masked / alpha_sum # batch, 1, 1 ; normalized
else: # concat
M_ = M.transpose(0, 1) # batch, seqlen, mem_dim
x_ = x.unsqueeze(1).expand(-1, M.size()[0], -1) # batch, seqlen, cand_dim
M_x_ = torch.cat([M_, x_], 2) # batch, seqlen, mem_dim+cand_dim
mx_a = F.tanh(self.transform(M_x_)) # batch, seqlen, alpha_dim
alpha = F.softmax(self.vector_prod(mx_a), 1).transpose(1, 2) # batch, 1, seqlen
attn_pool = torch.bmm(alpha, M.transpose(0, 1))[:, 0, :]
return attn_pool, alpha