/
models.py
324 lines (259 loc) · 11.8 KB
/
models.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
from collections import namedtuple
from typing import Callable
import torch
from torch import nn
import torch.nn.functional as F
from data import Vocabulary
Output = namedtuple('Output', ['logits'])
def _get_masked_mean(embedded: torch.Tensor, attention_mask: torch.LongTensor) -> torch.Tensor:
"""
Args:
embedded: batch_size, pad_len, embed_dim
attention_mask: batch_size, pad_len
Returns:
batch_size, embed_dim
"""
seq_len = attention_mask.sum(dim=1, keepdim=True) # batch_size, pad_len
weights = (attention_mask / seq_len).unsqueeze(2) # batch_size, pad_len, 1
weighted_mean = torch.mul(embedded, weights).sum(1) # batch_size, embed_dim
return weighted_mean
class WordAveragingModel(nn.Module):
def __init__(self, vocab_size: int, embed_dim: int, embed_dropout: float = 0.25,
pad_idx: int = Vocabulary.pad_idx):
"""
(Embedded) word averaging model.
Args:
vocab_size: Vocabulary size.
embed_dim: Word embedding dimension.
embed_dropout: Dropout applied on word embedding.
Default: 0.25
pad_idx: Index of padding token in vocabulary.
Default: Vocabulary.pad_idx
"""
super(WordAveragingModel, self).__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=pad_idx)
self.embed_dropout = nn.Dropout(embed_dropout)
self.fc = nn.Linear(embed_dim, 1)
init_range = 0.5 / embed_dim
self.embedding.weight.data.uniform_(-init_range, init_range)
self.embedding.weight.data[pad_idx].zero_()
def forward(self, input_ids: torch.LongTensor, attention_mask: torch.LongTensor) -> Output:
"""
Args:
input_ids: batch_size, pad_len
attention_mask: batch_size, pad_len
Returns:
Output with logits
"""
embedded = self.embed_dropout(self.embedding(input_ids)) # batch_size, pad_len, embed_dim
hidden = _get_masked_mean(embedded, attention_mask) # batch_size, embed_dim
logits = self.fc(hidden) # batch_size, 1
return Output(logits)
@property
def word_embedding(self) -> torch.Tensor:
"""
Embedded word vectors.
Returns:
vocab_size, embed_dim
"""
return self.embedding.weight.data
class AttentionWeightedWordAveragingModel(nn.Module):
def __init__(self, vocab_size: int, embed_dim: int,
attention: Callable[[torch.Tensor, torch.LongTensor], torch.Tensor],
res_conn: bool = False, embed_dropout: float = 0.25, pad_idx: int = Vocabulary.pad_idx):
"""
Adding attention weights on top of word averaging model.
Args:
vocab_size: Vocabulary size.
embed_dim: Word embedding dimension.
attention: Attention calculator.
res_conn: Whether apply residual connection to weighted
hidden state with average embedding. Default: False
embed_dropout: Dropout applied on word embedding.
Default: 0.25
pad_idx: Index of padding token in vocabulary.
Default: Vocabulary.pad_idx
"""
super(AttentionWeightedWordAveragingModel, self).__init__()
self.embedding = nn.Embedding(vocab_size, embed_dim, padding_idx=pad_idx)
self.embed_dropout = nn.Dropout(embed_dropout)
self.attention = attention
self.fc = nn.Linear(embed_dim, 1)
self.res_conn = res_conn
init_range = 0.5 / embed_dim
self.embedding.weight.data.uniform_(-init_range, init_range)
self.embedding.weight.data[pad_idx].zero_()
def forward(self, input_ids: torch.LongTensor, attention_mask: torch.LongTensor) -> Output:
"""
Args:
input_ids: batch_size, pad_len
attention_mask: batch_size, pad_len
Returns:
Output with logits
"""
embedded = self.embed_dropout(self.embedding(input_ids)) # batch_size, pad_len, embed_dim
attention = self.attention(embedded, attention_mask).unsqueeze(2) # batch_size, pad_len, 1
hidden = torch.mul(embedded, attention).sum(1) # batch_size, embed_dim
if self.res_conn:
embed_avg = _get_masked_mean(embedded, attention_mask) # batch_size, embed_dim
hidden += embed_avg
logits = self.fc(hidden) # batch_size, 1
return Output(logits)
class CosineSimilarityAttention(nn.Module):
def __init__(self, embed_dim: int):
"""
Attention computed from cosine similarity between embedded word
vector and u.
Args:
embed_dim: Word embedding dimension.
"""
super(CosineSimilarityAttention, self).__init__()
init_range = 0.5 / embed_dim
u_tensor = torch.Tensor(embed_dim)
u_tensor.uniform_(-init_range, init_range)
self.u = nn.Parameter(u_tensor)
def forward(self, embedded: torch.Tensor, attention_mask: torch.LongTensor) -> torch.Tensor:
"""
Args:
embedded: batch_size, pad_len, embed_dim
attention_mask: batch_size, pad_len
Returns:
batch_size, pad_len
"""
cosine = self.cosine_similarity_to_u(embedded) # batch_size, pad_len
masked = cosine.masked_fill(~attention_mask.bool(), float('-inf'))
attention = torch.softmax(masked, dim=1) # batch_size, pad_len
return attention
def cosine_similarity_to_u(self, embedded: torch.Tensor) -> torch.Tensor:
"""
Computes cosine similarity between embedded word vector and u.
Args:
embedded: *, embed_dim
Returns:
*
"""
return F.cosine_similarity(embedded, self.u, dim=-1)
def dot_product_self_attention(embedded: torch.Tensor, attention_mask: torch.LongTensor) -> torch.Tensor:
"""
Self attention computed from dot product with other embedded word
vectors in the same sequence.
Args:
embedded: batch_size, pad_len, embed_dim
attention_mask: batch_size, pad_len
Returns:
batch_size, pad_len
"""
summed_dot_prod = torch.bmm(embedded, embedded.transpose(1, 2)).sum(2) # batch_size, pad_len
masked = summed_dot_prod.masked_fill(~attention_mask.bool(), float('-inf'))
attention = torch.softmax(masked, dim=1) # batch_size, pad_len
return attention
class MultiHeadSelfAttention(nn.Module):
def __init__(self, model_dim: int, num_heads: int = 1):
"""
Multi-head self (identical input Q, K, V) attention.
Args:
model_dim: d_model.
num_heads: h.
"""
super(MultiHeadSelfAttention, self).__init__()
assert model_dim % num_heads == 0,\
f'Model dimension {model_dim} not divisible by number of heads {num_heads}'
head_dim = model_dim // num_heads
self.to_query = nn.Linear(model_dim, head_dim * num_heads, bias=False)
self.to_key = nn.Linear(model_dim, head_dim * num_heads, bias=False)
self.to_value = nn.Linear(model_dim, head_dim * num_heads, bias=False)
self.fc = nn.Linear(head_dim * num_heads, model_dim)
self.num_heads = num_heads
self.head_dim = head_dim
def forward(self, embedded: torch.Tensor, attention_mask: torch.LongTensor) -> torch.Tensor:
"""
Args:
embedded: batch_size, pad_len, model_dim
attention_mask: batch_size, pad_len
Returns:
batch_size, pad_len, model_dim
"""
batch_size = embedded.shape[0]
# batch_size, pad_len, num_heads, head_dim
splitted_shape = (batch_size, -1, self.num_heads, self.head_dim)
query = self.to_query(embedded).view(*splitted_shape)
key = self.to_key(embedded).view(*splitted_shape)
value = self.to_value(embedded).view(*splitted_shape)
# batch_size, num_heads, pad_len, pad_len
scaled_dot_prod = torch.einsum('bqnh,bknh->bnqk', query, key) / self.head_dim ** 0.5
mask = ~attention_mask[:, None, None, :].bool()
masked = scaled_dot_prod.masked_fill(mask, float('-inf'))
attention = F.softmax(masked, dim=-1)
attended = torch.einsum('bnqa,banh->bqnh', attention, value) # batch_size, pad_len, num_heads, head_dim
concated = attended.reshape(batch_size, -1, self.num_heads * self.head_dim)
# batch_size, pad_len, num_heads * head_dim
output = self.fc(concated) # batch_size, pad_len, model_dim
return output
class MultiHeadSelfAttentionModel(nn.Module):
def __init__(self, vocab_size: int, model_dim: int, num_heads: int = 1, pos_encode: bool = False,
embed_dropout: float = 0.25, attention_dropout: float = 0.25, pad_idx: int = Vocabulary.pad_idx):
"""
Model implementing multi-head self attention, from input to
the output of the first transformer encoder sublayer, then
mapped to a single logit for binary classification.
Args:
vocab_size: Vocabulary size.
model_dim: Word embedding dimension.
num_heads: Number of attention heads. Default: 1
pos_encode: Whether add positional encoding on embedded
sequence. Default: False
embed_dropout: Dropout applied on word embedding.
Default: 0.25
attention_dropout: Dropout applied on multi-head attention.
Default: 0.25
pad_idx: Index of padding token in vocabulary.
Default: Vocabulary.pad_idx
"""
super(MultiHeadSelfAttentionModel, self).__init__()
self.embedding = nn.Embedding(vocab_size, model_dim, padding_idx=pad_idx)
self.pos_encode = pos_encode
self.embed_dropout = nn.Dropout(embed_dropout)
self.multihead_attention = MultiHeadSelfAttention(model_dim, num_heads)
self.attention_dropout = nn.Dropout(attention_dropout)
self.layer_norm = nn.LayerNorm(model_dim)
self.fc = nn.Linear(model_dim, 1)
self.model_dim = model_dim
init_range = 0.5 / model_dim
self.embedding.weight.data.uniform_(-init_range, init_range)
self.embedding.weight.data[pad_idx].zero_()
def forward(self, input_ids: torch.LongTensor, attention_mask: torch.LongTensor) -> Output:
"""
Args:
input_ids: batch_size, pad_len
attention_mask: batch_size, pad_len
Returns:
Output with logits
"""
# batch_size, pad_len, model_dim
embedded = self.embedding(input_ids)
if self.pos_encode:
embedded += self.get_positional_encoding(input_ids.shape[1])
embedded = self.embed_dropout(embedded)
attention_out = self.attention_dropout(self.multihead_attention(embedded, attention_mask))
hidden = self.layer_norm(embedded + attention_out) # batch_size, pad_len, model_dim
hidden = _get_masked_mean(hidden, attention_mask) # batch_size, model_dim
logits = self.fc(hidden) # batch_size, 1
return Output(logits)
def get_positional_encoding(self, pad_len: int) -> torch.Tensor:
"""
Calculates positional encoding.
Args:
pad_len: Input sequence length.
Returns:
1, pad_len, model_dim
"""
pos = torch.arange(pad_len).float()
dim = torch.arange(self.model_dim)
frequency = 1 / 10000 ** (dim / self.model_dim)
pe = torch.matmul(pos[:, None], frequency[None, :]) # pad_len, model_dim
pe[:, 0::2] = torch.sin(pe[:, 0::2])
pe[:, 1::2] = torch.cos(pe[:, 1::2])
pe /= self.model_dim # to the same scale with embedding
w = next(self.parameters()) # move pe to the same device as model
pe = w.new_tensor(pe.tolist())
return pe[None, :, :] # 1, pad_len, model_dim