forked from AngusMonroe/BLSTM-CRF-NER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·437 lines (391 loc) · 15 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
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
# coding=utf-8
import optparse
import itertools
from collections import OrderedDict
import loader
import torch
import time
import pickle
from torch.autograd import Variable
# import matplotlib.pyplot as plt
import sys
# import visdom
from utils import *
from loader import *
from model import BiLSTM_CRF
t = time.time()
models_path = "models/"
optparser = optparse.OptionParser()
optparser.add_option(
"-T", "--train", default="dataset/eng.train",
help="Train set location"
)
optparser.add_option(
"-d", "--dev", default="dataset/eng.testa",
help="Dev set location"
)
optparser.add_option(
"-t", "--test", default="dataset/eng.testb",
help="Test set location"
)
optparser.add_option(
'--test_train', default='dataset/eng.train54019',
help='test train'
)
optparser.add_option(
'--score', default='evaluation/temp/score.txt',
help='score file location'
)
optparser.add_option(
"-s", "--tag_scheme", default="iob",
help="Tagging scheme (IOB or IOBES)"
)
optparser.add_option(
"-l", "--lower", default="1",
type='int', help="Lowercase words (this will not affect character inputs)"
)
optparser.add_option(
"-z", "--zeros", default="0",
type='int', help="Replace digits with 0"
)
optparser.add_option(
"-c", "--char_dim", default="25",
type='int', help="Char embedding dimension"
)
optparser.add_option(
"-C", "--char_lstm_dim", default="25",
type='int', help="Char LSTM hidden layer size"
)
optparser.add_option(
"-b", "--char_bidirect", default="1",
type='int', help="Use a bidirectional LSTM for chars"
)
optparser.add_option(
"-w", "--word_dim", default="100",
type='int', help="Token embedding dimension"
)
optparser.add_option(
"-W", "--word_lstm_dim", default="200",
type='int', help="Token LSTM hidden layer size"
)
optparser.add_option(
"-B", "--word_bidirect", default="1",
type='int', help="Use a bidirectional LSTM for words"
)
optparser.add_option(
"-p", "--pre_emb", default="models/glove.6B.100d.txt",
help="Location of pretrained embeddings"
)
optparser.add_option(
"-A", "--all_emb", default="1",
type='int', help="Load all embeddings"
)
optparser.add_option(
"-a", "--cap_dim", default="0",
type='int', help="Capitalization feature dimension (0 to disable)"
)
optparser.add_option(
"-f", "--crf", default="1",
type='int', help="Use CRF (0 to disable)"
)
optparser.add_option(
"-D", "--dropout", default="0.5",
type='float', help="Droupout on the input (0 = no dropout)"
)
optparser.add_option(
"-r", "--reload", default="0",
type='int', help="Reload the last saved model"
)
optparser.add_option(
"-g", '--use_gpu', default='1',
type='int', help='whether or not to ues gpu'
)
optparser.add_option(
'--loss', default='loss.txt',
help='loss file location'
)
optparser.add_option(
'--name', default='test',
help='model name'
)
optparser.add_option(
'--char_mode', choices=['CNN', 'LSTM'], default='CNN',
help='char_CNN or char_LSTM'
)
opts = optparser.parse_args()[0]
parameters = OrderedDict()
parameters['tag_scheme'] = opts.tag_scheme
parameters['lower'] = opts.lower == 1
parameters['zeros'] = opts.zeros == 1
parameters['char_dim'] = opts.char_dim
parameters['char_lstm_dim'] = opts.char_lstm_dim
parameters['char_bidirect'] = opts.char_bidirect == 1
parameters['word_dim'] = opts.word_dim
parameters['word_lstm_dim'] = opts.word_lstm_dim
parameters['word_bidirect'] = opts.word_bidirect == 1
parameters['pre_emb'] = opts.pre_emb
parameters['all_emb'] = opts.all_emb == 1
parameters['cap_dim'] = opts.cap_dim
parameters['crf'] = opts.crf == 1
parameters['dropout'] = opts.dropout
parameters['reload'] = opts.reload == 1
parameters['name'] = opts.name
parameters['char_mode'] = opts.char_mode
parameters['use_gpu'] = opts.use_gpu == 1 and torch.cuda.is_available()
use_gpu = parameters['use_gpu']
mapping_file = 'models/mapping.pkl'
name = parameters['name']
model_name = models_path + name #get_name(parameters)
tmp_model = model_name + '.tmp'
assert os.path.isfile(opts.train)
print(opts.train)
assert os.path.isfile(opts.dev)
print(opts.dev)
assert os.path.isfile(opts.test)
print(opts.test)
assert parameters['char_dim'] > 0 or parameters['word_dim'] > 0
assert 0. <= parameters['dropout'] < 1.0
assert parameters['tag_scheme'] in ['iob', 'iobes']
assert not parameters['all_emb'] or parameters['pre_emb']
assert not parameters['pre_emb'] or parameters['word_dim'] > 0
assert not parameters['pre_emb'] or os.path.isfile(parameters['pre_emb'])
if not os.path.isfile(eval_script):
raise Exception('CoNLL evaluation script not found at "%s"' % eval_script)
if not os.path.exists(eval_temp):
os.makedirs(eval_temp)
if not os.path.exists(models_path):
os.makedirs(models_path)
lower = parameters['lower']
zeros = parameters['zeros']
tag_scheme = parameters['tag_scheme']
train_sentences = loader.load_sentences(opts.train, lower, zeros)
dev_sentences = loader.load_sentences(opts.dev, lower, zeros)
test_sentences = loader.load_sentences(opts.test, lower, zeros)
test_train_sentences = loader.load_sentences(opts.test_train, lower, zeros)
update_tag_scheme(train_sentences, tag_scheme)
update_tag_scheme(dev_sentences, tag_scheme)
update_tag_scheme(test_sentences, tag_scheme)
update_tag_scheme(test_train_sentences, tag_scheme)
dico_words_train = word_mapping(train_sentences, lower)[0]
dico_words, word_to_id, id_to_word = augment_with_pretrained(
dico_words_train.copy(),
parameters['pre_emb'],
list(itertools.chain.from_iterable(
[[w[0] for w in s] for s in dev_sentences + test_sentences])
) if not parameters['all_emb'] else None
)
dico_chars, char_to_id, id_to_char = char_mapping(train_sentences)
dico_tags, tag_to_id, id_to_tag = tag_mapping(train_sentences)
train_data = prepare_dataset(
train_sentences, word_to_id, char_to_id, tag_to_id, lower
)
dev_data = prepare_dataset(
dev_sentences, word_to_id, char_to_id, tag_to_id, lower
)
test_data = prepare_dataset(
test_sentences, word_to_id, char_to_id, tag_to_id, lower
)
test_train_data = prepare_dataset(
test_train_sentences, word_to_id, char_to_id, tag_to_id, lower
)
print("%i / %i / %i sentences in train / dev / test." % (
len(train_data), len(dev_data), len(test_data)))
all_word_embeds = {}
for i, line in enumerate(codecs.open(opts.pre_emb, 'r', 'utf-8')):
s = line.strip().split()
if len(s) == parameters['word_dim'] + 1:
all_word_embeds[s[0]] = np.array([float(i) for i in s[1:]])
word_embeds = np.random.uniform(-np.sqrt(0.06), np.sqrt(0.06), (len(word_to_id), opts.word_dim))
for w in word_to_id:
if w in all_word_embeds:
word_embeds[word_to_id[w]] = all_word_embeds[w]
elif w.lower() in all_word_embeds:
word_embeds[word_to_id[w]] = all_word_embeds[w.lower()]
print('Loaded %i pretrained embeddings.' % len(all_word_embeds))
with open(mapping_file, 'wb') as f:
mappings = {
'word_to_id': word_to_id,
'tag_to_id': tag_to_id,
'char_to_id': char_to_id,
'parameters': parameters,
'word_embeds': word_embeds
}
pickle.dump(mappings, f)
print('word_to_id: ', len(word_to_id))
model = BiLSTM_CRF(vocab_size=len(word_to_id),
tag_to_ix=tag_to_id,
embedding_dim=parameters['word_dim'],
hidden_dim=parameters['word_lstm_dim'],
use_gpu=use_gpu,
char_to_ix=char_to_id,
pre_word_embeds=word_embeds,
use_crf=parameters['crf'],
char_mode=parameters['char_mode'])
# n_cap=4,
# cap_embedding_dim=10)
if parameters['reload']:
model.load_state_dict(torch.load(model_name))
if use_gpu:
model.cuda()
learning_rate = 0.015
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)
losses = []
loss = 0.0
best_dev_F = -1.0
best_test_F = -1.0
best_train_F = -1.0
all_F = [[0, 0, 0]]
plot_every = 1000
eval_every = 20000
count = 0
# vis = visdom.Visdom()
sys.stdout.flush()
def evaluating(model, datas, best_F):
prediction = []
save = False
new_F = 0.0
confusion_matrix = torch.zeros((len(tag_to_id) - 2, len(tag_to_id) - 2))
for data in datas:
ground_truth_id = data['tags']
words = data['str_words']
chars2 = data['chars']
caps = data['caps']
if parameters['char_mode'] == 'LSTM':
chars2_sorted = sorted(chars2, key=lambda p: len(p), reverse=True)
d = {}
for i, ci in enumerate(chars2):
for j, cj in enumerate(chars2_sorted):
if ci == cj and not j in d and not i in list(d.values()):
d[j] = i
continue
chars2_length = [len(c) for c in chars2_sorted]
char_maxl = max(chars2_length)
chars2_mask = np.zeros((len(chars2_sorted), char_maxl), dtype='int')
for i, c in enumerate(chars2_sorted):
chars2_mask[i, :chars2_length[i]] = c
chars2_mask = Variable(torch.LongTensor(chars2_mask))
if parameters['char_mode'] == 'CNN':
d = {}
chars2_length = [len(c) for c in chars2]
char_maxl = max(chars2_length)
chars2_mask = np.zeros((len(chars2_length), char_maxl), dtype='int')
for i, c in enumerate(chars2):
chars2_mask[i, :chars2_length[i]] = c
chars2_mask = Variable(torch.LongTensor(chars2_mask))
dwords = Variable(torch.LongTensor(data['words']))
dcaps = Variable(torch.LongTensor(caps))
if use_gpu:
val, out = model(dwords.cuda(), chars2_mask.cuda(), dcaps.cuda(), chars2_length, d)
else:
val, out = model(dwords, chars2_mask, dcaps, chars2_length, d)
predicted_id = out
for (word, true_id, pred_id) in zip(words, ground_truth_id, predicted_id):
line = ' '.join([word, id_to_tag[true_id], id_to_tag[pred_id]])
prediction.append(line)
confusion_matrix[true_id, pred_id] += 1
prediction.append('')
predf = eval_temp + '/pred.' + name
scoref = eval_temp + '/score.' + name
with open(predf, 'wb') as f:
f.write(bytes('\n'.join(prediction), encoding="utf8"))
os.system('%s < %s > %s' % (eval_script, predf, scoref))
eval_lines = [l.rstrip() for l in codecs.open(scoref, 'r', 'utf8')]
for i, line in enumerate(eval_lines):
print(line)
if i == 1:
new_F = float(line.strip().split()[-1])
if new_F > best_F:
best_F = new_F
save = True
print('the best F is ', new_F)
print(("{: >2}{: >7}{: >7}%s{: >9}" % ("{: >7}" * confusion_matrix.size(0))).format(
"ID", "NE", "Total",
*([id_to_tag[i] for i in range(confusion_matrix.size(0))] + ["Percent"])
))
for i in range(confusion_matrix.size(0)):
print(("{: >2}{: >7}{: >7}%s{: >9}" % ("{: >7}" * confusion_matrix.size(0))).format(
str(i), id_to_tag[i], str(confusion_matrix[i].sum()),
*([confusion_matrix[i][j] for j in range(confusion_matrix.size(0))] +
["%.3f" % (confusion_matrix[i][i] * 100. / max(1, confusion_matrix[i].sum()))])
))
return best_F, new_F, save
model.train(True)
for epoch in range(1, 21):
print("epoch = %i." % epoch)
for i, index in enumerate(np.random.permutation(len(train_data))):
tr = time.time()
count += 1
data = train_data[index]
model.zero_grad()
sentence_in = data['words']
sentence_in = Variable(torch.LongTensor(sentence_in))
tags = data['tags']
chars2 = data['chars']
######### char lstm
if parameters['char_mode'] == 'LSTM':
chars2_sorted = sorted(chars2, key=lambda p: len(p), reverse=True)
d = {}
for i, ci in enumerate(chars2):
for j, cj in enumerate(chars2_sorted):
if ci == cj and not j in d and not i in list(d.values()):
d[j] = i
continue
chars2_length = [len(c) for c in chars2_sorted]
char_maxl = max(chars2_length)
chars2_mask = np.zeros((len(chars2_sorted), char_maxl), dtype='int')
for i, c in enumerate(chars2_sorted):
chars2_mask[i, :chars2_length[i]] = c
chars2_mask = Variable(torch.LongTensor(chars2_mask))
# ######## char cnn
if parameters['char_mode'] == 'CNN':
d = {}
chars2_length = [len(c) for c in chars2]
char_maxl = max(chars2_length)
chars2_mask = np.zeros((len(chars2_length), char_maxl), dtype='int')
for i, c in enumerate(chars2):
chars2_mask[i, :chars2_length[i]] = c
chars2_mask = Variable(torch.LongTensor(chars2_mask))
targets = torch.LongTensor(tags)
caps = Variable(torch.LongTensor(data['caps']))
if use_gpu:
neg_log_likelihood = model.neg_log_likelihood(sentence_in.cuda(), targets.cuda(), chars2_mask.cuda(), caps.cuda(), chars2_length, d)
else:
neg_log_likelihood = model.neg_log_likelihood(sentence_in, targets, chars2_mask, caps, chars2_length, d)
loss += neg_log_likelihood.item() / len(data['words'])
neg_log_likelihood.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 5.0)
optimizer.step()
if count % plot_every == 0:
loss /= plot_every
print(count, ': ', loss)
if losses == []:
losses.append(loss)
losses.append(loss)
# text = '<p>' + '</p><p>'.join([str(l) for l in losses[-9:]]) + '</p>'
# losswin = 'loss_' + name
# textwin = 'loss_text_' + name
# vis.line(np.array(losses), X=np.array([plot_every*i for i in range(len(losses))]),
# win=losswin, opts={'title': losswin, 'legend': ['loss']})
# vis.text(text, win=textwin, opts={'title': textwin})
loss = 0.0
if count % (eval_every) == 0 and count > (eval_every * 20) or \
count % (eval_every*4) == 0 and count < (eval_every * 20):
model.train(False)
# torch.save(model, model_name)
best_train_F, new_train_F, _ = evaluating(model, test_train_data, best_train_F)
best_dev_F, new_dev_F, save = evaluating(model, dev_data, best_dev_F)
if save:
torch.save(model, model_name)
best_test_F, new_test_F, _ = evaluating(model, test_data, best_test_F)
sys.stdout.flush()
# all_F.append([new_train_F, new_dev_F, new_test_F])
# Fwin = 'F-score of {train, dev, test}_' + name
# vis.line(np.array(all_F), win=Fwin,
# X=np.array([eval_every*i for i in range(len(all_F))]),
# opts={'title': Fwin, 'legend': ['train', 'dev', 'test']})
model.train(True)
if count % len(train_data) == 0:
adjust_learning_rate(optimizer, lr=learning_rate/(1+0.05*count/len(train_data)))
print(time.time() - t)
# plt.plot(losses)
# plt.show()