-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.py
347 lines (289 loc) · 11.4 KB
/
model.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import numpy as np
import math
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
import torch
import torch.nn.functional as F
from registry import register
from functools import partial
registry = {}
register = partial(register, registry=registry)
@register('rnn')
class RNN(nn.Module):
def __init__(self, args):
super().__init__()
self.dim = args.emb_dim
self.encoder_layer = args.encoder_layer
self.embedding = nn.Embedding(args.vocab_size, self.dim, padding_idx=0)
self.embedding.weight.requires_grad = True
self.rnn = nn.LSTM(input_size=self.dim, hidden_size=int(self.dim // 2), batch_first=True, num_layers=self.encoder_layer, bidirectional=True)
self.linear1 = nn.Linear(self.dim, self.dim)
self.linear2 = nn.Linear(self.dim, self.dim)
self.activiation = nn.Tanh()
self.dropout = nn.Dropout(p=args.drop_rate)
def forward(self, x, mask=None):
x_embed = self.embedding(x)
shape = x_embed.size()
mask = mask.squeeze().cpu().detach().numpy()
mask = [np.sum(e != 0) for e in mask]
# rnn pack
packed = pack_padded_sequence(x_embed, mask, batch_first=True, enforce_sorted=False)
encoder_outputs_packed, (h_last, c_last) = self.rnn(packed)
rnn_output, _ = pad_packed_sequence(encoder_outputs_packed, batch_first=True)
output = list()
for index in range(len(mask)):
temp = rnn_output[index, mask[index]-1, :]
output.append(temp)
output = torch.reshape(torch.cat(output, dim=0), (shape[0], self.dim))
output = self.dropout(output)
output = self.linear1(output)
output = self.activiation(output)
output = self.dropout(output)
output = self.linear2(output)
return output
@register('cnn')
class CNN(nn.Module):
def __init__(self, args):
super().__init__()
self.dim = args.emb_dim
self.embedding = nn.Embedding(args.vocab_size, self.dim, padding_idx=0)
self.embedding.weight.data.uniform_(-1, 1)
self.windows = [3, 4, 5]
self.convs = list()
for window in self.windows:
self.convs.append(nn.Conv2d(1, self.dim, (window, self.dim)).cuda())
self.dropout = torch.nn.Dropout(p=args.drop_rate)
self.proj = nn.Linear(len(self.windows)*self.dim, self.dim)
def forward(self, x, x_lens):
embeddings = self.embedding(x)
embeddings = embeddings.unsqueeze(1)
poolings = list()
for conv in self.convs:
conv_f = conv(embeddings)
conv_f = F.relu(conv_f.squeeze(3))
pooling = conv_f.max(dim=-1)[0]
poolings.append(pooling)
poolings = self.dropout(torch.cat(poolings, dim=-1))
new_embed = self.proj(poolings)
return new_embed
@register('attention')
class Attention(nn.Module):
def __init__(self, args):
super(Attention, self).__init__()
self.dim = args.emb_dim
self.embedding = nn.Embedding(args.vocab_size, self.dim, padding_idx=0)
self.embedding.weight.requires_grad = True
self.encoders = nn.ModuleList([OriAttention(args) for _ in range(1)])
self.sublayer = SublayerConnection(args.drop_rate, self.dim)
def forward(self, x, mask):
x = self.embedding(x)
for i, encoder in enumerate(self.encoders):
x = self.sublayer(x, lambda x: encoder(x, mask))
return x.masked_fill_(~mask, -float('inf')).max(dim=1)[0]
class OriAttention(nn.Module):
def __init__(self, args):
super().__init__()
self.head = 1
self.dropout = nn.Dropout(p=args.drop_rate)
def forward(self, x, mask):
q = x
k = x
v = x
q, k, v = (split_last(a, (self.head, -1)).transpose(1, 2)
for a in [q, k, v])
scores = torch.matmul(q, k.transpose(2, 3)) / (k.size(-1) ** 0.25)
mask = torch.matmul(mask.float(), mask.transpose(1, 2).float()).bool()
mask = mask.unsqueeze(1)
mask = mask.repeat([1, self.head, 1, 1])
scores.masked_fill_(~mask, -1e7)
scores = F.softmax(scores, dim=2)
scores = scores.transpose(2, 3)
v_ = torch.matmul(scores, v)
v_ = v_.transpose(1, 2).contiguous()
v_ = merge_last(v_, 2)
return v_
def l2norm(x):
return x / x.norm(p=2, dim=1, keepdim=True)
@register('pam')
class Pamela(nn.Module):
def __init__(self, args):
super(Pamela, self).__init__()
self.dim = args.emb_dim
self.encoder_layer = args.encoder_layer
self.embedding = nn.Embedding(args.vocab_size, self.dim, padding_idx=0)
self.embedding.weight.requires_grad = True
self.head = args.att_head_num
self.encoders = nn.ModuleList([Pamelaformer(args) for _ in range(self.encoder_layer)])
self.sublayer = SublayerConnection(args.drop_rate, self.dim)
def forward(self, x, mask):
x = self.embedding(x)
shape = list(x.size())
position = PositionalEncoding(shape[-1], shape[-2])
pos_att = position(x)
for i, encoder in enumerate(self.encoders):
x = self.sublayer(x, lambda x: encoder(x, mask, pos_att))
x = x.masked_fill_(~mask, 0).sum(dim=1)
return l2norm(x)
class Pamelaformer(nn.Module):
def __init__(self, args):
super().__init__()
self.self_attention = SAM(args)
self.pos_attention = PAM(args)
dim = args.emb_dim
proj_dim = args.emb_dim
self.merge = args.merge
if self.merge:
proj_dim = 2 * args.emb_dim
self.projection = nn.Sequential(
nn.Linear(proj_dim, dim),
nn.ReLU()
)
self.dropout = nn.Dropout(p=args.drop_rate)
def forward(self, x, mask, position, merge=True):
att = self.self_attention(x, mask)
pos = self.pos_attention(x, mask, position)
if self.merge:
c = self.projection(torch.cat([att, pos], dim=-1))
else:
c = self.projection(pos)
return c
class PAM(nn.Module):
def __init__(self, args):
super().__init__()
dim = args.emb_dim
self.head = args.att_head_num
self.projection = nn.Sequential(
nn.Linear(dim, dim),
nn.ReLU()
)
self.dropout = nn.Dropout(p=args.drop_rate)
def forward(self, x, mask, pos):
q = pos
k = pos
v = x
q, k, v = (split_last(a, (self.head, -1)).transpose(1, 2) for a in [q, k, v])
scores = torch.matmul(q, k.transpose(2, 3)) / (k.size(-1) ** 0.25)
mask = torch.matmul(mask.float(), mask.transpose(1, 2).float()).bool()
mask = mask.unsqueeze(1)
mask = mask.repeat([1, self.head, 1, 1])
scores.masked_fill_(~mask, -1e7)
scores = F.softmax(scores, dim=2)
scores = scores.transpose(2, 3)
v_ = torch.matmul(scores, v)
v_ = v_.transpose(1, 2).contiguous()
v_ = merge_last(v_, 2)
v_ = self.projection(v_)
return v_
class SAM(nn.Module):
def __init__(self, args):
super().__init__()
attention_dim = 32
self.head = args.att_head_num
hidden_size = args.emb_dim
self.projectionq = nn.Sequential(
nn.Linear(hidden_size, attention_dim),
nn.ReLU()
)
self.projectionk = nn.Sequential(
nn.Linear(hidden_size, attention_dim),
nn.ReLU()
)
self.dropout = nn.Dropout(p=args.drop_rate)
def forward(self, x, mask):
q = self.projectionq(x)
k = self.projectionk(x)
v = x
q, k, v = (split_last(a, (self.head, -1)).transpose(1, 2)
for a in [q, k, v])
scores = torch.matmul(q, k.transpose(2, 3)) / (k.size(-1) ** 0.25)
mask = torch.matmul(mask.float(), mask.transpose(1, 2).float()).bool()
mask = mask.unsqueeze(1)
mask = mask.repeat([1, self.head, 1, 1])
scores.masked_fill_(~mask, -1e7)
scores = F.softmax(scores, dim=2)
scores = scores.transpose(2, 3)
v_ = torch.matmul(scores, v)
v_ = v_.transpose(1, 2).contiguous()
v_ = merge_last(v_, 2)
return v_
def split_last(x, shape):
"split the last dimension to given shape"
shape = list(shape)
assert shape.count(-1) <= 1
if -1 in shape:
shape[shape.index(-1)] = int(x.size(-1) / -np.prod(shape))
return x.view(*x.size()[:-1], *shape)
def merge_last(x, n_dims):
"merge the last n_dims to a dimension"
s = x.size()
assert n_dims > 1 and n_dims < len(s)
return x.view(*s[:-n_dims], -1)
class SublayerConnection(nn.Module):
"""
A residual connection followed by a layer norm.
Note for code simplicity the norm is first as opposed to last.
"""
def __init__(self, dropout, dim):
super(SublayerConnection, self).__init__()
self.norm = LayerNorm(dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x, sublayer):
"Apply residual connection to any sublayer with the same size."
return x + self.dropout(self.norm(sublayer(x)))
class LayerNorm(nn.Module):
"Construct a layernorm module (See citation for details)."
def __init__(self, features, eps=1e-6):
super(LayerNorm, self).__init__()
self.a_2 = nn.Parameter(torch.ones(features))
self.b_2 = nn.Parameter(torch.zeros(features))
self.eps = eps
def forward(self, x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.a_2 * (x - mean) / (std + self.eps) + self.b_2
def cal_fixed_pos_att(max_len, window_size):
win = (window_size - 1) // 2
weight = float(1 / window_size)
attn_dict = dict()
for sen_len in range(1, max_len+1):
attn = np.eye(sen_len)
if sen_len < window_size:
attn_dict[sen_len] = attn
continue
for i in range(sen_len):
attn[i, i-win:i+win+1] = weight
attn[0, 0:win+1] = weight
attn_dict[sen_len] = torch.FloatTensor(attn)
return attn_dict
class PositionalAttCached(nn.Module):
def __init__(self, d_model, pos_attns, max_len=5000):
super(PositionalAttCached, self).__init__()
# Compute the positional encodings once in log space.
self.d_model = d_model
self.pos_attns = pos_attns
self.max_len = max_len
def forward(self, x):
shape = list(x.size())
pos_attn = self.pos_attns[shape[1]]
p_e = Variable(pos_attn, requires_grad=False).cuda()
p_e = p_e.repeat([shape[0], 1, 1])
return p_e
class PositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=5000):
super(PositionalEncoding, self).__init__()
# Compute the positional encodings once in log space.
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len).unsqueeze(1)
position = position * 1
div_term = torch.exp(torch.arange(0, d_model, 2) *
-(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):
shape = list(x.size())
p_e = Variable(self.pe[:, :x.size(1)], requires_grad=False).cuda()
p_e = p_e.repeat([shape[0], 1, 1])
return p_e