forked from 201518018629031/STS-NN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
237 lines (215 loc) · 11.2 KB
/
main.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
# -*- coding: utf-8 -*-
import time
import argparse
import numpy as np
import random, os
import torch
import torch.nn.functional as F
import torch.optim as optim
from strnn import STRNN
from util import loadData, convert_to_one_hot, evaluation_4class, loadData_Cross
from sklearn.metrics import accuracy_score, classification_report
# Training settings
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disables CUDA training.')
parser.add_argument('--fastmode', action='store_true', default=False,
help='Validate during training pass.')
parser.add_argument('--seed', type=int, default=0, help='Random seed.')
parser.add_argument('--dataset', type=str, default='twitter15',
help='the dataset name: twitter15, twitter16(default: twitter15)')
parser.add_argument('--vocab_size', type=int, default=5000,
help='the size of vocabulary (default:5000s)')
parser.add_argument('--input_size', type=int, default=300,
help='the dimension of word embedding (default: 300)')
parser.add_argument('--epochs', type=int, default=30,
help='Number of epochs to train.')
parser.add_argument('--lr', type=float, default=0.005,
help='Initial learning rate.')
parser.add_argument('--weight_decay', type=float, default=5e-4,
help='Weight decay (L2 loss on parameters).')
parser.add_argument('--hidden_size', type=int, default=100,
help='Number of hidden units.')
parser.add_argument('--elapsed_time',type=int, default=3000,
help='the elapsed time after source tweet posted (0, 60(1h), 120(2h), 240(4h), 480(8h), 720(12h), 1440(24h), 2160(36h), default: 3000 represents all)')
parser.add_argument('--tweets_count', type=int, default=500,
help='the tweets count after source tweet posted (0, 10, 20, 40, 60, 80, 200, 300, default: 500 represents all)')
parser.add_argument('--batch_size', type=int, default=64,
help='input batch size for training (default: 64)')
parser.add_argument('--filename', type = str, default = "",
help='output file')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
target_names = ['NR','FR','TR','UR']
if args.dataset == 'weibo':
target_names = ['NR','FR']
random.seed(args.seed)
np.random.seed(args.seed)
os.environ['PYTHONHASHSEED'] = str(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def train(epoch, best_acc, patience, fold_id):
model.train()
total_iters = len(sequences_train)//args.batch_size + 1
loss_accum = 0
avg_acc = 0
idx_list = np.arange(len(sequences_train))
random.shuffle(idx_list)
for i in range(total_iters):
selected_idx = idx_list[(i*args.batch_size):((i+1)*args.batch_size)]
if len(selected_idx) == 0:
continue
output = []
loss_train = []
for j in selected_idx:
x_index = index_train[j]
sequence = sequences_train[j]
out = model(x_index, sequence)
y = y_train[j].view(-1)
# print(y.size())
loss = F.nll_loss(out, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
output.append(out)
loss_train.append(round(float(loss.detach().cpu().numpy()),2))
output = torch.cat([out for out in output], 0)
y_selected = y_train[selected_idx]
corrects = (torch.max(output, 1)[1].view(len(selected_idx)).data == y_selected.data).sum()
accuracy = 100*corrects/len(selected_idx)
if i > 0 and i % 100 == 0:
best_acc, patience = evaluate(best_acc, patience, fold_id)
model.train()
avg_acc += accuracy
print('Batch [{}] - loss:{:.6f} acc:{:.4f}% ({}/{})'.format(i, np.mean(loss_train), accuracy, corrects, len(selected_idx)))
loss_accum += np.mean(loss_train)
average_loss = loss_accum/total_iters
average_acc = avg_acc/total_iters
print("loss training: {:.6f} average_acc: {:.6f}".format(average_loss, average_acc))
return best_acc, patience
def adjust_learning_rate(optimizer, decay_rate=.5):
now_lr = 0
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * decay_rate
now_lr = param_group['lr']
return now_lr
def predict(index_list, sequences_list):
model.eval()
output = []
for i,x_index in enumerate(index_list):
sequence = sequences_list[i]
out = model(x_index, sequence)
output.append(out)
output = torch.cat([out for out in output], 0)
return output
def evaluate(best_acc, patience, fold_id):
output = predict(index_dev, sequences_dev)
predicted = torch.max(output, dim=1)[1]
y_pred = predicted.data.cpu().numpy().tolist()
val_labels = y_dev.data.cpu().numpy().tolist()
acc = accuracy_score(val_labels, y_pred)
if acc > best_acc:
best_acc = acc
patience = 0
if args.elapsed_time == 3000 and args.tweets_count == 500:
torch.save(model.state_dict(), 'weights.best.{}.f{}'.format(args.dataset, fold_id))
elif args.elapsed_time < 3000 and args.tweets_count == 500:
torch.save(model.state_dict(), 'weights.best.{}.f{}.et{}'.format(args.dataset,fold_id, args.elapsed_time))
elif args.elapsed_time == 3000 and args.tweets_count < 500:
torch.save(model.state_dict(), 'weights.best.{}.f{}.tc{}'.format(args.dataset, fold_id, args.tweets_count))
print(classification_report(val_labels, y_pred, target_names=target_names, digits=5))
print('Val set acc: {}'.format(acc))
print('Best val set acc: {}'.format(best_acc))
print('save model!!!!')
else:
patience += 1
return best_acc, patience
def test(fold_id):
# model.eval()
output = predict(index_test, sequences_test)
predicted = torch.max(output, dim=1)[1]
y_pred = predicted.data.cpu().numpy().tolist()
test_labels = y_test.data.cpu().numpy().tolist()
print('=====================================')
print('the result of {}-fold:'.format(fold_id))
print(classification_report(test_labels, y_pred, target_names=target_names, digits=5))
with open('result.{}.f{}.et{}.tc{}'.format(args.dataset, fold_id, args.elapsed_time, args.tweets_count), 'w') as f:
f.write('====================================='+'\n')
f.write(classification_report(test_labels, y_pred, target_names=target_names, digits=5))
t_labels = convert_to_one_hot(y_test.unsqueeze(1).cpu(), 4).cuda()
if args.dataset == 'weibo':
t_labels = convert_to_one_hot(y_test.unsqueeze(1).cpu(), 2).cuda()
result_test = evaluation_4class(output, t_labels)
return result_test
fold_ids = [i for i in range(5)]
result_test_dict = {}
for i in range(20):
result_test_dict[i] = []
for fold_id in fold_ids:
sequences_train, index_train, y_train, sequences_dev, index_dev, y_dev, sequences_test, index_test, y_test = loadData_Cross(args.dataset, args.vocab_size, args.elapsed_time, args.tweets_count, fold_id)
model = STRNN(vocab_size=args.vocab_size,
input_size=args.input_size,
hidden_size=args.hidden_size,
nclass=y_train.max().item() + 1,
device=args.cuda)
optimizer = optim.Adam(model.parameters(),
lr=args.lr, weight_decay=args.weight_decay)
# optimizer = optim.Adagrad(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
if args.cuda:
model.cuda()
y_train = y_train.cuda()
y_dev = y_dev.cuda()
y_test = y_test.cuda()
# Train model
best_acc = 0.0
patience = 0
t_total = time.time()
for epoch in range(1, args.epochs+1):
print("Epoch {}/{}".format(epoch, args.epochs))
best_acc, patience = train(epoch, best_acc, patience,fold_id)
if epoch >= 10 and patience > 3:
print('Reload the best model ...')
if args.elapsed_time == 3000 and args.tweets_count == 500:
model.load_state_dict(torch.load('weights.best.{}.f{}'.format(args.dataset, fold_id)))
elif args.elapsed_time < 3000 and args.tweets_count == 500:
model.load_state_dict(torch.load('weights.best.{}.f{}.et{}'.format(args.dataset, fold_id, args.elapsed_time)))
elif args.elapsed_time == 3000 and args.tweets_count < 500:
model.load_state_dict(torch.load('weights.best.{}.f{}.tc{}'.format(args.dataset, fold_id, args.tweets_count)))
now_lr = adjust_learning_rate(optimizer)
print(now_lr)
patience = 0
best_acc, patience = evaluate(best_acc, patience, fold_id)
print("Optimization Finished!")
print("Total time elapsed: {:.4f}s".format(time.time() - t_total))
# Testing
print('Loading model to test set ...')
if args.elapsed_time == 3000 and args.tweets_count == 500:
model.load_state_dict(torch.load('weights.best.{}.f{}'.format(args.dataset, fold_id)))
elif args.elapsed_time < 3000 and args.tweets_count == 500:
model.load_state_dict(torch.load('weights.best.{}.f{}.et{}'.format(args.dataset, fold_id, args.elapsed_time)))
elif args.elapsed_time == 3000 and args.tweets_count < 500:
model.load_state_dict(torch.load('weights.best.{}.f{}.tc{}'.format(args.dataset, fold_id, args.tweets_count)))
result_test = test(fold_id)
for i in range(20):
result_test_dict[i].append(result_test[i])
result = []
for i in range(20):
result.append(np.mean(result_test_dict[i]))
print('the result of {}-fold cross validation in test set:'.format(len(fold_ids)))
print('acc:{:.4f} Favg:{:.4f},{:.4f},{:.4f}'.format(result[0], result[1], result[2], result[3]) +
' C1:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[4], result[5], result[6], result[7]) +
' C2:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[8], result[9], result[10], result[11]) +
' C3:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[12], result[13], result[14], result[15]) +
' C4:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[16], result[17], result[18], result[19]))
if not args.filename == "":
with open(args.filename, 'w') as f:
f.write('the result of {}-fold cross validation in test set:'.format(len(fold_ids)))
f.write('acc:{:.4f} Favg:{:.4f},{:.4f},{:.4f}'.format(result[0], result[1], result[2], result[3]) +
' C1:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[4], result[5], result[6], result[7]) +
' C2:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[8], result[9], result[10], result[11]) +
' C3:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[12], result[13], result[14], result[15]) +
' C4:{:.4f},{:.4f},{:.4f},{:.4f}'.format(result[16], result[17], result[18], result[19]))