-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
336 lines (253 loc) · 12.4 KB
/
main.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
import os
import argparse
import time
from dataloader import get_train_data_loader, get_test_data_loader
from utils import seq2sen
from model import Encoder, Decoder
from nltk.translate.bleu_score import corpus_bleu
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import sys
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = open("results/log.txt", "w")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
#this flush method is needed for python 3 compatibility.
#this handles the flush command by doing nothing.
#you might want to specify some extra behavior here.
pass
def save_checkpoint(model, path):
model_state = {
'state_dict' : model.state_dict()
}
torch.save(model_state, path)
print('A check point has been generated : ' + path)
def main(args):
# constant definition
sos_idx = 0
eos_idx = 1
pad_idx = 2
a_dim = 512
h_dim = 512
attn_dim = 512
embed_dim = 512
regularize_constant = 1. # lambda * L => lambda = 1/L
vocabulary = torch.load(args.voca_path)
vocab_size = len(vocabulary)
device = torch.device("cuda" if(torch.cuda.is_available()) else "cpu")
encoder = Encoder().to(device)
decoder = Decoder(a_dim, h_dim, attn_dim, vocab_size, embed_dim).to(device)
# We do not train the encoder
encoder.eval()
if not args.test:
# train
validation_term = 1
best_bleu = 0.
num_of_epochs_since_improvement = 0
early_stop_criterion = 20
train_loader = get_train_data_loader(args.path, args.token_path, args.voca_path, args.batch_size, pad_idx)
valid_loader = get_test_data_loader(args.path, args.token_path, args.voca_path, args.batch_size, pad_idx, dataset_type = 'valid')
criterion = nn.CrossEntropyLoss(ignore_index = pad_idx)
optimizer = torch.optim.Adam(decoder.parameters(), lr = 0.0001)
print('Start training ...')
for epoch in range(args.epochs):
# early stopping
if num_of_epochs_since_improvement > early_stop_criterion :
print("There's no improvement on BLEU score while %d epochs"%(num_of_epochs_since_improvement))
print("Stop Training")
break
start_epoch = time.time()
i = 0
############################################################################################################################################
# training
decoder.train()
for src_batch, trg_batch in train_loader:
batch_start = time.time()
src_batch = src_batch.to(device)
trg_batch = torch.tensor(trg_batch).to(device)
trg_input = trg_batch[:,:-1]
trg_output = trg_batch[:,1:].contiguous().view(-1)
a = encoder(src_batch)
preds, alphas = decoder(a, trg_input) # [batch, C, vocab_size], [batch, C, L]
optimizer.zero_grad()
loss = criterion(preds.view(-1, preds.size(-1)), trg_output) # NLL loss
regularize_term = regularize_constant * ((1. - torch.sum(alphas, dim = 1)) ** 2).mean()
total_loss = loss + regularize_term
total_loss.backward()
optimizer.step()
i = i+1
# flush the GPU cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
batch_time = time.time() - batch_start
print('[%d/%d][%d/%d] train loss : %.4f (%.4f / %.4f) | time : %.2fs'%(epoch+1, args.epochs, i, train_loader.size//args.batch_size + 1, total_loss.item(), loss.item(), regularize_term.item(), batch_time))
epoch_time = time.time() - start_epoch
print('Time taken for %d epoch : %.2fs'%(epoch+1, epoch_time))
############################################################################################################################################
# validation
if i % validation_term == 0:
decoder.eval()
j = 0
pred, ref = [], []
for src_batch, trg_batch in valid_loader:
start = time.time()
batch_size = src_batch.size(0)
src_batch = src_batch.to(device) # [batch, 3, 244, 244]
trg_batch = torch.tensor(trg_batch).to(device) # [batch * 5, C]
trg_batch = torch.split(trg_batch, 5)
batches = []
for k in range(batch_size):
batches.append(trg_batch[k].unsqueeze(0))
trg_batch = torch.cat(batches, dim = 0) # [batch, 5, C]
max_length = trg_batch.size(-1)
pred_batch = torch.zeros(batch_size, 1, dtype = int).to(device) # [batch, 1] = [[0],[0],...,[0]]
# eos_mask[i] = 1 means i-th sentence has eos
eos_mask = torch.zeros(batch_size, dtype = int)
a = encoder(src_batch)
for _ in range(max_length):
output, _ = decoder(a, pred_batch) # [batch, _+1, vocab_size]
# greedy search
output = torch.argmax(F.softmax(output, dim = -1), dim = -1) # [batch_size, _+1]
predictions = output[:,-1].unsqueeze(1)
pred_batch = torch.cat([pred_batch, predictions], dim = -1)
for l in range(batch_size):
if predictions[l] == eos_idx:
eos_mask[l] = 1
# every sentence has eos
if eos_mask.sum() == batch_size :
break
# flush the GPU cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
pred += seq2sen(pred_batch.cpu().numpy().tolist(), vocabulary)
for m in range(batch_size):
ref += [seq2sen(trg_batch[m].cpu().numpy().tolist(), vocabulary)]
t = time.time() - start
j += 1
print("[%d/%d] prediction done | time : %.2fs"%(j, valid_loader.size // args.batch_size + 1, t))
bleu_1 = corpus_bleu(ref, pred, weights = (1./1.,)) * 100
bleu_2 = corpus_bleu(ref, pred, weights = (1./2., 1./2.,)) * 100
bleu_3 = corpus_bleu(ref, pred, weights = (1./3., 1./3., 1./3.,)) * 100
bleu_4 = corpus_bleu(ref, pred, weights = (1./4., 1./4., 1./4., 1./4.,)) * 100
print(f'BLEU-1: {bleu_1:.2f}')
print(f'BLEU-2: {bleu_2:.2f}')
print(f'BLEU-3: {bleu_3:.2f}')
print(f'BLEU-4: {bleu_4:.2f}')
if bleu_1 > best_bleu :
num_of_epochs_since_improvement = 0
best_bleu = bleu_1
print('Best BLEU-1 has been updated : %.2f'%(best_bleu))
save_checkpoint(decoder, 'checkpoints/best')
else :
num_of_epochs_since_improvement += validation_term
print("There's no improvement on BLEU score while %d epochs"%(num_of_epochs_since_improvement))
################################################################################################################################################################
print('End of the training')
else:
if os.path.exists(args.checkpoint):
decoder_checkpoint = torch.load(args.checkpoint)
decoder.load_state_dict(decoder_checkpoint['state_dict'])
print("trained decoder " + args.checkpoint + " is loaded")
decoder.eval()
# test
test_loader = get_test_data_loader(args.path, args.token_path, args.voca_path, args.batch_size, pad_idx)
j = 0
pred, ref = [], []
for src_batch, trg_batch in test_loader:
# predict pred_batch from src_batch with your model.
# every sentences in pred_batch should start with <sos> token (index: 0) and end with <eos> token (index: 1).
# every <pad> token (index: 2) should be located after <eos> token (index: 1).
# example of pred_batch:
# [[0, 5, 6, 7, 1],
# [0, 4, 9, 1, 2],
# [0, 6, 1, 2, 2]]
start = time.time()
batch_size = src_batch.size(0)
src_batch = src_batch.to(device) # [batch, 3, 244, 244]
trg_batch = torch.tensor(trg_batch).to(device) # [batch * 5, C]
trg_batch = torch.split(trg_batch, 5)
batches = []
for k in range(batch_size):
batches.append(trg_batch[k].unsqueeze(0))
trg_batch = torch.cat(batches, dim = 0) # [batch, 5, C]
max_length = trg_batch.size(-1)
pred_batch = torch.zeros(batch_size, 1, dtype = int).to(device) # [batch, 1] = [[0],[0],...,[0]]
# eos_mask[i] = 1 means i-th sentence has eos
eos_mask = torch.zeros(batch_size, dtype = int)
a = encoder(src_batch)
for _ in range(max_length):
output, _ = decoder(a, pred_batch) # [batch, _+1, vocab_size]
# greedy search
output = torch.argmax(F.softmax(output, dim = -1), dim = -1) # [batch_size, _+1]
predictions = output[:,-1].unsqueeze(1)
pred_batch = torch.cat([pred_batch, predictions], dim = -1)
for l in range(batch_size):
if predictions[l] == eos_idx:
eos_mask[l] = 1
# every sentence has eos
if eos_mask.sum() == batch_size :
break
# flush the GPU cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
pred += seq2sen(pred_batch.cpu().numpy().tolist(), vocabulary)
for m in range(batch_size):
ref += [seq2sen(trg_batch[m].cpu().numpy().tolist(), vocabulary)]
t = time.time() - start
j += 1
print("[%d/%d] prediction done | time : %.2fs"%(j, test_loader.size // args.batch_size + 1, t))
bleu_1 = corpus_bleu(ref, pred, weights = (1./1.,)) * 100
bleu_2 = corpus_bleu(ref, pred, weights = (1./2., 1./2.,)) * 100
bleu_3 = corpus_bleu(ref, pred, weights = (1./3., 1./3., 1./3.,)) * 100
bleu_4 = corpus_bleu(ref, pred, weights = (1./4., 1./4., 1./4., 1./4.,)) * 100
print(f'BLEU-1: {bleu_1:.2f}')
print(f'BLEU-2: {bleu_2:.2f}')
print(f'BLEU-3: {bleu_3:.2f}')
print(f'BLEU-4: {bleu_4:.2f}')
with open('results/pred.txt', 'w') as f:
for line in pred:
f.write('{}\n'.format(line))
with open('results/ref.txt', 'w') as f:
for lines in ref:
for line in lines:
f.write('{}\n'.format(line))
f.write('_'*50 + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='LAS')
parser.add_argument(
'--path',
type=str,
default='data/')
parser.add_argument(
'--token_path',
type=str,
default='preprocessed_data/tokens')
parser.add_argument(
'--voca_path',
type=str,
default='preprocessed_data/vocabulary')
parser.add_argument(
'--epochs',
type=int,
default=200)
parser.add_argument(
'--batch_size',
type=int,
default=32)
parser.add_argument(
'--test',
action='store_true')
parser.add_argument(
'--checkpoint',
type=str,
default='checkpoints/best'
)
args = parser.parse_args()
sys.stdout = Logger()
main(args)