-
Notifications
You must be signed in to change notification settings - Fork 2
/
han_end2end.py
382 lines (289 loc) · 14.2 KB
/
han_end2end.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import time
import torch
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torch.optim as optim
from model import HierarchialAttentionNetwork
from utils import *
from datasets import HANDataset
from sklearn.metrics import recall_score, precision_score, f1_score, accuracy_score
# Data parameters
data_folder = './outdata'
word2vec_file = os.path.join(data_folder, 'word2vec_model') # path to pre-trained word2vec embeddings
with open(os.path.join(data_folder, 'word_map.json'), 'r') as j:
word_map = json.load(j)
classes = ['relevant', 'Armed Assault', 'Bombing/Explosion', 'Kidnapping', 'Other']
label_map = {k: v for v, k in enumerate(classes)}
rev_label_map = {v: k for k, v in label_map.items()}
# Model parameters
n_classes = len(label_map)
print(n_classes)
# word_rnn_size = 50 # word RNN size
# sentence_rnn_size = 50 # character RNN size
# word_att_size = 100 # size of the word-level attention layer (also the size of the word context vector)
# sentence_att_size = 100 # size of the sentence-level attention layer (also the size of the sentence context vector)
word_rnn_size = 200 # word RNN size
sentence_rnn_size = 200 # character RNN size
word_att_size = 400 # size of the word-level attention layer (also the size of the word context vector)
sentence_att_size = 400 # size of the sentence-level attention layer (also the size of the sentence context vector)
word_rnn_layers = 1 # number of layers in character RNN
sentence_rnn_layers = 1 # number of layers in word RNN
dropout = 0.3 # dropout
fine_tune_word_embeddings = True # fine-tune word embeddings?
# Training parameters
start_epoch = 0 # start at this epoch
batch_size = 64 # batch size
lr = 1e-3 # learning rate
momentum = 0.9 # momentum
workers = 4 # number of workers for loading data in the DataLoader
epochs = 7 # number of epochs to run
grad_clip = None # clip gradients at this value
print_freq = 2000 # print training or validation status every __ batches
checkpoint = None # path to model checkpoint, None if none
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
cudnn.benchmark = True # set to true only if inputs to model are fixed size; otherwise lot of computational overhead
### TODO eval
# Evaluation parameters
threshold = 0.5
####
def train(train_loader, model, criterion, optimizer, epoch,threshold = 0.5):
"""
Performs one epoch's training.
:param train_loader: DataLoader for training data
:param model: model
:param criterion: cross entropy loss layer
:param optimizer: optimizer
:param epoch: epoch number
"""
model.train() # training mode enables dropout
batch_time = AverageMeter() # forward prop. + back prop. time per batch
data_time = AverageMeter() # data loading time per batch
losses = AverageMeter() # cross entropy loss
accs = AverageMeter() # accuracies
start = time.time()
# Batches
for i, (documents, sentences_per_document, words_per_sentence, labels) in enumerate(train_loader):
data_time.update(time.time() - start)
documents = documents.to(device) # (batch_size, sentence_limit, word_limit)
sentences_per_document = sentences_per_document.squeeze(1).to(device) # (batch_size)
words_per_sentence = words_per_sentence.to(device) # (batch_size, sentence_limit)
labels = 1.0 * labels.squeeze(1).to(device) # (batch_size)
# TODO ##################################
# labels = labels[:,1:]
# Forward prop.
scores, word_alphas, sentence_alphas \
= model(documents, sentences_per_document, words_per_sentence)
# (n_documents, n_classes), (n_documents, max_doc_len_in_batch, max_sent_len_in_batch), (n_documents, max_doc_len_in_batch)
# Loss
loss = criterion(scores, labels) # scalar
# criterion = nn.CrossEntropyLoss()
# criterion = nn.BCEWithLogitsLoss()(scores, 1.0*labels)
# Back prop.
optimizer.zero_grad()
loss.backward()
# Clip gradients
if grad_clip is not None:
clip_gradient(optimizer, grad_clip)
# Update
optimizer.step()
# Find accuracy
# _, predictions = scores.max(dim=1) # (n_documents)
# threshold = 0.5
predictions = F.sigmoid(scores)
predictions[predictions >= threshold] = 1
predictions[predictions < threshold] = 0
#
# correct_predictions = torch.eq(predictions, labels).sum().item()
# accuracy = correct_predictions / labels.size(0)
accuracy = accuracy_score(labels.detach().cpu().numpy(), predictions.detach().cpu().numpy())
# Keep track of metrics
losses.update(loss.item(), labels.size(0))
batch_time.update(time.time() - start)
accs.update(accuracy, labels.size(0))
start = time.time()
# Print training status
if i % print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data Load Time {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Accuracy {acc.val:.3f} ({acc.avg:.3f})'.format(epoch, i, len(train_loader),
batch_time=batch_time,
data_time=data_time, loss=losses,
acc=accs))
def eval(test_loader, model,threshold):
# Track metrics
accs1 = AverageMeter()
res1 = []
lab1 = []
accs2 = AverageMeter()
res2 = []
lab2 = []
accs = AverageMeter()
res = []
lab = []
# Evaluate in batches
for i, (documents, sentences_per_document, words_per_sentence, labels) in enumerate(
tqdm(test_loader, desc='Evaluating')):
documents = documents.to(device) # (batch_size, sentence_limit, word_limit)
sentences_per_document = sentences_per_document.squeeze(1).to(device) # (batch_size)
words_per_sentence = words_per_sentence.to(device) # (batch_size, sentence_limit)
labels = labels.squeeze(1).to(device) # (batch_size)
# Forward prop.
scores, word_alphas, sentence_alphas = model(documents, sentences_per_document,
words_per_sentence) # (n_documents, n_classes), (n_documents, max_doc_len_in_batch, max_sent_len_in_batch), (n_documents, max_doc_len_in_batch)
# Find accuracy
labels = labels.detach().cpu().numpy()
y_multi = labels[:, 1:] # True multi-labels
y_relevant = labels[:, 0] # True relevant labels
pred = F.sigmoid(scores)
pred[pred >= threshold] = 1
pred[pred < threshold] = 0
pred = pred.detach().cpu().numpy() # predicted multi-labels
# TODO:
# 1. predicted relevant labels: if all the multi-labels are False, then the relevant labels will be False.
# 2. Use relevant directly ( End to End with relevant)
# pred_relevant = list(map(int, pred_multi.any(axis=1)))
pred_relevant = pred[:,0]
pred_multi = pred[:,1:]
# TODO : Multi-label classfication
acc_multi = accuracy_score(y_multi, pred_multi)
accs1.update(acc_multi, labels.shape[0])
res1.extend(pred_multi.tolist())
lab1.extend(y_multi.tolist())
# TODO : is-relevant or not binary classfication
# y_relevant = labels[:,0]
acc_relevant = accuracy_score(y_relevant, pred_relevant)
accs2.update(acc_relevant, labels.shape[0])
res2.extend(pred_relevant)
lab2.extend(y_relevant)
# Overall multi-label classification ( if we consider relevant as a class)
acc = accuracy_score(labels, pred)
accs.update(acc, labels.shape[0])
res.extend(pred)
lab.extend(labels)
# Print final result
# print('\n* Multi-label TEST ACCURACY \t %.3f' % (accs1.avg))
# print("* RECALL SCORE\t\t %.3f" % recall_score(lab1, res1, average='micro'))
# print("* PRECISION SCORE\t %.3f" % precision_score(lab1, res1, average='micro'))
# print("* F1 SCORE\t\t\t %.3f" % f1_score(lab1, res1, average='micro'))
#
# # print(multilabel_confusion_matrix(lab1, res1))
# # from sklearn.metrics import multilabel_confusion_matrix
#
# print('\n* Relevant ACCURACY \t %.3f' % (accs2.avg))
# print("* RECALL SCORE\t\t %.3f" % recall_score(lab2, res2, average='micro'))
# print("* PRECISION SCORE\t %.3f" % precision_score(lab2, res2, average='micro'))
# print("* F1 SCORE\t\t\t %.3f" % f1_score(lab2, res2, average='micro'))
# # print(multilabel_confusion_matrix(lab2, res2))
#
#
# print('\n* Overall ACCURACY \t %.3f' % (accs.avg))
# print("* RECALL SCORE\t\t %.3f" % recall_score(lab, res, average='micro'))
# print("* PRECISION SCORE\t %.3f" % precision_score(lab, res, average='micro'))
# print("* F1 SCORE\t\t\t %.3f" % f1_score(lab, res, average='micro'))
df = pd.DataFrame(columns = ['threshold', 'type', 'accuracy',
'recall(micro)', 'precision(micro)', 'f1(micro)',
'recall(macro)', 'precision(macro)', 'f1(macro)',
# 'recall(custom)', 'precision(custom)', 'f1(custom)', 'EMR', 'accuracy(custom)'
'accuracy(custom)', 'precision(custom)','recall(custom)', 'f1(custom)', 'EMR']
)
df.loc[len(df)] = [threshold,
'multi-label',
accs1.avg,
recall_score(lab1, res1, average='micro'),
precision_score(lab1, res1, average='micro'),
f1_score(lab1, res1, average='micro'),
recall_score(lab1, res1, average='macro'),
precision_score(lab1, res1, average='macro'),
f1_score(lab1, res1, average='macro'),
accuracy_multilabel(lab1, res1),
precision_multilabel(lab1, res1),
recall_multilabel(lab1, res1),
f1_multilabel(lab1, res1),
accuracy_score(np.array(lab1), np.array(res1))
]
df.loc[len(df)] = [threshold,
'relevant',
accs2.avg,
recall_score(lab2, res2, average='micro'),
precision_score(lab2, res2, average='micro'),
f1_score(lab2, res2, average='micro'),
recall_score(lab2, res2, average='macro'),
precision_score(lab2, res2, average='macro'),
f1_score(lab2, res2, average='macro'),
None, None, None, None, None
# recall_multilabel(lab1, res1),
# precision_multilabel(lab1, res1),
# f1_multilabel(lab1, res1),
# accuracy_score(np.array(lab1), np.array(res1)),
# accuracy_multilabel(lab1, res1),
]
df.loc[len(df)] = [threshold,
'overall',
accs.avg,
recall_score(lab, res, average='micro'),
precision_score(lab, res, average='micro'),
f1_score(lab, res, average='micro'),
recall_score(lab, res, average='macro'),
precision_score(lab, res, average='macro'),
f1_score(lab, res, average='macro'),
accuracy_multilabel(lab, res),
precision_multilabel(lab, res),
recall_multilabel(lab, res),
f1_multilabel(lab, res),
accuracy_score(np.array(lab), np.array(res)),
]
return df
# def main():
"""
Training and validation.
"""
# global checkpoint, start_epoch, word_map
# Initialize model
embeddings, emb_size = load_word2vec_embeddings(word2vec_file, word_map) # load pre-trained word2vec embeddings
model = HierarchialAttentionNetwork(n_classes=n_classes,
vocab_size=len(word_map),
emb_size=emb_size,
word_rnn_size=word_rnn_size,
sentence_rnn_size=sentence_rnn_size,
word_rnn_layers=word_rnn_layers,
sentence_rnn_layers=sentence_rnn_layers,
word_att_size=word_att_size,
sentence_att_size=sentence_att_size,
dropout=dropout)
model.sentence_attention.word_attention.init_embeddings(embeddings) # initialize embedding layer with pre-trained embeddings
model.sentence_attention.word_attention.fine_tune_embeddings(fine_tune_word_embeddings) # fine-tune
optimizer = optim.Adam(params=filter(lambda p: p.requires_grad, model.parameters()), lr=lr)
criterion = nn.BCEWithLogitsLoss()
# Move to device
model = model.to(device)
criterion = criterion.to(device)
# DataLoaders
train_loader = torch.utils.data.DataLoader(HANDataset(data_folder, 'train'), batch_size=batch_size, shuffle=True,
num_workers=workers, pin_memory=True)
test_loader = torch.utils.data.DataLoader(HANDataset(data_folder, 'test'), batch_size=batch_size, shuffle=False,
num_workers=workers, pin_memory=True)
# Epochs
for epoch in range(start_epoch, epochs):
# One epoch's training
print(epoch)
train(train_loader=train_loader,
model=model,
criterion=criterion,
optimizer=optimizer,
epoch=epoch)
# Decay learning rate every epoch
# adjust_learning_rate(optimizer, 0.1)
# Save checkpoint
save_checkpoint(epoch, model, optimizer, word_map)
print('\n--- EVAL --- \n')
df = []
for threshold in np.linspace(0.1, 0.9, 9):
print('\nthreshold', threshold)
df.append(eval(test_loader, model, threshold))
df = pd.concat(df).reset_index(drop=True)
print(df[df.type == 'multi-label'].to_string())
print(df[df.type == 'relevant'].to_string())
print(df[df.type == 'overall'].to_string())
pd.set_option('display.max_columns', 30)