-
Notifications
You must be signed in to change notification settings - Fork 44
/
train.py
175 lines (154 loc) · 7.12 KB
/
train.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
#!/usr/bin/env python
# coding:utf-8
import helper.logger as logger
from models.model import HiAGM
import torch
import sys
from helper.configure import Configure
import os
from data_modules.data_loader import data_loaders
from data_modules.vocab import Vocab
from train_modules.criterions import ClassificationLoss
from train_modules. trainer import Trainer
from helper.utils import load_checkpoint, save_checkpoint
import time
def set_optimizer(config, model):
"""
:param config: helper.configure, Configure Object
:param model: computational graph
:return: torch.optim
"""
params = model.optimize_params_dict()
if config.train.optimizer.type == 'Adam':
return torch.optim.Adam(lr=config.train.optimizer.learning_rate,
params=params)
else:
raise TypeError("Recommend the Adam optimizer")
def train(config):
"""
:param config: helper.configure, Configure Object
"""
# loading corpus and generate vocabulary
corpus_vocab = Vocab(config,
min_freq=5,
max_size=50000)
# get data
train_loader, dev_loader, test_loader = data_loaders(config, corpus_vocab)
# build up model
hiagm = HiAGM(config, corpus_vocab, model_type=config.model.type, model_mode='TRAIN')
hiagm.to(config.train.device_setting.device)
# define training objective & optimizer
criterion = ClassificationLoss(os.path.join(config.data.data_dir, config.data.hierarchy),
corpus_vocab.v2i['label'],
recursive_penalty=config.train.loss.recursive_regularization.penalty,
recursive_constraint=config.train.loss.recursive_regularization.flag)
optimize = set_optimizer(config, hiagm)
# get epoch trainer
trainer = Trainer(model=hiagm,
criterion=criterion,
optimizer=optimize,
vocab=corpus_vocab,
config=config)
# set origin log
best_epoch = [-1, -1]
best_performance = [0.0, 0.0]
model_checkpoint = config.train.checkpoint.dir
model_name = config.model.type
wait = 0
if not os.path.isdir(model_checkpoint):
os.mkdir(model_checkpoint)
else:
# loading previous checkpoint
dir_list = os.listdir(model_checkpoint)
dir_list.sort(key=lambda fn: os.path.getatime(os.path.join(model_checkpoint, fn)))
latest_model_file = ''
for model_file in dir_list[::-1]:
if model_file.startswith('best'):
continue
else:
latest_model_file = model_file
break
if os.path.isfile(os.path.join(model_checkpoint, latest_model_file)):
logger.info('Loading Previous Checkpoint...')
logger.info('Loading from {}'.format(os.path.join(model_checkpoint, latest_model_file)))
best_performance, config = load_checkpoint(model_file=os.path.join(model_checkpoint, latest_model_file),
model=hiagm,
config=config,
optimizer=optimize)
logger.info('Previous Best Performance---- Micro-F1: {}%, Macro-F1: {}%'.format(
best_performance[0], best_performance[1]))
# train
for epoch in range(config.train.start_epoch, config.train.end_epoch):
start_time = time.time()
trainer.train(train_loader,
epoch)
trainer.eval(train_loader, epoch, 'TRAIN')
performance = trainer.eval(dev_loader, epoch, 'DEV')
# saving best model and check model
if not (performance['micro_f1'] >= best_performance[0] or performance['macro_f1'] >= best_performance[1]):
wait += 1
if wait % config.train.optimizer.lr_patience == 0:
logger.warning("Performance has not been improved for {} epochs, updating learning rate".format(wait))
trainer.update_lr()
if wait == config.train.optimizer.early_stopping:
logger.warning("Performance has not been improved for {} epochs, stopping train with early stopping"
.format(wait))
break
if performance['micro_f1'] > best_performance[0]:
wait = 0
logger.info('Improve Micro-F1 {}% --> {}%'.format(best_performance[0], performance['micro_f1']))
best_performance[0] = performance['micro_f1']
best_epoch[0] = epoch
save_checkpoint({
'epoch': epoch,
'model_type': config.model.type,
'state_dict': hiagm.state_dict(),
'best_performance': best_performance,
'optimizer': optimize.state_dict()
}, os.path.join(model_checkpoint, 'best_micro_' + model_name))
if performance['macro_f1'] > best_performance[1]:
wait = 0
logger.info('Improve Macro-F1 {}% --> {}%'.format(best_performance[1], performance['macro_f1']))
best_performance[1] = performance['macro_f1']
best_epoch[1] = epoch
save_checkpoint({
'epoch': epoch,
'model_type': config.model.type,
'state_dict': hiagm.state_dict(),
'best_performance': best_performance,
'optimizer': optimize.state_dict()
}, os.path.join(model_checkpoint, 'best_macro_' + model_name))
if epoch % 10 == 1:
save_checkpoint({
'epoch': epoch,
'model_type': config.model.type,
'state_dict': hiagm.state_dict(),
'best_performance': best_performance,
'optimizer': optimize.state_dict()
}, os.path.join(model_checkpoint, model_name + '_epoch_' + str(epoch)))
logger.info('Epoch {} Time Cost {} secs.'.format(epoch, time.time() - start_time))
best_epoch_model_file = os.path.join(model_checkpoint, 'best_micro_' + model_name)
if os.path.isfile(best_epoch_model_file):
load_checkpoint(best_epoch_model_file, model=hiagm,
config=config,
optimizer=optimize)
trainer.eval(test_loader, best_epoch[0], 'TEST')
best_epoch_model_file = os.path.join(model_checkpoint, 'best_macro_' + model_name)
if os.path.isfile(best_epoch_model_file):
load_checkpoint(best_epoch_model_file, model=hiagm,
config=config,
optimizer=optimize)
trainer.eval(test_loader, best_epoch[1], 'TEST')
return
if __name__ == "__main__":
configs = Configure(config_json_file=sys.argv[1])
if configs.train.device_setting.device == 'cuda':
os.system('CUDA_VISIBLE_DEVICES=' + str(configs.train.device_setting.visible_device_list))
else:
os.system("CUDA_VISIBLE_DEVICES=''")
torch.manual_seed(2019)
torch.cuda.manual_seed(2019)
logger.Logger(configs)
if not os.path.isdir(configs.train.checkpoint.dir):
os.mkdir(configs.train.checkpoint.dir)
train(configs)