-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
136 lines (117 loc) · 5.88 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
import random
import torch
import numpy as np
import argparse
import os
from utils import WordVocabulary, LabelVocabulary, Alphabet, build_pretrain_embedding, my_collate_fn, lr_decay
import time
from dataset import MyDataset
from torch.utils.data import DataLoader
from model import NamedEntityRecog
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from train import train_model, evaluate
seed_num = 42
random.seed(seed_num)
torch.manual_seed(seed_num)
np.random.seed(seed_num)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Named Entity Recognition Model')
parser.add_argument('--word_embed_dim', type=int, default=100)
parser.add_argument('--word_hidden_dim', type=int, default=100)
parser.add_argument('--char_embedding_dim', type=int, default=30)
parser.add_argument('--char_hidden_dim', type=int, default=50)
parser.add_argument('--dropout', type=float, default=0.5)
parser.add_argument('--pretrain_embed_path', default='data/glove.6B.100d.txt')
parser.add_argument('--savedir', default='data/model/')
parser.add_argument('--batch_size', type=int, default=10)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--optimizer', default='sgd')
parser.add_argument('--lr', type=float, default=0.015)
parser.add_argument('--feature_extractor', choices=['lstm', 'cnn'], default='cnn')
parser.add_argument('--use_char', type=bool, default=True)
parser.add_argument('--train_path', default='data/eng.train')
parser.add_argument('--dev_path', default='data/eng.testa')
parser.add_argument('--test_path', default='data/eng.testb')
parser.add_argument('--patience', type=int, default=10)
parser.add_argument('--number_normalized', type=bool, default=True)
parser.add_argument('--use_crf', type=bool, default=False)
args = parser.parse_args()
use_gpu = torch.cuda.is_available()
print('use_crf:', args.use_crf)
if not os.path.exists(args.savedir):
os.makedirs(args.savedir)
eval_path = "evaluation"
eval_temp = os.path.join(eval_path, "temp")
eval_script = os.path.join(eval_path, "conlleval")
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)
pred_file = eval_temp + '/pred.txt'
score_file = eval_temp + '/score.txt'
model_name = args.savedir + '/' + args.feature_extractor + str(args.use_char) + str(args.use_crf)
word_vocab = WordVocabulary(args.train_path, args.dev_path, args.test_path, args.number_normalized)
label_vocab = LabelVocabulary(args.train_path)
alphabet = Alphabet(args.train_path, args.dev_path, args.test_path)
emb_begin = time.time()
pretrain_word_embedding = build_pretrain_embedding(args.pretrain_embed_path, word_vocab, args.word_embed_dim)
emb_end = time.time()
emb_min = (emb_end - emb_begin) % 3600 // 60
print('build pretrain embed cost {}m'.format(emb_min))
train_dataset = MyDataset(args.train_path, word_vocab, label_vocab, alphabet, args.number_normalized)
dev_dataset = MyDataset(args.dev_path, word_vocab, label_vocab, alphabet, args.number_normalized)
test_dataset = MyDataset(args.test_path, word_vocab, label_vocab, alphabet, args.number_normalized)
train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, collate_fn=my_collate_fn)
dev_dataloader = DataLoader(dev_dataset, batch_size=args.batch_size, shuffle=False, collate_fn=my_collate_fn)
test_dataloader = DataLoader(test_dataset, batch_size=args.batch_size, shuffle=False, collate_fn=my_collate_fn)
model = NamedEntityRecog(word_vocab.size(), args.word_embed_dim, args.word_hidden_dim, alphabet.size(),
args.char_embedding_dim, args.char_hidden_dim,
args.feature_extractor, label_vocab.size(), args.dropout,
pretrain_embed=pretrain_word_embedding, use_char=args.use_char, use_crf=args.use_crf,
use_gpu=use_gpu)
if use_gpu:
model = model.cuda()
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9)
train_begin = time.time()
print('train begin', '-' * 50)
print()
print()
writer = SummaryWriter('log')
batch_num = -1
best_f1 = -1
early_stop = 0
for epoch in range(args.epochs):
epoch_begin = time.time()
print('train {}/{} epoch'.format(epoch + 1, args.epochs))
optimizer = lr_decay(optimizer, epoch, 0.05, args.lr)
batch_num = train_model(train_dataloader, model, optimizer, batch_num, writer, use_gpu)
new_f1 = evaluate(dev_dataloader, model, word_vocab, label_vocab, pred_file, score_file, eval_script, use_gpu)
print('f1 is {} at {}th epoch on dev set'.format(new_f1, epoch + 1))
if new_f1 > best_f1:
best_f1 = new_f1
print('new best f1 on dev set:', best_f1)
early_stop = 0
torch.save(model.state_dict(), model_name)
else:
early_stop += 1
epoch_end = time.time()
cost_time = epoch_end - epoch_begin
print('train {}th epoch cost {}m {}s'.format(epoch + 1, int(cost_time / 60), int(cost_time % 60)))
print()
if early_stop > args.patience:
print('early stop')
break
train_end = time.time()
train_cost = train_end - train_begin
hour = int(train_cost / 3600)
min = int((train_cost % 3600) / 60)
second = int(train_cost % 3600 % 60)
print()
print()
print('train end', '-' * 50)
print('train total cost {}h {}m {}s'.format(hour, min, second))
print('-' * 50)
model.load_state_dict(torch.load(model_name))
test_acc = evaluate(test_dataloader, model, word_vocab, label_vocab, pred_file, score_file, eval_script, use_gpu)
print('test acc on test set:', test_acc)