-
Notifications
You must be signed in to change notification settings - Fork 56
/
train.py
157 lines (126 loc) · 5.5 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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils import data
from model import Net
from data_load import NerDataset, pad, VOCAB, tokenizer, tag2idx, idx2tag
import os
import numpy as np
import argparse
def train(model, iterator, optimizer, criterion):
model.train()
for i, batch in enumerate(iterator):
words, x, is_heads, tags, y, seqlens = batch
_y = y # for monitoring
optimizer.zero_grad()
logits, y, _ = model(x, y) # logits: (N, T, VOCAB), y: (N, T)
logits = logits.view(-1, logits.shape[-1]) # (N*T, VOCAB)
y = y.view(-1) # (N*T,)
loss = criterion(logits, y)
loss.backward()
optimizer.step()
if i==0:
print("=====sanity check======")
print("words:", words[0])
print("x:", x.cpu().numpy()[0][:seqlens[0]])
print("tokens:", tokenizer.convert_ids_to_tokens(x.cpu().numpy()[0])[:seqlens[0]])
print("is_heads:", is_heads[0])
print("y:", _y.cpu().numpy()[0][:seqlens[0]])
print("tags:", tags[0])
print("seqlen:", seqlens[0])
print("=======================")
if i%10==0: # monitoring
print(f"step: {i}, loss: {loss.item()}")
def eval(model, iterator, f):
model.eval()
Words, Is_heads, Tags, Y, Y_hat = [], [], [], [], []
with torch.no_grad():
for i, batch in enumerate(iterator):
words, x, is_heads, tags, y, seqlens = batch
_, _, y_hat = model(x, y) # y_hat: (N, T)
Words.extend(words)
Is_heads.extend(is_heads)
Tags.extend(tags)
Y.extend(y.numpy().tolist())
Y_hat.extend(y_hat.cpu().numpy().tolist())
## gets results and save
with open("temp", 'w') as fout:
for words, is_heads, tags, y_hat in zip(Words, Is_heads, Tags, Y_hat):
y_hat = [hat for head, hat in zip(is_heads, y_hat) if head == 1]
preds = [idx2tag[hat] for hat in y_hat]
assert len(preds)==len(words.split())==len(tags.split())
for w, t, p in zip(words.split()[1:-1], tags.split()[1:-1], preds[1:-1]):
fout.write(f"{w} {t} {p}\n")
fout.write("\n")
## calc metric
y_true = np.array([tag2idx[line.split()[1]] for line in open("temp", 'r').read().splitlines() if len(line) > 0])
y_pred = np.array([tag2idx[line.split()[2]] for line in open("temp", 'r').read().splitlines() if len(line) > 0])
num_proposed = len(y_pred[y_pred>1])
num_correct = (np.logical_and(y_true==y_pred, y_true>1)).astype(np.int).sum()
num_gold = len(y_true[y_true>1])
print(f"num_proposed:{num_proposed}")
print(f"num_correct:{num_correct}")
print(f"num_gold:{num_gold}")
try:
precision = num_correct / num_proposed
except ZeroDivisionError:
precision = 1.0
try:
recall = num_correct / num_gold
except ZeroDivisionError:
recall = 1.0
try:
f1 = 2*precision*recall / (precision + recall)
except ZeroDivisionError:
if precision*recall==0:
f1=1.0
else:
f1=0
final = f + ".P%.2f_R%.2f_F%.2f" %(precision, recall, f1)
with open(final, 'w') as fout:
result = open("temp", "r").read()
fout.write(f"{result}\n")
fout.write(f"precision={precision}\n")
fout.write(f"recall={recall}\n")
fout.write(f"f1={f1}\n")
os.remove("temp")
print("precision=%.2f"%precision)
print("recall=%.2f"%recall)
print("f1=%.2f"%f1)
return precision, recall, f1
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--batch_size", type=int, default=128)
parser.add_argument("--lr", type=float, default=0.0001)
parser.add_argument("--n_epochs", type=int, default=30)
parser.add_argument("--finetuning", dest="finetuning", action="store_true")
parser.add_argument("--top_rnns", dest="top_rnns", action="store_true")
parser.add_argument("--logdir", type=str, default="checkpoints/01")
parser.add_argument("--trainset", type=str, default="conll2003/train.txt")
parser.add_argument("--validset", type=str, default="conll2003/valid.txt")
hp = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = Net(hp.top_rnns, len(VOCAB), device, hp.finetuning).cuda()
model = nn.DataParallel(model)
train_dataset = NerDataset(hp.trainset)
eval_dataset = NerDataset(hp.validset)
train_iter = data.DataLoader(dataset=train_dataset,
batch_size=hp.batch_size,
shuffle=True,
num_workers=4,
collate_fn=pad)
eval_iter = data.DataLoader(dataset=eval_dataset,
batch_size=hp.batch_size,
shuffle=False,
num_workers=4,
collate_fn=pad)
optimizer = optim.Adam(model.parameters(), lr = hp.lr)
criterion = nn.CrossEntropyLoss(ignore_index=0)
for epoch in range(1, hp.n_epochs+1):
train(model, train_iter, optimizer, criterion)
print(f"=========eval at epoch={epoch}=========")
if not os.path.exists(hp.logdir): os.makedirs(hp.logdir)
fname = os.path.join(hp.logdir, str(epoch))
precision, recall, f1 = eval(model, eval_iter, fname)
torch.save(model.state_dict(), f"{fname}.pt")
print(f"weights were saved to {fname}.pt")