-
Notifications
You must be signed in to change notification settings - Fork 9
/
train.py
96 lines (78 loc) · 3.71 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
import os
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils import data
import consts
from model import Net
from data_load import ACE2005Dataset, pad, all_triggers, all_entities, all_postags, word2id, wordemb
from eval import eval
def train(model, iterator, optimizer, criterion):
model.train()
for i, batch in enumerate(iterator):
tokens_2d, triggers_2d, entities_3d, postags_2d, adj, seqlen_1d, words, triggers = batch
optimizer.zero_grad()
trigger_logits, trigger_hat_2d = model.predict_triggers(tokens_2d=tokens_2d, entities_3d=entities_3d,
postags_2d=postags_2d, seqlen_1d=seqlen_1d, adjm=adj)
triggers_y_2d = torch.LongTensor(triggers_2d).to(model.device)
trigger_logits = trigger_logits.view(-1, trigger_logits.shape[-1])
trigger_loss = criterion(trigger_logits, triggers_y_2d.view(-1))
loss = trigger_loss
loss.backward()
optimizer.step()
if i % 40 == 0: # monitoring
print("step: {}, loss: {}".format(i, loss.item()))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--batch_size", type=int, default=consts.batch_size)
parser.add_argument("--lr", type=float, default=consts.lr)
parser.add_argument("--n_epochs", type=int, default=consts.n_epochs)
parser.add_argument("--logdir", type=str, default="output/logdir2")
parser.add_argument("--trainset", type=str, default=consts.train_data)
parser.add_argument("--devset", type=str, default=consts.dev_data)
parser.add_argument("--testset", type=str, default=consts.test_data)
hp = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = Net(
device=device,
trigger_size=len(all_triggers),
word_size=[len(word2id), consts.WORD_DIM],
word_emb=wordemb,
entity_size=[len(all_entities), consts.ENTITY_DIM],
postags_size=[len(all_postags), consts.POSTAG_DIM],
position_size=[2*consts.MAXLEN, consts.POSITION_DIM]
)
if device == 'cuda':
model = model.cuda()
train_dataset = ACE2005Dataset(hp.trainset)
dev_dataset = ACE2005Dataset(hp.devset)
test_dataset = ACE2005Dataset(hp.testset)
train_iter = data.DataLoader(dataset=train_dataset,
batch_size=hp.batch_size,
shuffle=True,
num_workers=4,
collate_fn=pad)
dev_iter = data.DataLoader(dataset=dev_dataset,
batch_size=hp.batch_size,
shuffle=False,
num_workers=4,
collate_fn=pad)
test_iter = data.DataLoader(dataset=test_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()
if not os.path.exists(hp.logdir):
os.makedirs(hp.logdir)
for epoch in range(1, hp.n_epochs + 1):
print("=========train at epoch={}=========".format(epoch))
train(model, train_iter, optimizer, criterion)
fname = os.path.join(hp.logdir, str(epoch))
print("=========eval dev at epoch={}=========".format(epoch))
metric_dev = eval(model, dev_iter, fname + '_dev', write=False)
print("=========eval test at epoch={}=========".format(epoch))
metric_test = eval(model, test_iter, fname + '_test',write=False)
torch.save(model, "best_model.pt")