-
Notifications
You must be signed in to change notification settings - Fork 43
/
s2s_train.py
139 lines (104 loc) · 4.23 KB
/
s2s_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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
import random
import math
import time
import glob
from config import Config
from s2s_dataset import gen
from torch.utils.data import DataLoader, Dataset
from models.Seq2Seq import Attention, Encoder, Decoder, Seq2Seq
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def init_weights(m):
for name, param in m.named_parameters():
if 'weight' in name:
nn.init.normal_(param.data, mean=0, std=0.01)
else:
nn.init.constant_(param.data, 0)
def train(model, steps, iterator, optimizer, criterion, clip):
model.train()
epoch_loss = 0
epoch_acc = 0
for i in range(steps):
src, trg = next(iterator)
optimizer.zero_grad()
output = model(src)
output = output.permute(1, 0, 2).contiguous().view(-1, len(config.class_char))
trg = trg.contiguous().long().view(-1)
loss = criterion(output, trg)
loss.backward()
acc = np.mean((torch.argmax(output, 1) == trg).cpu().numpy())
torch.nn.utils.clip_grad_norm_(model.parameters(), clip)
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc
return epoch_loss / steps, epoch_acc / steps
def evaluate(model, steps, iterator, criterion):
model.eval()
epoch_loss = 0
epoch_acc = 0
for i in range(steps):
src, trg = next(iterator)
output = model(src)
output = output.permute(1, 0, 2).contiguous().view(-1, len(config.class_char))
trg = trg.contiguous().long().view(-1)
loss = criterion(output, trg)
acc = np.mean((torch.argmax(output, 1) == trg).cpu().numpy())
# print(torch.argmax(output,1))
epoch_loss += loss.item()
epoch_acc += acc
return epoch_loss / steps, epoch_acc / steps
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
if __name__ == "__main__":
config = Config()
device = config.device
attn = Attention(config.s2s_enc_hid, config.s2s_dec_hid)
enc = Encoder(config.s2s_emb_dim,
config.s2s_enc_hid,
config.s2s_dec_hid,
config.s2s_enc_dropout)
dec = Decoder(len(config.class_char),
config.s2s_emb_dim,
config.s2s_enc_hid,
config.s2s_dec_hid,
config.s2s_enc_dropout,
attn)
model = Seq2Seq(enc, dec, device).to(device)
model.apply(init_weights)
print(f'The model has {count_parameters(model):,} train parameters')
optimizer = optim.Adam(model.parameters())
# weight_CE = torch.FloatTensor([0.1,1,1,1,1,1,1,1,1,1]).to(device)
# criterion = nn.CrossEntropyLoss(weight = weight_CE)
criterion = nn.CrossEntropyLoss()
# train data
train_paths = glob.glob("data/train/*.json")
test_paths = glob.glob("data/test/*.json")
train_steps = len(train_paths)//config.s2s_batch_size+1
test_steps = len(test_paths)//config.s2s_batch_size+1
train_iterator = gen(train_paths, config.s2s_batch_size, config.max_box_num, device)
test_iterator = gen(test_paths, config.s2s_batch_size, config.max_box_num, device)
best_valid_loss = float('inf')
for epoch in range(config.s2s_epoch):
start_time = time.time()
train_loss, train_acc = train(model, train_steps, train_iterator,
optimizer, criterion, config.s2s_clip)
valid_loss, valid_acc = evaluate(
model, test_steps, test_iterator, criterion)
end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict(), config.s2s_path)
print(f'Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s')
print(
f'\tTrain Loss: {train_loss:.3f} | Train ACC: {train_acc:.3f}')
print(
f'\t Val. Loss: {valid_loss:.3f} | Val. ACC: {valid_acc:.3f}')