-
Notifications
You must be signed in to change notification settings - Fork 19
/
sequence_train.py
83 lines (71 loc) · 2.78 KB
/
sequence_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
import torch
from torch import optim
import torch.nn.functional as F
import generate_data
from utils import to_var
from pointer_network import PointerNetwork
total_size = 10000
weight_size = 256
emb_size = 32
batch_size = 250
n_epochs = 5
input_seq_len = 4
input, targets = generate_data.make_seq_data(total_size, input_seq_len)
inp_size = input_seq_len
# Convert to torch tensors
input = to_var(torch.LongTensor(input)) # (N, L)
targets = to_var(torch.LongTensor(targets)) # (N, L)
data_split = (int)(total_size * 0.9)
train_X = input[:data_split]
train_Y = targets[:data_split]
test_X = input[data_split:]
test_Y = targets[data_split:]
# from pointer_network import PointerNetwork
def train(model, X, Y, batch_size, n_epochs):
model.train()
optimizer = optim.Adam(model.parameters())
N = X.size(0)
L = X.size(1)
# M = Y.size(1)
for epoch in range(n_epochs + 1):
# for i in range(len(train_batches))
for i in range(0, N-batch_size, batch_size):
x = X[i:i+batch_size] # (bs, L)
y = Y[i:i+batch_size] # (bs, M)
probs = model(x) # (bs, M, L)
outputs = probs.view(-1, L) # (bs*M, L)
# outputs = probs.view(L, -1).t().contiguous() # (bs*M, L)
y = y.view(-1) # (bs*M)
loss = F.nll_loss(outputs, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if epoch % 2 == 0:
print('epoch: {}, Loss: {:.5f}'.format(epoch, loss.item()))
# for _ in range(2): # random showing results
# pick = np.random.randint(0, batch_size)
# probs = probs.contiguous().view(batch_size, M, L).transpose(2, 1) # (bs, L, M)
# y = y.view(batch_size, M)
# print("predict: ", probs.max(1)[1].data[pick][0], probs.max(1)[1].data[pick][1],
# "target : ", y.data[pick][0], y.data[pick][1])
test(model, X, Y)
def test(model, X, Y):
probs = model(X) # (bs, M, L)
_v, indices = torch.max(probs, 2) # (bs, M)
# show test examples
# for i in range(len(indices)):
# print('-----')
# print('test', [v for v in X[i].data])
# print('label', [v for v in Y[i].data])
# print('pred', [v for v in indices[i].data])
# if torch.equal(Y[i].data, indices[i].data):
# print('eq')
# if i>20: break
correct_count = sum([1 if torch.equal(ind.data, y.data) else 0 for ind, y in zip(indices, Y)])
print('Acc: {:.2f}% ({}/{})'.format(correct_count/len(X)*100, correct_count, len(X)))
model = PointerNetwork(inp_size, emb_size, weight_size, input_seq_len)
if torch.cuda.is_available():
model.cuda()
train(model, train_X, train_Y, batch_size, n_epochs)
print('----Test result---')
test(model, test_X, test_Y)