-
Notifications
You must be signed in to change notification settings - Fork 63
/
train_batch.py
218 lines (185 loc) · 7.29 KB
/
train_batch.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import torch
import torch.nn as nn
from torch import optim
import time, random
import os
from tqdm import tqdm
from lstm import LSTMSentiment
from bilstm import BiLSTMSentiment
from torchtext import data
import numpy as np
import argparse
torch.set_num_threads(8)
torch.manual_seed(1)
random.seed(1)
def load_bin_vec(fname, vocab):
"""
Loads 300x1 word vecs from Google (Mikolov) word2vec
"""
word_vecs = {}
with open(fname, "rb") as f:
header = f.readline()
vocab_size, layer1_size = map(int, header.split())
binary_len = np.dtype('float32').itemsize * layer1_size
for line in range(vocab_size):
word = []
while True:
ch = f.read(1).decode('latin-1')
if ch == ' ':
word = ''.join(word)
break
if ch != '\n':
word.append(ch)
if word in vocab:
word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32')
else:
f.read(binary_len)
return word_vecs
def get_accuracy(truth, pred):
assert len(truth) == len(pred)
right = 0
for i in range(len(truth)):
if truth[i] == pred[i]:
right += 1.0
return right / len(truth)
def train_epoch_progress(model, train_iter, loss_function, optimizer, text_field, label_field, epoch):
model.train()
avg_loss = 0.0
truth_res = []
pred_res = []
count = 0
for batch in tqdm(train_iter, desc='Train epoch '+str(epoch+1)):
sent, label = batch.text, batch.label
label.data.sub_(1)
truth_res += list(label.data)
model.batch_size = len(label.data)
model.hidden = model.init_hidden()
pred = model(sent)
pred_label = pred.data.max(1)[1].numpy()
pred_res += [x for x in pred_label]
model.zero_grad()
loss = loss_function(pred, label)
avg_loss += loss.data[0]
count += 1
loss.backward()
optimizer.step()
avg_loss /= len(train_iter)
acc = get_accuracy(truth_res, pred_res)
return avg_loss, acc
def train_epoch(model, train_iter, loss_function, optimizer):
model.train()
avg_loss = 0.0
truth_res = []
pred_res = []
count = 0
for batch in train_iter:
sent, label = batch.text, batch.label
label.data.sub_(1)
truth_res += list(label.data)
model.batch_size = len(label.data)
model.hidden = model.init_hidden()
pred = model(sent)
pred_label = pred.data.max(1)[1].numpy()
pred_res += [x for x in pred_label]
model.zero_grad()
loss = loss_function(pred, label)
avg_loss += loss.data[0]
count += 1
loss.backward()
optimizer.step()
avg_loss /= len(train_iter)
acc = get_accuracy(truth_res, pred_res)
return avg_loss, acc
def evaluate(model, data, loss_function, name):
model.eval()
avg_loss = 0.0
truth_res = []
pred_res = []
for batch in data:
sent, label = batch.text, batch.label
label.data.sub_(1)
truth_res += list(label.data)
model.batch_size = len(label.data)
model.hidden = model.init_hidden()
pred = model(sent)
pred_label = pred.data.max(1)[1].numpy()
pred_res += [x for x in pred_label]
loss = loss_function(pred, label)
avg_loss += loss.data[0]
avg_loss /= len(data)
acc = get_accuracy(truth_res, pred_res)
print(name + ': loss %.2f acc %.1f' % (avg_loss, acc*100))
return acc
def load_sst(text_field, label_field, batch_size):
train, dev, test = data.TabularDataset.splits(path='./data/SST2/', train='train.tsv',
validation='dev.tsv', test='test.tsv', format='tsv',
fields=[('text', text_field), ('label', label_field)])
text_field.build_vocab(train, dev, test)
label_field.build_vocab(train, dev, test)
train_iter, dev_iter, test_iter = data.BucketIterator.splits((train, dev, test),
batch_sizes=(batch_size, len(dev), len(test)), sort_key=lambda x: len(x.text), repeat=False, device=-1)
## for GPU run
# train_iter, dev_iter, test_iter = data.BucketIterator.splits((train, dev, test),
# batch_sizes=(batch_size, len(dev), len(test)), sort_key=lambda x: len(x.text), repeat=False, device=None)
return train_iter, dev_iter, test_iter
# def adjust_learning_rate(learning_rate, optimizer, epoch):
# lr = learning_rate * (0.1 ** (epoch // 10))
# for param_group in optimizer.param_groups:
# param_group['lr'] = lr
# return optimizer
args = argparse.ArgumentParser()
args.add_argument('--m', dest='model', default='lstm', help='specify the mode to use (default: lstm)')
args = args.parse_args()
EPOCHS = 20
USE_GPU = torch.cuda.is_available()
EMBEDDING_DIM = 300
HIDDEN_DIM = 150
BATCH_SIZE = 5
timestamp = str(int(time.time()))
best_dev_acc = 0.0
text_field = data.Field(lower=True)
label_field = data.Field(sequential=False)
train_iter, dev_iter, test_iter = load_sst(text_field, label_field, BATCH_SIZE)
if not args.model:
model = LSTMSentiment(embedding_dim=EMBEDDING_DIM, hidden_dim=HIDDEN_DIM, vocab_size=len(text_field.vocab), label_size=len(label_field.vocab)-1,\
use_gpu=USE_GPU, batch_size=BATCH_SIZE)
if args.model == 'bilstm':
model = BiLSTMSentiment(embedding_dim=EMBEDDING_DIM, hidden_dim=HIDDEN_DIM, vocab_size=len(text_field.vocab), label_size=len(label_field.vocab)-1,\
use_gpu=USE_GPU, batch_size=BATCH_SIZE)
if USE_GPU:
model = model.cuda()
print('Load word embeddings...')
# # glove
# text_field.vocab.load_vectors('glove.6B.100d')
# word2vector
word_to_idx = text_field.vocab.stoi
pretrained_embeddings = np.random.uniform(-0.25, 0.25, (len(text_field.vocab), 300))
pretrained_embeddings[0] = 0
word2vec = load_bin_vec('./data/GoogleNews-vectors-negative300.bin', word_to_idx)
for word, vector in word2vec.items():
pretrained_embeddings[word_to_idx[word]-1] = vector
# text_field.vocab.load_vectors(wv_type='', wv_dim=300)
model.embeddings.weight.data.copy_(torch.from_numpy(pretrained_embeddings))
# model.embeddings.weight.data = text_field.vocab.vectors
# model.embeddings.embed.weight.requires_grad = False
best_model = model
optimizer = optim.Adam(model.parameters(), lr=1e-3)
loss_function = nn.NLLLoss()
print('Training...')
out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp))
print("Writing to {}\n".format(out_dir))
if not os.path.exists(out_dir):
os.makedirs(out_dir)
for epoch in range(EPOCHS):
avg_loss, acc = train_epoch_progress(model, train_iter, loss_function, optimizer, text_field, label_field, epoch)
tqdm.write('Train: loss %.2f acc %.1f' % (avg_loss, acc*100))
dev_acc = evaluate(model, dev_iter, loss_function, 'Dev')
if dev_acc > best_dev_acc:
if best_dev_acc > 0:
os.system('rm '+ out_dir + '/best_model' + '.pth')
best_dev_acc = dev_acc
best_model = model
torch.save(best_model.state_dict(), out_dir + '/best_model' + '.pth')
# evaluate on test with the best dev performance model
test_acc = evaluate(best_model, test_iter, loss_function, 'Test')
test_acc = evaluate(best_model, test_iter, loss_function, 'Final Test')