forked from albertwy/BiLSTM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_batch.py
282 lines (218 loc) · 9.77 KB
/
main_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# coding:utf8
import argparse
import os
import time
from progress.bar import Bar
import yutils
import numpy
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from nnet.blstm import BLSTM
from nnet.lstm import LSTM
from nnet.cnn import CNN
torch.manual_seed(123456)
def test_prf(pred, labels):
"""
4. log and return prf scores
:return:
"""
total = len(labels)
pred_right = [0, 0]
pred_all = [0, 0]
gold = [0, 0]
for i in range(total):
pred_all[pred[i]] += 1
if pred[i] == labels[i]:
pred_right[pred[i]] += 1
gold[labels[i]] += 1
print " Prediction:", pred_all, " Right:", pred_right, " Gold:", gold
''' -- for all labels -- '''
print " ****** Neg|Neu|Pos ******"
accuracy = 1.0 * sum(pred_right) / total
p, r, f1 = yutils.cal_prf(pred_all, pred_right, gold, formation=False)
_, _, macro_f1 = yutils.cal_prf(pred_all, pred_right, gold,
formation=False,
metric_type="macro")
print " Accuracy on test is %d/%d = %f" % (sum(pred_right), total, accuracy)
print " Precision: %s\n Recall : %s\n F1 score : %s\n Macro F1 score on test (Neg|Neu|Pos) is %f" \
% (p, r, f1, macro_f1)
return accuracy
def test(model, dataset, args, data_part="test"):
"""
:param model:
:param args:
:param dataset:
:param data_part:
:return:
"""
tvt_set = dataset[data_part]
tvt_set = yutils.YDataset(tvt_set["xIndexes"],
tvt_set["yLabels"],
to_pad=True, max_len=args.sen_max_len)
test_set = tvt_set
sentences, sentences_seqlen, sentences_mask, labels = test_set.next_batch(len(test_set))
assert len(test_set) == len(sentences) == len(labels)
tic = time.time()
model.eval()
''' Prepare data and prediction'''
batch_size = len(sentences)
sentences_, sentences_seqlen_, sentences_mask_ = \
var_batch(args, batch_size, sentences, sentences_seqlen, sentences_mask)
probs = model(sentences_, sentences_seqlen_, sentences_mask_)
_, pred = torch.max(probs, dim=1)
if args.cuda:
pred = pred.view(-1).cpu().data.numpy()
else:
pred = pred.view(-1).data.numpy()
tit = time.time() - tic
print " Predicting {:d} examples using {:5.4f} seconds".format(len(test_set), tit)
labels = numpy.asarray(labels)
''' log and return prf scores '''
accuracy = test_prf(pred, labels)
return accuracy
def var_batch(args, batch_size, sentences, sentences_seqlen, sentences_mask):
"""
Transform the input batch to PyTorch variables
:return:
"""
# dtype = torch.from_numpy(sentences, dtype=torch.cuda.LongTensor)
sentences_ = Variable(torch.LongTensor(sentences).view(batch_size, args.sen_max_len))
sentences_seqlen_ = Variable(torch.LongTensor(sentences_seqlen).view(batch_size, 1))
sentences_mask_ = Variable(torch.LongTensor(sentences_mask).view(batch_size, args.sen_max_len))
if args.cuda:
sentences_ = sentences_.cuda()
sentences_seqlen_ = sentences_seqlen_.cuda()
sentences_mask_ = sentences_mask_.cuda()
return sentences_, sentences_seqlen_, sentences_mask_
def train(model, training_data, args, optimizer, criterion):
model.train()
batch_size = args.batch_size
sentences, sentences_seqlen, sentences_mask, labels = training_data
# print batch_size, len(sentences), len(labels)
assert batch_size == len(sentences) == len(labels)
''' Prepare data and prediction'''
sentences_, sentences_seqlen_, sentences_mask_ = \
var_batch(args, batch_size, sentences, sentences_seqlen, sentences_mask)
labels_ = Variable(torch.LongTensor(labels))
if args.cuda:
labels_ = labels_.cuda()
assert len(sentences) == len(labels)
model.zero_grad()
probs = model(sentences_, sentences_seqlen_, sentences_mask_)
loss = criterion(probs.view(len(labels_), -1), labels_)
loss.backward()
optimizer.step()
def main(args):
# define location to save the model
if args.save == "__":
# LSTM_100_40_8
args.save = "saved_model/%s_%d_%d_%d" % \
(args.model, args.nhid, args.sen_max_len, args.batch_size)
in_dir = "data/mr/"
dataset = yutils.pickle2dict(in_dir + "features_glove.pkl")
if args.is_test:
with open(args.save + "/model.pt") as f:
model = torch.load(f)
test(model, dataset, args)
else:
''' make sure the folder to save models exist '''
if not os.path.exists(args.save):
os.mkdir(args.save)
embeddings = yutils.pickle2dict(in_dir + "embeddings_glove.pkl")
dataset["embeddings"] = embeddings
emb_np = numpy.asarray(embeddings, dtype=numpy.float32) # from_numpy
emb = torch.from_numpy(emb_np)
models = {"LSTM": LSTM, "BLSTM": BLSTM, "CNN": CNN}
model = models[args.model](embeddings=emb,
input_dim=args.embsize,
hidden_dim=args.nhid,
num_layers=args.nlayers,
output_dim=2,
max_len=args.sen_max_len,
dropout=args.dropout)
if torch.cuda.is_available():
if not args.cuda:
print "Waring: You have a CUDA device, so you should probably run with --cuda"
else:
torch.cuda.manual_seed(args.seed)
model.cuda()
optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=1e-5)
criterion = nn.CrossEntropyLoss()
training_set = dataset["training"]
training_set = yutils.YDataset(training_set["xIndexes"],
training_set["yLabels"],
to_pad=True,
max_len=args.sen_max_len)
best_acc_test, best_acc_valid = -numpy.inf, -numpy.inf
batches_per_epoch = int(len(training_set)/args.batch_size)
print "--------------\nEpoch 0 begins!"
max_train_steps = int(args.epochs * batches_per_epoch * 10)
bar = Bar(" Processing", max=max_train_steps)
tic = time.time()
print "-----------------------------", max_train_steps, len(training_set), args.batch_size
for step in xrange(max_train_steps):
bar.next()
training_batch = training_set.next_batch(args.batch_size)
train(model, training_batch, args, optimizer, criterion)
if (step+1) % batches_per_epoch == 0:
print " using %.5f seconds" % (time.time() - tic)
tic = time.time()
''' Test after each epoch '''
print "\n Begin to predict the results on Validation"
acc_score = test(model, dataset, args, data_part="validation")
print " ----Old best acc score on validation is %f" % best_acc_valid
if acc_score > best_acc_valid:
print " ----New acc score on validation is %f" % acc_score
best_acc_valid = acc_score
with open(args.save + "/model.pt", 'wb') as to_save:
torch.save(model, to_save)
acc_test = test(model, dataset, args)
print " ----Old best acc score on test is %f" % best_acc_test
if acc_test > best_acc_test:
best_acc_test = acc_test
print " ----New acc score on test is %f" % acc_test
print "--------------\nEpoch %d begins!" % (training_set.epochs_completed + 1)
# print the final result
with open(args.save + "/model.pt") as f:
model = torch.load(f)
test(model, dataset, args)
bar.finish()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PyTorch AoA for Stance Project")
''' load data and save model'''
parser.add_argument("--save", type=str, default="__",
help="path to save the model")
''' model parameters '''
parser.add_argument("--model", type=str, default="BLSTM",
help="type of model to use for Stance Project")
parser.add_argument("--embsize", type=int, default=100,
help="size of word embeddings")
parser.add_argument("--emb", type=str, default="glove",
help="type of word embeddings")
parser.add_argument("--nhid", type=int, default=50,
help="size of RNN hidden layer")
parser.add_argument("--nlayers", type=int, default=1,
help="number of layers of LSTM")
parser.add_argument("--lr", type=float, default=0.01,
help="learning rate")
parser.add_argument("--epochs", type=int, default=100,
help="number of training epoch")
parser.add_argument("--batch_size", type=int, default=8,
help="batch size")
parser.add_argument("--dropout", type=float, default=0.1,
help="dropout rate")
parser.add_argument("--seed", type=int, default=123456,
help="random seed for reproduction")
parser.add_argument("--cuda", action="store_true",
help="use CUDA")
parser.add_argument("--sen_max_len", type=int, default=40,
help="max time step of tweet sequence")
''' test purpose'''
parser.add_argument("--is_test", action="store_true",
help="flag for training model or only test")
my_args = parser.parse_args()
torch.manual_seed(my_args.seed)
main(my_args)