forked from zhangj111/astnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
145 lines (124 loc) · 4.78 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
import pandas as pd
import random
import torch
import time
import numpy as np
from gensim.models.word2vec import Word2Vec
from model import BatchProgramClassifier
from torch.autograd import Variable
from torch.utils.data import DataLoader
import os
import sys
def get_batch(dataset, idx, bs):
tmp = dataset.iloc[idx: idx+bs]
data, labels = [], []
for _, item in tmp.iterrows():
data.append(item[1])
labels.append(item[2]-1)
return data, torch.LongTensor(labels)
if __name__ == '__main__':
root = 'data/'
train_data = pd.read_pickle(root+'train/blocks.pkl')
val_data = pd.read_pickle(root + 'dev/blocks.pkl')
test_data = pd.read_pickle(root+'test/blocks.pkl')
word2vec = Word2Vec.load(root+"train/embedding/node_w2v_128").wv
embeddings = np.zeros((word2vec.syn0.shape[0] + 1, word2vec.syn0.shape[1]), dtype="float32")
embeddings[:word2vec.syn0.shape[0]] = word2vec.syn0
HIDDEN_DIM = 100
ENCODE_DIM = 128
LABELS = 104
EPOCHS = 15
BATCH_SIZE = 64
USE_GPU = True
MAX_TOKENS = word2vec.syn0.shape[0]
EMBEDDING_DIM = word2vec.syn0.shape[1]
model = BatchProgramClassifier(EMBEDDING_DIM,HIDDEN_DIM,MAX_TOKENS+1,ENCODE_DIM,LABELS,BATCH_SIZE,
USE_GPU, embeddings)
if USE_GPU:
model.cuda()
parameters = model.parameters()
optimizer = torch.optim.Adamax(parameters)
loss_function = torch.nn.CrossEntropyLoss()
train_loss_ = []
val_loss_ = []
train_acc_ = []
val_acc_ = []
best_acc = 0.0
print('Start training...')
# training procedure
best_model = model
for epoch in range(EPOCHS):
start_time = time.time()
total_acc = 0.0
total_loss = 0.0
total = 0.0
i = 0
while i < len(train_data):
batch = get_batch(train_data, i, BATCH_SIZE)
i += BATCH_SIZE
train_inputs, train_labels = batch
if USE_GPU:
train_inputs, train_labels = train_inputs, train_labels.cuda()
model.zero_grad()
model.batch_size = len(train_labels)
model.hidden = model.init_hidden()
output = model(train_inputs)
loss = loss_function(output, Variable(train_labels))
loss.backward()
optimizer.step()
# calc training acc
_, predicted = torch.max(output.data, 1)
total_acc += (predicted == train_labels).sum()
total += len(train_labels)
total_loss += loss.item()*len(train_inputs)
train_loss_.append(total_loss / total)
train_acc_.append(total_acc.item() / total)
# validation epoch
total_acc = 0.0
total_loss = 0.0
total = 0.0
i = 0
while i < len(val_data):
batch = get_batch(val_data, i, BATCH_SIZE)
i += BATCH_SIZE
val_inputs, val_labels = batch
if USE_GPU:
val_inputs, val_labels = val_inputs, val_labels.cuda()
model.batch_size = len(val_labels)
model.hidden = model.init_hidden()
output = model(val_inputs)
loss = loss_function(output, Variable(val_labels))
# calc valing acc
_, predicted = torch.max(output.data, 1)
total_acc += (predicted == val_labels).sum()
total += len(val_labels)
total_loss += loss.item()*len(val_inputs)
val_loss_.append(total_loss / total)
val_acc_.append(total_acc.item() / total)
end_time = time.time()
if total_acc/total > best_acc:
best_model = model
print('[Epoch: %3d/%3d] Training Loss: %.4f, Validation Loss: %.4f,'
' Training Acc: %.3f, Validation Acc: %.3f, Time Cost: %.3f s'
% (epoch + 1, EPOCHS, train_loss_[epoch], val_loss_[epoch],
train_acc_[epoch], val_acc_[epoch], end_time - start_time))
total_acc = 0.0
total_loss = 0.0
total = 0.0
i = 0
model = best_model
while i < len(test_data):
batch = get_batch(test_data, i, BATCH_SIZE)
i += BATCH_SIZE
test_inputs, test_labels = batch
if USE_GPU:
test_inputs, test_labels = test_inputs, test_labels.cuda()
model.batch_size = len(test_labels)
model.hidden = model.init_hidden()
output = model(test_inputs)
loss = loss_function(output, Variable(test_labels))
_, predicted = torch.max(output.data, 1)
total_acc += (predicted == test_labels).sum()
total += len(test_labels)
total_loss += loss.item() * len(test_inputs)
print("Testing results(Acc):", total_acc.item() / total)