-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
599 lines (463 loc) · 21.7 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import torch
import torch.nn as nn
from torch import Tensor
import torch.optim as optim
class Encoder(nn.Module):
"""Encoder
Encoder without Attention
"""
def __init__(self, emb_dim:int, hid_dim:int, v_size:int, device:int, num_layers:int, batch_first:bool, dropout_rate:float):
"""init
initialize encoder
Args:
emb_dim (int): the dimension of embedded word vector
hid_dim (int): the dimension of hidden state of LSTM
v_size (int): the size of the input vocabulary
device (torch.device): cpu or cuda
num_layers (int): the number of LSTM layers
batch_first (bool): set to False
dropout_rate (float): dropout rate between different LSTM layers
"""
super(Encoder, self).__init__()
self.device = device
self.hid_dim = hid_dim
self.embed = nn.Embedding(v_size, emb_dim)
self.lstm = nn.LSTM(input_size=emb_dim, hidden_size=hid_dim, num_layers=num_layers, batch_first=batch_first, dropout=dropout_rate, bidirectional=False) # many to one
def forward(self, text:Tensor):
"""forward
forward process
Args:
text (Tensor): a batch of input sentences
Returns:
state (Tensor): the last hidden state and cell state of LSTM
"""
embedding = self.embed(text) # (text_len, batch_size) -> (text_len, batch_size, emb_dim)
_, state = self.lstm(embedding) # (text_len, batch_size, emb_dim) -> _, ((num_layers, batch_size, hid_dim), (num_layers, batch_size, hid_dim)) # state = (h_n, c_n)
return state
class Encoder_Attention(nn.Module):
"""Encoder
Encoder with Attention
"""
def __init__(self, emb_dim:int, hid_dim:int, v_size:int, device:int, num_layers:int, batch_first:bool, dropout_rate:float):
"""init
initialize encoder
Args:
emb_dim (int): the dimension of embedded word vector
hid_dim (int): the dimension of hidden state of LSTM
v_size (int): the size of the input vocabulary
device (torch.device): cpu or cuda
num_layers (int): the number of LSTM layers
batch_first (bool): set to False
dropout_rate (float): dropout rate between different LSTM layers
"""
super(Encoder_Attention, self).__init__()
self.device = device
self.hid_dim = hid_dim
self.embed = nn.Embedding(v_size, emb_dim)
self.lstm = nn.LSTM(input_size=emb_dim, hidden_size=hid_dim, num_layers=num_layers, batch_first=batch_first, dropout=dropout_rate, bidirectional=False) # many to one
def forward(self, text:Tensor):
"""forward
forward process
Args:
text (Tensor): a batch of input sentences
Returns:
output (Tensor): all the hidden states
state (Tensor): the last hidden state and cell state of LSTM
"""
embedding = self.embed(text) # (text_len, batch_size) -> (text_len, batch_size, emb_dim)
output, state = self.lstm(embedding) # (text_len, batch_size, emb_dim) -> (text_len, batch_size, hid_dim), ((num_layers, batch_size, hid_dim), (num_layers, batch_size, hid_dim)) # state = (h_n, c_n)
return output, state
class Decoder(nn.Module):
"""Decoder
Decoder without Attention
"""
def __init__(self, emb_dim:int, hid_dim:int, v_size:int, device:int, num_layers:int, batch_first:bool, dropout_rate:float, output_dropout_rate:float):
"""init
initialize decoder
Args:
emb_dim (int): the dimension of embedded word vector
hid_dim (int): the dimension of hidden state of LSTM
v_size (int): the size of the output vocabulary
device (torch.device): cpu or cuda
num_layers (int): the number of LSTM layers
batch_first (bool): set to False
dropout_rate (float): dropout rate between different LSTM layers
output_dropout_rate (float): dropout rate after the last LSTM layer
"""
super(Decoder, self).__init__()
self.device = device
self.hid_dim = hid_dim
self.v_size = v_size
self.embed = nn.Embedding(v_size, hid_dim)
self.lstm = nn.LSTM(input_size=emb_dim, hidden_size=hid_dim, num_layers=num_layers, batch_first=batch_first, dropout=dropout_rate, bidirectional=False) # many to many
self.dropout = nn.Dropout(output_dropout_rate)
self.linear = nn.Linear(hid_dim, v_size)
# self.softmax = nn.LogSoftmax(dim=1)
self.softmax = nn.Softmax(dim=1)
def forward(self, input:Tensor, hidden:Tensor):
"""forward
forward one step
Args:
input (Tensor): input word at a time
hidden (Tensor): the last hidden state and cell state of the encoder
Returns:
output (Tensor): output distribution
hidden (Tensor): hidden state and cell state
"""
embedding = self.embed(input) # (batch_size) -> (batch_size, hid_dim)
output, hidden = self.lstm(embedding.unsqueeze(0), hidden) # ((1, batch_size, hid_dim), (h, c)) -> ((1, batch_size, hid_size), (h, c))
output = self.dropout(output.squeeze(0)) # (1, batch_size, hid_size) -> (batch_size, hid_size)
output = self.linear(output) # (batch_size, hid_size) -> (batch_size, v_size)
output = self.softmax(output) # (batch_size, v_size) -> (batch_size, v_size)
return output, hidden
class Decoder_Attention(nn.Module):
"""Decoder
Decoder with Attention
"""
def __init__(self, emb_dim:int, hid_dim:int, v_size:int, device:int, num_layers:int, batch_first:bool, dropout_rate:float, output_dropout_rate:float):
"""init
initialize decoder
Args:
emb_dim (int): the dimension of embedded word vector
hid_dim (int): the dimension of hidden state of LSTM
v_size (int): the size of the output vocabulary
device (torch.device): cpu or cuda
num_layers (int): the number of LSTM layers
batch_first (bool): set to False
dropout_rate (float): dropout rate between different LSTM layers
output_dropout_rate (float): dropout rate after the last LSTM layer
"""
super(Decoder_Attention, self).__init__()
self.device = device
self.hid_dim = hid_dim
self.v_size = v_size
self.embed = nn.Embedding(v_size, hid_dim)
self.lstm = nn.LSTM(input_size=emb_dim, hidden_size=hid_dim, num_layers=num_layers, batch_first=batch_first, dropout=dropout_rate, bidirectional=False) # many to many
self.W1 = torch.nn.Linear(hid_dim, hid_dim, False)
self.W2 = torch.nn.Linear(hid_dim, hid_dim, False)
self.Tanh = nn.Tanh()
self.dropout = nn.Dropout(output_dropout_rate)
self.linear = nn.Linear(hid_dim, v_size)
# self.softmax = nn.LogSoftmax(dim=1)
self.softmax = nn.Softmax(dim=1)
def forward(self, input:Tensor, hidden:Tensor, encoder_output:Tensor):
"""forward
forward one step
Args:
input (Tensor): input word at a time
hidden (Tensor): the last hidden state and cell state of the encoder
encoder_output (Tensor): all the hidden states of the encoder
Returns:
output (Tensor): output distribution
hidden (Tensor): hidden state and cell state
"""
embedding = self.embed(input) # (batch_size) -> (batch_size, hid_dim)
output, hidden = self.lstm(embedding.unsqueeze(0), hidden) # ((1, batch_size, hid_dim), (h, c)) -> ((1, batch_size, hid_size), (h, c))
text_len = encoder_output.shape[0]
extend_output = output.repeat(text_len, 1, 1) # (text_len, batch_size, hid_dim)
product = torch.bmm(extend_output.view(-1, 1, self.hid_dim), encoder_output.view(-1, self.hid_dim, 1)) # (text_len * batch_size, 1, 1)
weight = product.view(text_len, -1, 1).repeat(1, 1, self.hid_dim) # (text_len, batch_size, hid_dim)
weighted_encoder_output = weight * encoder_output # (text_len, batch_size, hid_dim)
c = weighted_encoder_output.sum(dim=0) # (batch_size, hid_dim)
output = self.Tanh(self.W1(output) + self.W2(c)) # (batch_size, hid_dim)
output = self.dropout(output.squeeze(0)) # (1, batch_size, hid_size) -> (batch_size, hid_size)
output = self.linear(output) # (batch_size, hid_size) -> (batch_size, v_size)
output = self.softmax(output) # (batch_size, v_size) -> (batch_size, v_size)
return output, hidden
class Seq2Seq(nn.Module):
"""Seq2seq model
seq2seq model
"""
def __init__(self, encoder: nn.Module, decoder:nn.Module, device:int, form_field, scheduled_sampling=1.0):
"""init
Initialize seq2seq model
Args:
encoder (nn.Module): encoder
decoder (nn.Module): decoder
device (torch.device): cpu or cuda
form_field (torchtext.data.Field): form_field
scheduled_sampling (float): To what rate use golden answer while training
"""
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.device = device
self.form_field = form_field
self.scheduled_sampling = scheduled_sampling
def train_forward(self, text:Tensor, form:Tensor) -> Tensor:
"""train forward
train forward process
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
Returns:
outputs (Tensor): distributions of words at each time
"""
batch_size = text.shape[1]
max_len = form.shape[0]
form_v_size = self.decoder.v_size
outputs = torch.zeros(max_len, batch_size, form_v_size).to(self.device) # softmax outputs
hidden = self.encoder(text) # (text_len, batch_size, emb_dim) -> ((num_layers, batch_size, hid_dim), (num_layers, batch_size, hid_dim))
outputs[0] = torch.nn.functional.one_hot(form[0, :], num_classes=form_v_size)
for t in range(1, max_len):
ans = torch.where((torch.rand(batch_size) < self.scheduled_sampling).to(self.device), form[t-1, :], outputs[t-1].argmax(1))
out, hidden = self.decoder(ans, hidden)
outputs[t] = out
return outputs # (max_len, batch_size, form_v_size)
def test_forward(self, text:Tensor, form:Tensor) -> Tensor:
"""test forward
test forward process without beam search
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
Returns:
outputs (Tensor): distributions of words at each time
predicts (Tensor): prediction
"""
batch_size = text.shape[1]
max_len = form.shape[0]
form_v_size = self.decoder.v_size
outputs = torch.zeros(max_len, batch_size, form_v_size) # softmax outputs
predicts = torch.zeros(max_len, batch_size, dtype=int)
hidden = self.encoder(text)
outputs[0] = torch.nn.functional.one_hot(form[0, :], num_classes=form_v_size)
predicts[0] = form[0, :]
dec_in = form[0, :]
for t in range(1, max_len):
out, hidden = self.decoder(dec_in, hidden)
outputs[t] = out
dec_in = out.argmax(1)
predicts[t] = dec_in
return outputs, predicts
def test_forward_with_ans(self, text:Tensor, form:Tensor) -> Tensor:
"""test forward
test forward process with right answers without beam search
(used for checking whether training is done properly)
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
Returns:
outputs (Tensor): distributions of words at each time
predicts (Tensor): prediction
"""
batch_size = text.shape[1]
max_len = form.shape[0]
form_v_size = self.decoder.v_size
outputs = torch.zeros(max_len, batch_size, form_v_size) # softmax outputs
predicts = torch.zeros(max_len, batch_size, dtype=int)
hidden = self.encoder(text)
outputs[0] = torch.nn.functional.one_hot(form[0, :], num_classes=form_v_size)
predicts[0] = form[0, :]
dec_in = form[0, :]
for t in range(1, max_len):
# out, hidden = self.decoder(dec_in, hidden)
out, hidden = self.decoder(form[t-1, :], hidden)
outputs[t] = out
dec_in = out.argmax(1)
predicts[t] = dec_in
return outputs, predicts
def test_forward_beam(self, text:Tensor, form:Tensor, keep_dim:int) -> Tensor:
"""test forward
test forward process with beam search (batch size should be set to 1)
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
keep_dim (int): how many candidates to keep at each time
Returns:
outputs (Tensor): distributions of words at each time
predicts (Tensor): prediction
"""
batch_size = text.shape[1]
if batch_size != 1:
raise ValueError(f'batch_size should be 1 but given batch_size is {batch_size}')
form_v_size = self.decoder.v_size
states = self.encoder(text) # ((num_layers, batch_size, hid_dim), (num_layers, batch_size, hid_dim))
topk = [{
'output': [self.form_field.vocab.stoi['<S>']],
'log_prob': 0.0,
'states': states
}]
for t in range(1, 50):
candidate = []
for keep in topk:
# reach end of sentence
if keep['output'][-1] == self.form_field.vocab.stoi['<E>']:
candidate.append(keep)
continue
out, hidden = self.decoder(torch.tensor([keep['output'][-1]]).to(self.device), keep['states'])
log_out = torch.log(out).squeeze(dim=0)
for i in range(form_v_size):
candidate.append({
'output': keep['output'] + [i],
'log_prob': keep['log_prob'] + log_out[i].item(),
'states': hidden
})
candidate.sort(key=lambda x: x['log_prob'], reverse=True)
topk = candidate[0:keep_dim]
return topk
class Seq2Seq_Attention(nn.Module):
"""Seq2seq model with attention
seq2seq model with attention
"""
def __init__(self, encoder: nn.Module, decoder:nn.Module, device:int, form_field, scheduled_sampling):
"""init
Initialize seq2seq model
Args:
encoder (nn.Module): encoder
decoder (nn.Module): decoder
device (torch.device): cpu or cuda
form_field (torchtext.data.Field): form_field
scheduled_sampling (float): To what rate use golden answer while training
"""
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.device = device
self.form_field = form_field
self.scheduled_sampling = scheduled_sampling
def train_forward(self, text:Tensor, form:Tensor) -> Tensor:
"""train forward
train forward process
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
Returns:
outputs (Tensor): distributions of words at each time
"""
batch_size = text.shape[1]
max_len = form.shape[0]
form_v_size = self.decoder.v_size
outputs = torch.zeros(max_len, batch_size, form_v_size).to(self.device) # softmax outputs
enc_output, hidden = self.encoder(text) # (text_len, batch_size, emb_dim) -> _, ((num_layers, batch_size, hid_dim), (num_layers, batch_size, hid_dim))
outputs[0] = torch.nn.functional.one_hot(form[0, :], num_classes=form_v_size)
for t in range(1, max_len):
ans = torch.where((torch.rand(batch_size) < self.scheduled_sampling).to(self.device), form[t-1, :], outputs[t-1].argmax(1))
out, hidden = self.decoder(ans, hidden, enc_output)
outputs[t] = out
return outputs # (max_len, batch_size, form_v_size)
def test_forward(self, text:Tensor, form:Tensor) -> Tensor:
"""test forward
test forward process without beam search
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
Returns:
outputs (Tensor): distributions of words at each time
predicts (Tensor): prediction
"""
batch_size = text.shape[1]
max_len = form.shape[0]
form_v_size = self.decoder.v_size
outputs = torch.zeros(max_len, batch_size, form_v_size) # softmax outputs
predicts = torch.zeros(max_len, batch_size, dtype=int)
enc_output, hidden = self.encoder(text)
outputs[0] = torch.nn.functional.one_hot(form[0, :], num_classes=form_v_size)
predicts[0] = form[0, :]
dec_in = form[0, :]
for t in range(1, max_len):
out, hidden = self.decoder(dec_in, hidden, enc_output)
# out, hidden = self.decoder(form[t, :], hidden)
outputs[t] = out
dec_in = out.argmax(1)
predicts[t] = dec_in
return outputs, predicts
def test_forward_with_ans(self, text:Tensor, form:Tensor) -> Tensor:
"""test forward
test forward process with right answers without beam search
(used for checking whether training is done properly)
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
Returns:
outputs (Tensor): distributions of words at each time
predicts (Tensor): prediction
"""
batch_size = text.shape[1]
max_len = form.shape[0]
form_v_size = self.decoder.v_size
outputs = torch.zeros(max_len, batch_size, form_v_size) # softmax outputs
predicts = torch.zeros(max_len, batch_size, dtype=int)
enc_output, hidden = self.encoder(text)
outputs[0] = torch.nn.functional.one_hot(form[0, :], num_classes=form_v_size)
predicts[0] = form[0, :]
dec_in = form[0, :]
for t in range(1, max_len):
# out, hidden = self.decoder(dec_in, hidden)
out, hidden = self.decoder(form[t-1, :], hidden, enc_output)
outputs[t] = out
dec_in = out.argmax(1)
predicts[t] = dec_in
return outputs, predicts
def test_forward_beam(self, text:Tensor, form:Tensor, keep_dim:int) -> Tensor:
"""test forward
test forward process with beam search (batch size should be set to 1)
Args:
text (Tensor): a batch of input sentences
form (Tensor): a batch of output sentences
keep_dim (int): how many candidates to keep at each time
Returns:
outputs (Tensor): distributions of words at each time
predicts (Tensor): prediction
"""
batch_size = text.shape[1]
if batch_size != 1:
raise ValueError(f'batch_size should be 1 but given batch_size is {batch_size}')
max_len = form.shape[0]
form_v_size = self.decoder.v_size
enc_output, states = self.encoder(text) # ((num_layers, batch_size, hid_dim), (num_layers, batch_size, hid_dim))
topk = [{
'output': [self.form_field.vocab.stoi['<S>']],
'log_prob': 0.0,
'states': states
}]
for t in range(1, 50):
candidate = []
for keep in topk:
# reach end of sentence
if keep['output'][-1] == self.form_field.vocab.stoi['<E>']:
candidate.append(keep)
continue
out, hidden = self.decoder(torch.tensor([keep['output'][-1]]).to(self.device), keep['states'], enc_output)
log_out = torch.log(out).squeeze(dim=0)
for i in range(form_v_size):
candidate.append({
'output': keep['output'] + [i],
'log_prob': keep['log_prob'] + log_out[i].item(),
'states': hidden
})
candidate.sort(key=lambda x: x['log_prob'], reverse=True)
topk = candidate[0:keep_dim]
return topk
def init_weights(m: nn.Module):
"""init weights of a model
init weights of a model
Args:
m (nn.Module): model
"""
for name, param in m.named_parameters():
if 'weight' in name:
nn.init.uniform_(param.data, -0.08, 0.08)
else:
nn.init.constant_(param.data, 0)
def generate_criterion_and_optimzier(ignore_index, parameters, lr, alpha):
"""generate criterion and optimizer
generate criterion and optimzier
Args:
ignore_index (int): index to ignore when calculating cross entropy loss
lr (float): learning rate
alpha (float): hyperparameter for RMSprop
Returns:
criterion (nn.CrossEntropyLoss): cross entropy loss
optimzier (nn.RMSprop): RMSprop
"""
criterion = nn.CrossEntropyLoss(ignore_index=ignore_index)
optimizer = optim.RMSprop(parameters, lr=lr, alpha=alpha)
return criterion, optimizer
def save_model(model, path):
"""save model
save model
Args:
model (nn.Module): model to save
path (string): where to save
"""
torch.save(model.state_dict(), path)