-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
268 lines (230 loc) · 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
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
from __future__ import print_function
import argparse
import codecs
import functools
import itertools
import os
import sys
import time
from torch import optim
from tqdm import tqdm
from model.crf import *
from model.evaluator import eval_w
from model.lstm_crf import *
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Learning with BLSTM-CRF')
parser.add_argument('--rand_embedding', action='store_true', help='random initialize word embedding')
parser.add_argument('--emb_file', default='./embedding/glove.6B.100d.txt', help='path to pre-trained embedding')
parser.add_argument('--train_file', default='xxx/xx/daguan/datagrand/train.txt',
help='path to training file')
parser.add_argument('--dev_file', default='xxx/xx/daguan/datagrand/dev.txt',
help='path to development file')
parser.add_argument('--gpu', type=int, default=0, help='gpu id, set to -1 if use cpu mode')
parser.add_argument('--batch_size', type=int, default=10, help='batch size (10)')
parser.add_argument('--unk', default='unk', help='unknow-token in pre-trained embedding')
parser.add_argument('--checkpoint', default='./checkpoint/', help='path to checkpoint prefix')
parser.add_argument('--hidden', type=int, default=100, help='hidden dimension')
parser.add_argument('--drop_out', type=float, default=0.55, help='dropout ratio')
parser.add_argument('--epoch', type=int, default=200, help='maximum epoch number')
parser.add_argument('--start_epoch', type=int, default=0, help='start epoch idx')
parser.add_argument('--embedding_dim', type=int, default=100, help='dimension for word embedding')
parser.add_argument('--layers', type=int, default=1, help='number of lstm layers')
parser.add_argument('--lr', type=float, default=0.015, help='initial learning rate')
parser.add_argument('--lr_decay', type=float, default=0.05, help='decay ratio of learning rate')
parser.add_argument('--fine_tune', action='store_false', help='fine tune pre-trained embedding dictionary')
parser.add_argument('--load_check_point', default='', help='path of checkpoint')
parser.add_argument('--load_opt', action='store_true', help='load optimizer from ')
parser.add_argument('--update', choices=['sgd', 'adam'], default='sgd', help='optimizer method')
parser.add_argument('--momentum', type=float, default=0.9, help='momentum for sgd')
parser.add_argument('--clip_grad', type=float, default=5.0, help='grad clip at')
parser.add_argument('--small_crf', action='store_false',
help='use small crf instead of large crf, refer model.crf module for more details')
parser.add_argument('--mini_count', type=float, default=5, help='thresholds to replace rare words with <unk>')
parser.add_argument('--eva_matrix', choices=['a', 'fa'], default='fa', help='use f1 and accuracy or accuracy alone')
parser.add_argument('--patience', type=int, default=15, help='patience for early stop')
parser.add_argument('--least_iters', type=int, default=50, help='at least train how many epochs before stop')
parser.add_argument('--shrink_embedding', action='store_true',
help='shrink the embedding dictionary to corpus (open this if pre-trained embedding dictionary is too large, but disable this may yield better results on external corpus)')
args = parser.parse_args()
os.makedirs(args.checkpoint, exist_ok=True)
if args.gpu >= 0:
torch.cuda.set_device(args.gpu)
print('setting:')
print(args)
# load corpus
print('loading corpus')
with codecs.open(args.train_file, 'r', 'utf-8') as f:
lines = f.readlines()
with codecs.open(args.dev_file, 'r', 'utf-8') as f:
dev_lines = f.readlines()
# converting format
dev_features, dev_labels = utils.read_corpus(dev_lines)
if args.load_check_point:
if os.path.isfile(args.load_check_point):
print("loading checkpoint: '{}'".format(args.load_check_point))
checkpoint_file = torch.load(args.load_check_point)
args.start_epoch = checkpoint_file['epoch']
f_map = checkpoint_file['f_map']
l_map = checkpoint_file['l_map']
train_features, train_labels = utils.read_corpus(lines)
else:
print("no checkpoint found at: '{}'".format(args.load_check_point))
else:
print('constructing coding table')
# converting format
train_features, train_labels, f_map, l_map = utils.generate_corpus(lines, if_shrink_feature=True, thresholds=0)
f_set = {v for v in f_map}
f_map = utils.shrink_features(f_map, train_features, args.mini_count)
dt_f_set = functools.reduce(lambda x, y: x | y, map(lambda t: set(t), dev_features), f_set)
if not args.rand_embedding:
print("feature size: '{}'".format(len(f_map)))
print('loading embedding')
if args.fine_tune: # which means does not do fine-tune
f_map = {'<eof>': 0}
f_map, embedding_tensor, in_doc_words = utils.load_embedding_wlm(args.emb_file, ' ', f_map, dt_f_set,
args.caseless, args.unk,
args.embedding_dim,
shrink_to_corpus=args.shrink_embedding)
print("embedding size: '{}'".format(len(f_map)))
l_set = functools.reduce(lambda x, y: x | y, map(lambda t: set(t), dev_labels))
for label in l_set:
if label not in l_map:
l_map[label] = len(l_map)
# construct dataset
dataset = utils.construct_bucket_mean_vb(train_features, train_labels, f_map, l_map)
dev_dataset = utils.construct_bucket_mean_vb(dev_features, dev_labels, f_map, l_map)
dataset_loader = [torch.utils.data.DataLoader(tup, args.batch_size, shuffle=True, drop_last=False) for tup in
dataset]
dev_dataset_loader = [torch.utils.data.DataLoader(tup, 50, shuffle=False, drop_last=False) for tup in dev_dataset]
# build model
print('building model')
ner_model = LSTM_CRF(len(f_map), len(l_map), args.embedding_dim, args.hidden, args.layers, args.drop_out,
large_CRF=args.small_crf)
if args.load_check_point:
ner_model.load_state_dict(checkpoint_file['state_dict'])
else:
if not args.rand_embedding:
ner_model.load_pretrained_embedding(embedding_tensor)
print('random initialization')
ner_model.rand_init(init_embedding=args.rand_embedding)
if args.update == 'sgd':
optimizer = optim.SGD(ner_model.parameters(), lr=args.lr, momentum=args.momentum)
elif args.update == 'adam':
optimizer = optim.Adam(ner_model.parameters(), lr=args.lr)
if args.load_check_point and args.load_opt:
optimizer.load_state_dict(checkpoint_file['optimizer'])
crit = CRFLoss_vb(len(l_map), l_map['<start>'], l_map['<pad>'])
if args.gpu >= 0:
if_cuda = True
print('device: ' + str(args.gpu))
torch.cuda.set_device(args.gpu)
crit.cuda()
ner_model.cuda()
packer = CRFRepack(len(l_map), True)
else:
if_cuda = False
packer = CRFRepack(len(l_map), False)
tot_length = sum(map(lambda t: len(t), dataset_loader))
best_f1 = float('-inf')
best_acc = float('-inf')
track_list = list()
start_time = time.time()
epoch_list = range(args.start_epoch, args.start_epoch + args.epoch)
patience_count = 0
evaluator = eval_w(packer, l_map, args.eva_matrix)
for epoch_idx, args.start_epoch in enumerate(epoch_list):
epoch_loss = 0
ner_model.train()
for feature, tg, mask in tqdm(
itertools.chain.from_iterable(dataset_loader), mininterval=2,
desc=' - Tot it %d (epoch %d)' % (tot_length, args.start_epoch), leave=False, file=sys.stdout):
fea_v, tg_v, mask_v = packer.repack_vb(feature, tg, mask)
ner_model.zero_grad()
scores, hidden = ner_model.forward(fea_v)
loss = crit.forward(scores, tg_v, mask_v)
loss.backward()
nn.utils.clip_grad_norm_(ner_model.parameters(), args.clip_grad)
optimizer.step()
epoch_loss += utils.to_scalar(loss)
# update lr
utils.adjust_learning_rate(optimizer, args.lr / (1 + (args.start_epoch + 1) * args.lr_decay))
# average
epoch_loss /= tot_length
# eval & save check_point
if 'f' in args.eva_matrix:
dev_result = evaluator.calc_score(ner_model, dev_dataset_loader)
for label, (dev_f1, dev_pre, dev_rec, dev_acc, msg) in dev_result.items():
print('DEV : %s : dev_f1: %.4f dev_rec: %.4f dev_pre: %.4f dev_acc: %.4f | %s\n' % (
label, dev_f1, dev_pre, dev_rec, dev_acc, msg))
(dev_f1, dev_pre, dev_rec, dev_acc, msg) = dev_result['total']
if dev_f1 > best_f1:
patience_count = 0
best_f1 = dev_f1
track_list.append(
{'loss': epoch_loss, 'dev_f1': dev_f1, 'dev_acc': dev_acc})
print(
'(loss: %.4f, epoch: %d, dev F1 = %.4f, dev acc = %.4f,), saving...' %
(epoch_loss,
args.start_epoch,
dev_f1,
dev_acc,))
try:
utils.save_checkpoint({
'epoch': args.start_epoch,
'state_dict': ner_model.state_dict(),
'optimizer': optimizer.state_dict(),
'f_map': f_map,
'l_map': l_map,
}, {'track_list': track_list,
'args': vars(args)
}, args.checkpoint + 'lstm_crf')
except Exception as inst:
print(inst)
else:
patience_count += 1
print('(loss: %.4f, epoch: %d, dev F1 = %.4f, dev acc = %.4f)' %
(epoch_loss,
args.start_epoch,
dev_f1,
dev_acc))
track_list.append({'loss': epoch_loss, 'dev_f1': dev_f1, 'dev_acc': dev_acc})
else:
dev_acc = evaluator.calc_score(ner_model, dev_dataset_loader)
if dev_acc > best_acc:
patience_count = 0
best_acc = dev_acc
track_list.append(
{'loss': epoch_loss, 'dev_acc': dev_acc})
print(
'(loss: %.4f, epoch: %d, dev acc = %.4f), saving...' %
(epoch_loss,
args.start_epoch,
dev_acc))
try:
utils.save_checkpoint({
'epoch': args.start_epoch,
'state_dict': ner_model.state_dict(),
'optimizer': optimizer.state_dict(),
'f_map': f_map,
'l_map': l_map,
}, {'track_list': track_list,
'args': vars(args)
}, args.checkpoint + 'lstm_crf')
except Exception as inst:
print(inst)
else:
patience_count += 1
print('(loss: %.4f, epoch: %d, dev acc = %.4f)' %
(epoch_loss,
args.start_epoch,
dev_acc))
track_list.append({'loss': epoch_loss, 'dev_acc': dev_acc})
print('epoch: ' + str(args.start_epoch) + '\t in ' + str(args.epoch) + ' take: ' + str(
time.time() - start_time) + ' s')
if patience_count >= args.patience and args.start_epoch >= args.least_iters:
break
# printing summary
print('setting:')
print(args)