-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformerBlock.py
186 lines (150 loc) · 6.83 KB
/
TransformerBlock.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
import torch
import torch.nn.init as init
import torch.nn.functional as F
import torch.nn as nn
import copy
import math
def MultileayerModule(module, N):
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
class MultiHeadedAttention(nn.Module):
def __init__(self, h, d_model, dropout=0.1):
""" Take in model size and numbe of heads """
super(MultiHeadedAttention, self).__init__()
assert d_model % h == 0
self.d_k = d_model // h
self.h = h
self.linears = MultileayerModule(nn.Linear(d_model, d_model), 4)
self.attn = None
self.dropout = nn.Dropout(p=dropout)
self.init_weights()
def init_weights(self):
for l in self.linears:
init.xavier_normal_(l.weight)
def forward(self, query, key, value, mask=None):
if mask is not None:
mask = mask.unsqueeze(1)
nbatches = query.size(0)
# h x d_k
query, key, value = [l(x).view(nbatches, -1, self.h, self.d_k).transpose(1, 2)
for l, x in zip(self.linears, (query, key, value))]
x, self.attn = self.attention(query, key, value, dropout=self.dropout)
# concat
x = x.transpose(1, 2).contiguous().view(nbatches, -1, self.h * self.d_k)
outputs = self.linears[-1](x)
outputs = torch.squeeze(outputs)
return outputs
def attention(self, query, key, value, mask=None, dropout=None):
d_k = query.size(-1)
scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(d_k) # matmul矩阵相乘
if mask is not None:
scores = scores.masked_fill(mask == 0, -1e9)
p_attn = F.softmax(scores, dim=-1)
if dropout is not None:
p_attn = dropout(p_attn)
return torch.matmul(p_attn, value), p_attn
class PositionalEncoding(nn.Module):
"Implement the PE function."
def __init__(self, d_model, dropout, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1).float()
div_term = torch.exp(torch.arange(0, d_model, 2).float() * -(math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:, :x.size(1)]
return self.dropout(x)
class TransformerBlock(nn.Module):
def __init__(self, input_size, d_k=64, d_v=64, n_heads=2, is_layer_norm=True, attn_dropout=0.1):
super(TransformerBlock, self).__init__()
self.n_heads = n_heads
self.d_k = d_k if d_k is not None else input_size
self.d_v = d_v if d_v is not None else input_size
self.is_layer_norm = is_layer_norm
if is_layer_norm:
self.layer_morm = nn.LayerNorm(normalized_shape=input_size)
# self.pos_encoding = PositionalEncoding(d_model=input_size, dropout=0.5)
self.W_q = nn.Parameter(torch.Tensor(input_size, n_heads * d_k))
self.W_k = nn.Parameter(torch.Tensor(input_size, n_heads * d_k))
self.W_v = nn.Parameter(torch.Tensor(input_size, n_heads * d_v))
self.W_o = nn.Parameter(torch.Tensor(d_v*n_heads, input_size))
self.linear1 = nn.Linear(input_size, input_size)
self.linear2 = nn.Linear(input_size, input_size)
self.dropout = nn.Dropout(attn_dropout)
self.__init_weights__()
print(self)
def __init_weights__(self):
init.xavier_normal_(self.W_q)
init.xavier_normal_(self.W_k)
init.xavier_normal_(self.W_v)
init.xavier_normal_(self.W_o)
init.xavier_normal_(self.linear1.weight)
init.xavier_normal_(self.linear2.weight)
def FFN(self, X):
output = self.linear2(F.relu(self.linear1(X)))
output = self.dropout(output)
return output
def scaled_dot_product_attention(self, Q, K, V, mask, episilon=1e-6):
'''
:param Q: (*, max_q_words, n_heads, input_size)
:param K: (*, max_k_words, n_heads, input_size)
:param V: (*, max_v_words, n_heads, input_size)
:param mask: (*, max_q_words)
:param episilon:
:return:
'''
temperature = self.d_k ** 0.5
Q_K = torch.einsum("bqd,bkd->bqk", Q, K) / (temperature + episilon)
if mask is not None:
pad_mask = mask.unsqueeze(dim=-1).expand(-1, -1, K.size(1))
mask = torch.triu(torch.ones(pad_mask.size()), diagonal=1).bool().cuda()
mask_ = mask + pad_mask
Q_K = Q_K.masked_fill(mask_, -2**32+1)
Q_K_score = F.softmax(Q_K, dim=-1) # (batch_size, max_q_words, max_k_words)
Q_K_score = self.dropout(Q_K_score)
V_att = Q_K_score.bmm(V) # (*, max_q_words, input_size)
return V_att
def multi_head_attention(self, Q, K, V, mask):
'''
:param Q:
:param K:
:param V:
:param mask: (bsz, max_q_words)
:return:
'''
bsz, q_len, _ = Q.size()
bsz, k_len, _ = K.size()
bsz, v_len, _ = V.size()
Q_ = Q.matmul(self.W_q).view(bsz, q_len, self.n_heads, self.d_k)
K_ = K.matmul(self.W_k).view(bsz, k_len, self.n_heads, self.d_k)
V_ = V.matmul(self.W_v).view(bsz, v_len, self.n_heads, self.d_v)
Q_ = Q_.permute(0, 2, 1, 3).contiguous().view(bsz*self.n_heads, q_len, self.d_k)
K_ = K_.permute(0, 2, 1, 3).contiguous().view(bsz*self.n_heads, q_len, self.d_k)
V_ = V_.permute(0, 2, 1, 3).contiguous().view(bsz*self.n_heads, q_len, self.d_v)
if mask is not None:
mask = mask.unsqueeze(dim=1).expand(-1, self.n_heads, -1) # For head axis broadcasting.
mask = mask.reshape(-1, mask.size(-1))
V_att = self.scaled_dot_product_attention(Q_, K_, V_, mask)
V_att = V_att.view(bsz, self.n_heads, q_len, self.d_v)
V_att = V_att.permute(0, 2, 1, 3).contiguous().view(bsz, q_len, self.n_heads*self.d_v)
output = self.dropout(V_att.matmul(self.W_o)) # (batch_size, max_q_words, input_size)
return output
def forward(self, Q, K, V, mask=None):
'''
:param Q: (batch_size, max_q_words, input_size)
:param K: (batch_size, max_k_words, input_size)
:param V: (batch_size, max_v_words, input_size)
:return: output: (batch_size, max_q_words, input_size) same size as Q
'''
V_att = self.multi_head_attention(Q, K, V, mask)
if self.is_layer_norm:
X = self.layer_morm(Q + V_att) # (batch_size, max_r_words, embedding_dim)
output = self.layer_morm(self.FFN(X) + X)
else:
X = Q + V_att
output = self.FFN(X) + X
return output