-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattention.py
219 lines (184 loc) · 8.47 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
import math
import numpy
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter
from torch.nn import init
# Original dot product self-attention
class MultiHeadedAttention(nn.Module):
"""Multi-Head Attention layer
:param int n_head: the number of heads
:param int n_feat: the number of features
:param float dropout_rate: dropout rate
"""
def __init__(self, n_head, n_feat, dropout_rate):
super(MultiHeadedAttention, self).__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.linear_q = nn.Linear(n_feat, n_feat)
self.linear_k = nn.Linear(n_feat, n_feat)
self.linear_v = nn.Linear(n_feat, n_feat)
self.linear_out = nn.Linear(n_feat, n_feat)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Compute 'Scaled Dot Product Attention'
:param torch.Tensor query: (batch, time1, size)
:param torch.Tensor key: (batch, time2, size)
:param torch.Tensor value: (batch, time2, size)
:param torch.Tensor mask: (batch, time1, time2)
:return torch.Tensor: attentined and transformed `value` (batch, time1, d_model)
weighted by the query dot key attention (batch, head, time1, time2)
"""
n_batch = query.size(0)
q = self.linear_q(query).view(n_batch, -1, self.h, self.d_k)
k = self.linear_k(key).view(n_batch, -1, self.h, self.d_k)
v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
q = q.transpose(1, 2) # (batch, head, time1, d_k)
k = k.transpose(1, 2) # (batch, head, time2, d_k)
v = v.transpose(1, 2) # (batch, head, time2, d_k)
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.d_k) # (batch, head, time1, time2)
if mask is not None:
mask = mask.unsqueeze(1).eq(0) # (batch, 1, time1, time2)
min_value = float(numpy.finfo(torch.tensor(0, dtype=scores.dtype).numpy().dtype).min)
scores = scores.masked_fill(mask, min_value)
self.attn = torch.softmax(scores, dim=-1).masked_fill(mask, 0.0) # (batch, head, time1, time2)
else:
self.attn = torch.softmax(scores, dim=-1) # (batch, head, time1, time2)
p_attn = self.dropout(self.attn)
x = torch.matmul(p_attn, v) # (batch, head, time1, d_k)
x = x.transpose(1, 2).contiguous().view(n_batch, -1, self.h * self.d_k) # (batch, time1, d_model)
return self.linear_out(x) # (batch, time1, d_model)
class DenseSynthesizerAttention(nn.Module):
"""Dense Synthesizer attention layer
:param int n_head: the number of heads
:param int n_feat: the number of features
:param float dropout_rate: dropout rate
:param int time2: the number of time steps of target
"""
def __init__(self, n_head, n_feat, dropout_rate, time2):
super(DenseSynthesizerAttention, self).__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.time2 = time2
self.linear1 = nn.Linear(n_feat, n_feat)
self.linear2 = nn.Linear(n_feat, n_head*time2)
self.linear_v = nn.Linear(n_feat, n_feat)
self.linear_out = nn.Linear(n_feat, n_feat)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Compute 'Dense Synthesizer Attention'
:param torch.Tensor query: (batch, time1, size)
:param torch.Tensor key: not use
:param torch.Tensor value: (batch, time2, size)
:param torch.Tensor mask: (batch, time1, time2)
:return torch.Tensor: attentined and transformed `value` (batch, time1, d_model)
weighted by the dense attention (batch, head, time1, time2)
"""
n_batch = query.size(0)
B = self.linear2(F.relu(self.linear1(query))).view(n_batch, -1, self.h, self.time2)
v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
B = B.transpose(1, 2) # (batch, head, time1, time2)
v = v.transpose(1, 2) # (batch, head, time2, d_k)
if mask is not None:
mask = mask.unsqueeze(1).eq(0) # (batch, 1, time1, time2)
min_value = float(numpy.finfo(torch.tensor(0, dtype=B.dtype).numpy().dtype).min)
B = B.masked_fill(mask, min_value)
self.attn = torch.softmax(B, dim=-1).masked_fill(mask, 0.0) # (batch, head, time1, time2)
else:
self.attn = torch.softmax(B, dim=-1) # (batch, head, time1, time2)
p_attn = self.dropout(self.attn)
x = torch.matmul(p_attn, v) # (batch, head, time1, d_k)
x = x.transpose(1, 2).contiguous().view(n_batch, -1, self.h * self.d_k) # (batch, time1, d_model)
return self.linear_out(x) # (batch, time1, d_model)
class RandomSynthesizerAttention(nn.Module):
"""Random Synthesizer attention layer
:param int n_head: the number of head s
:param int n_feat: the number of features
:param float dropout_rate: dropout rate
:param int time1: the number of time steps of self
:param int time2: the number of time steps of target
"""
def __init__(self, n_head, n_feat, dropout_rate, time1, time2):
super(RandomSynthesizerAttention, self).__init__()
assert n_feat % n_head == 0
# We assume d_v always equals d_k
self.d_k = n_feat // n_head
self.h = n_head
self.time1 = time1
self.time2 = time2
self.attention_weight = Parameter(torch.Tensor(n_head*time1, time2))
init.kaiming_uniform_(self.attention_weight, a=math.sqrt(5))
self.linear_v = nn.Linear(n_feat, n_feat)
self.linear_out = nn.Linear(n_feat, n_feat)
self.attn = None
self.dropout = nn.Dropout(p=dropout_rate)
def forward(self, query, key, value, mask):
"""Compute 'Random Synthesizer Attention'
:param torch.Tensor query: not use
:param torch.Tensor key: not use
:param torch.Tensor value: (batch, time2, size)
:param torch.Tensor mask: (batch, time1, time2)
:return torch.Tensor: attentined and transformed `value` (batch, time1, d_model)
weighted by global attention (batch, head, time1, time2)
"""
n_batch = query.size(0)
v = self.linear_v(value).view(n_batch, -1, self.h, self.d_k)
v = v.transpose(1, 2) # (batch, head, time2, d_k)
self.attn = self.attention_weight.view(1, self.h, self.time1, self.time2) # (1, head, time1, time2)
self.attn = self.attn.repeat([n_batch, 1, 1, 1]) # (n_batch, head, time1, time2)
# Random Synthesizer Attention may not need mask
if mask is not None:
mask = mask.unsqueeze(1).eq(0) # (batch, 1, time1, time2)
min_value = float(numpy.finfo(torch.tensor(0, dtype=self.attn.dtype).numpy().dtype).min)
self.attn = self.attn.masked_fill(mask, min_value)
self.attn = torch.softmax(self.attn, dim=-1).masked_fill(mask, 0.0) # (batch, head, time1, time2)
else:
self.attn = torch.softmax(self.attn, dim=-1) # (batch, head, time1, time2)
p_attn = self.dropout(self.attn)
x = torch.matmul(p_attn, v) # (batch, head, time1, d_k)
x = x.transpose(1, 2).contiguous().view(n_batch, -1, self.h * self.d_k) # (batch, time1, d_model)
return self.linear_out(x) # (batch, time1, d_model)
if __name__ == '__main__':
denseattn = DenseSynthesizerAttention(n_head=2, n_feat=2, dropout_rate=0,
time2=6)
randomattn = RandomSynthesizerAttention(n_head=2, n_feat=2, dropout_rate=0,
time1=6, time2=6)
x1 = [
[
[1, 2],
[3, 1],
[5, 1],
[7, 2],
[1, 6],
[1, 1],
],
[
[1, 1],
[2, 1],
[6, 1],
[0, 0],
[0, 0],
[0, 0],
],
[
[3, 1],
[1, 2],
[6, 3],
[2, 3],
[0, 0],
[0, 0],
],
]
x1 = torch.Tensor(x1)
x2 = torch.randn((3, 6, 2))
y1 = denseattn(x1, x1, x1, None)
print(y1)
y2 = randomattn(x2, x2, x2, None)
print(y2)