-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (102 loc) · 4.44 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
import os
import subprocess
os.environ['PYTHONIOENCODING'] = 'utf-8'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
seed = 77
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
from utils.parser import get_parser
from utils.logger import get_logger
parser = get_parser()
option = parser.parse_args()
root_path = 'result'
logs_folder = os.path.join(root_path, 'logs', option.name)
save_folder = os.path.join(root_path, 'save', option.name)
sample_folder = os.path.join(root_path, 'sample', option.name)
result_folder = os.path.join(root_path, 'result', option.name)
subprocess.run('mkdir -p %s' % logs_folder, shell = True)
subprocess.run('mkdir -p %s' % save_folder, shell = True)
subprocess.run('mkdir -p %s' % sample_folder, shell = True)
subprocess.run('mkdir -p %s' % result_folder, shell = True)
logs_path = os.path.join(logs_folder, 'main.log' )
save_path = os.path.join(save_folder, 'best.ckpt')
logger = get_logger(option.name, logs_path)
from loaders.loader1 import get_loader as get_loader1
from loaders.loader2 import get_loader as get_loader2
from modules.module1 import get_module as get_module1
from modules.module2 import get_module as get_module2
from modules.module3 import get_module as get_module3
from modules.module4 import get_module as get_module4
from modules.module5 import get_module as get_module5
from modules.module6 import get_module as get_module6
from utils.misc import train, valid, save_checkpoint, load_checkpoint, save_sample
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
logger.info('prepare loader')
if (
option.module == 1 or
option.module == 2 or
option.module == 3 or
option.module == 5 or
option.module == 6
):
src_vocab, trg_vocab, train_loader, valid_loader, test_loader = get_loader1(option)
elif (
option.module == 4
):
src_vocab, trg_vocab, train_loader, valid_loader, test_loader = get_loader2(option)
logger.info('prepare module')
src_vocab_size = len(src_vocab['word2id'])
trg_vocab_size = len(trg_vocab['word2id'])
src_padded_idx = src_vocab['word2id'].get(src_vocab['special']['PAD_TOKEN'])
trg_padded_idx = trg_vocab['word2id'].get(trg_vocab['special']['PAD_TOKEN'])
if option.module == 1:
seq2seq = get_module1(option, src_vocab_size, trg_vocab_size)
elif option.module == 2:
seq2seq = get_module2(option, src_vocab_size, trg_vocab_size)
elif option.module == 3:
seq2seq = get_module3(option, src_vocab_size, trg_vocab_size)
elif option.module == 4:
seq2seq = get_module4(option, src_vocab_size, trg_vocab_size, src_padded_idx)
elif option.module == 5:
seq2seq = get_module5(option, src_vocab_size, trg_vocab_size, trg_padded_idx, device)
elif option.module == 6:
seq2seq = get_module6(option, src_vocab_size, trg_vocab_size, src_padded_idx, trg_padded_idx, device)
seq2seq = seq2seq.to(device)
logger.info('prepare envs')
optimizer = optim.Adam(seq2seq.parameters())
criterion = nn.CrossEntropyLoss(ignore_index = trg_padded_idx)
logger.info('start training!')
best_valid_loss = float('inf')
for epoch in range(option.num_epochs):
train_info = train(option.module, seq2seq, train_loader, criterion, optimizer, device, option.grad_clip)
valid_info = valid(option.module, seq2seq, valid_loader, criterion, optimizer, device, trg_vocab)
logger.info(
'[Epoch %d] Train Loss: %f, Valid Loss: %f, Valid BLEU: %f' %
(epoch, train_info['loss'], valid_info['loss'], valid_info['bleu'])
)
if best_valid_loss > valid_info['loss']:
best_valid_loss = valid_info['loss']
save_checkpoint(save_path, seq2seq, optimizer, epoch)
save_sample(sample_folder,
valid_info['reference_ids'], valid_info['hypothese_ids'],
valid_info['reference_wds'], valid_info['hypothese_wds']
)
logger.info('start testing!')
load_checkpoint(save_path, seq2seq, optimizer)
test_info = valid(option.module, seq2seq, test_loader, criterion, optimizer, device, trg_vocab)
logger.info(
'Test Loss: %f, Test BLEU: %f' %
(test_info['loss'], test_info['bleu'])
)
save_sample(result_folder,
test_info['reference_ids'], test_info['hypothese_ids'],
test_info['reference_wds'], test_info['hypothese_wds']
)