-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_lstm_model.py
420 lines (354 loc) · 13.2 KB
/
4_lstm_model.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#%% This script preprocesses the wikipedia data and trains the LSTM Network.
# The LSTM Network utility functions can be found in LSTM.py
from LSTM import LSTMN, WikiDocData, split_data, train_lstmn, batcher, process_batch
import pickle
import numpy as np
import os
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
from torch import optim
import torch.nn as nn
from sklearn import metrics
# Import namespace
from argparse import Namespace
from keras.preprocessing.text import Tokenizer # Use keras for tokenization & preprocessing
import itertools
# Helper functions
from preprocess_utils import tokenize_text
# Base directory
bd = "data/HAN"
# Create a map for the different sentence lengths
slmap = {}
for sl in ["S8", "S10", "S12", "S15"]:
idx=int(sl.strip("S"))
slmap[idx] = {"tokenizer": os.path.join(bd, "tokenizer_{}.pickle".format(sl)),
"embedding": os.path.join(bd, "HAN_embeddings_{}.pickle".format(sl)),
"input_data": os.path.join(bd, "data_{}.pickle".format(sl))}
# Model settings
args = Namespace(
# Tokenizer
data_files_map=slmap,
# File to save results
out_file='results/LSTM_trials.csv',
# Number of times to evaluate bayesian search for hyperparams
# NB: the HAN is very expensive to run even on a GPU ##########################
max_evals=300,
# Embedding size
embedding_dim=300,
# NN settings
embedding_trainable=False,
# Batch size
batch_size=128
)
#%% Load the data for one of the sentence lengths
sent_length = 15
# Load
with open(args.data_files_map[sent_length]["tokenizer"], "rb") as inFile:
tokenizer = pickle.load(inFile)
with open(args.data_files_map[sent_length]["embedding"], "rb") as inFile:
FTEMB = torch.tensor(pickle.load(inFile)).to(device)
with open(args.data_files_map[sent_length]["input_data"], "rb") as inFile:
data = pickle.load(inFile)
# Unpack
train_x, train_y = data["train_x"], data["train_y"]
docs_vectorized_lstm = []
doc_lstm = []
for doc in train_x:
doc_lstm = []
for sent in doc:
doc_lstm = doc_lstm + sent
docs_vectorized_lstm.append(doc_lstm)
train_x = docs_vectorized_lstm
labels_vect = data["labels_vectorized"]
idx_to_label = data["idx_to_label"]
label_to_idx = data["labels_to_idx"]
#%% View the max length of all sentences in all documents
# Max length of the documents
max_seq_doc = max([len(doc) for doc in train_x])
# View
print((max_seq_doc))
#%% Class weights (these are the same for each)
# Class weights
# Preprocess outcome label
train_y_ohe = np.zeros((len(train_y), len(data["labels_to_idx"])))
for idx,lbl in enumerate(train_y):
train_y_ohe[idx, lbl] = 1
# These weights are unnormalized but that's what pytorch is expecting
cw = torch.tensor(np.max(np.sum(train_y_ohe, axis=0)) / (np.sum(train_y_ohe, axis=0))).type(torch.float).to(device)
#%% Unique classes
num_classes = len(np.unique(labels_vect))
#%% Use hyperopt (Bayesian hyperparameter optimization) to search for good hyperparams
from hyperopt import STATUS_OK
import csv
from hyperopt import hp
# Optimizer
from hyperopt import tpe
# Save basic training information
from hyperopt import Trials
# Optimizer criterion
from hyperopt import fmin
# Use skorch for cross-validation
from skorch import NeuralNet
from skorch.dataset import CVSplit
# Function that sets up model and outputs and returns validation loss
def LSTMN_search(parameters):
"""Set up, run and evaluate a LSTMN"""
# Based on the parameters, load various settings
sent_length = parameters["sent_length"]
# Load data
with open(args.data_files_map[sent_length]["tokenizer"], "rb") as inFile:
tokenizer = pickle.load(inFile)
with open(args.data_files_map[sent_length]["embedding"], "rb") as inFile:
FTEMB = torch.tensor(pickle.load(inFile)).to(device)
with open(args.data_files_map[sent_length]["input_data"], "rb") as inFile:
data = pickle.load(inFile)
# Unpack
train_x, train_y = data["train_x"], data["train_y"]
docs_vectorized_lstm = []
doc_lstm = []
for doc in train_x:
doc_lstm = []
for sent in doc:
doc_lstm = doc_lstm + sent
docs_vectorized_lstm.append(doc_lstm)
train_x = docs_vectorized_lstm
labels_vect = data["labels_vectorized"]
idx_to_label = data["idx_to_label"]
label_to_idx = data["labels_to_idx"]
train, val = split_data(train_x, train_y, 6754, p=0.05)
train_x = train[0]
train_y = train[1]
# Set up the model
WikiLSTM = LSTMN(FTEMB,
args.batch_size,
num_classes,
bidirectional = parameters["bidirectional"],
nb_lstm_layers = parameters["nb_lstm_layers"],
nb_lstm_units = parameters["nb_lstm_units"],
dropout_prop=parameters["dropout_prop"])
# To cuda
WikiLSTM.to(device)
# Set up optimizer
optimizer = optim.Adam(WikiLSTM.parameters(), lr=parameters["learning_rate"])
# Criterion
if parameters["use_class_weights"]:
criterion = nn.CrossEntropyLoss(weight=cw)
else:
criterion = nn.CrossEntropyLoss()
# Run the model
WikiLSTM_out, history = train_lstmn(train_x, train_y, WikiLSTM, optimizer, criterion,
epochs=10, val_split=0.1, batch_size=args.batch_size,
device=device)
# Max accuracy
which_min = int(np.argmin(history["validation_loss"]))
# Write to file
with open(args.out_file, 'a') as of_connection:
writer = csv.writer(of_connection)
writer.writerow([parameters,
which_min,
np.round(history["training_loss"][which_min], 4),
np.round(history["validation_accuracy"][which_min], 4),
np.round(history["validation_loss"][which_min], 4),
np.round(history["validation_f1"][which_min], 4),
np.round(history["validation_precision"][which_min], 4),
np.round(history["validation_recall"][which_min], 4)])
# Return cross-validation loss
# NB: we are minimizing here zo we need to take 1-accuracy
return({"loss": history["validation_loss"][which_min], "parameters": parameters, "iteration": which_min, 'status':STATUS_OK})
# Define the search space
space = {
'nb_lstm_units': hp.choice('nb_lstm_units', [32,64,128]),
'nb_lstm_layers': hp.choice('nb_lstm_layers', [1,2]),
'bidirectional': hp.choice('bidirectional', [False, True]),
'sent_length': hp.choice("sent_length", [8, 10, 12, 15]),
'use_class_weights': hp.choice("use_class_weights", [True, False]),
'learning_rate': hp.loguniform('learning_rate', np.log(0.001), np.log(0.03)),
'dropout_prop': hp.uniform("dropout", 0, 0.5)
}
#%% Test space
# Test if works
from hyperopt.pyll.stochastic import sample
parameters = sample(space)
print(parameters)
po = LSTMN_search(parameters)
#%% Run the optimizer
# Algorithm
tpe_algorithm = tpe.suggest
# Trials object to track progress
bayes_trials = Trials()
# File to save first results
with open(args.out_file, 'w') as of_connection:
writer = csv.writer(of_connection)
# Write the headers to the file
writer.writerow(['params',
'iteration',
'train_loss',
'val_accuracy',
'val_loss',
"val_f1",
"val_precision",
"val_recall"])
# Optimize
import datetime
begin = datetime.datetime.now()
best = fmin(fn = LSTMN_search, space = space, algo = tpe.suggest,
max_evals = args.max_evals, trials = bayes_trials)
finish = datetime.datetime.now()
t_spent = begin-finish
print("began at: " + begin.strftime("%Y-%m-%d %H:%M"))
print("ended at: " + finish.strftime("%Y-%m-%d %H:%M"))
print("total time: " + str(t_spent))
#%% Train LSTMN on best parameters and train data
max_sent_length = 8
# Load data for the sentence length
sent_length = max_sent_length
with open(args.data_files_map[sent_length]["tokenizer"], "rb") as inFile:
tokenizer = pickle.load(inFile)
with open(args.data_files_map[sent_length]["embedding"], "rb") as inFile:
FTEMB = torch.tensor(pickle.load(inFile)).to(device)
with open(args.data_files_map[sent_length]["input_data"], "rb") as inFile:
data = pickle.load(inFile)
# Unpack
train_x, train_y = data["train_x"], data["train_y"]
docs_vectorized_lstm = []
doc_lstm = []
for doc in train_x:
doc_lstm = []
for sent in doc:
doc_lstm = doc_lstm + sent
docs_vectorized_lstm.append(doc_lstm)
train_x = docs_vectorized_lstm
labels_vect = data["labels_vectorized"]
idx_to_label = data["idx_to_label"]
label_to_idx = data["labels_to_idx"]
#%%
best = { ###########################################################################################################
"bidirectional" : True,
"dropout_prop" : 0.010131,
"learning_rate" : 0.020356,
"nb_lstm_units" : 32,
"nb_lstm_layers" : 1,
"use_class_weights" : True,
"batch_size" : 128,
"num_classes" : len(np.unique(labels_vect)),
"epochs" : 9
}
# Set up the model
WikiLSTM = LSTMN(FTEMB, best["batch_size"], best["num_classes"],
best["bidirectional"], best["nb_lstm_layers"],
best["nb_lstm_units"], best["dropout_prop"])
# To cuda
WikiLSTM.to(device)
# Set up optimizer
optimizer = optim.Adam(WikiLSTM.parameters(), lr= best["learning_rate"])
# Criterion
if best["use_class_weights"]:
criterion = nn.CrossEntropyLoss(weight=cw)
else:
criterion = nn.CrossEntropyLoss()
# Training routine
WikiLSTM_out, history = train_lstmn(train_x, train_y, WikiLSTM, optimizer, criterion,
epochs = best["epochs"], val_split = 0.1, batch_size = best["batch_size"],
device = device)
#%% Save model
torch.save(WikiLSTM_out.state_dict(), "models/LSTM.pt")
#%% Get test data
test_x, test_y = data["test_x"], data["test_y"]
docs_vectorized_lstm = []
doc_lstm = []
for doc in test_x:
doc_lstm = []
for sent in doc:
doc_lstm = doc_lstm + sent
docs_vectorized_lstm.append(doc_lstm)
test_x = docs_vectorized_lstm
#%% WikiDocData
test = WikiDocData(test_x, test_y)
train = WikiDocData(train_x, train_y)
#%% Evaluate the model on test data
# For now, just make a single batch of the test data for evaluation
valbatch = batcher(test, len(test.X))
# Preprocess
seqs, lens = process_batch(valbatch, device = device)
# Predict
with torch.no_grad():
WikiLSTM_out.eval()
probs = WikiLSTM_out(seqs, lens, batch_size = 1001)
#%% Same for train data
def predict_LSTM(model, dataset, batch_size = 128, device = "cpu"):
"""
Create predictions for a HAN
:param model: LSTM model
:param dataset: WikiDocData dataset
:param batch_size: size of the input batches to the model
:param device: device on which the model is run
:return: tuple containing predictions and ground truth labels
"""
n = len(dataset.X)
total = n // batch_size
remainder = n % batch_size
# Make indices
idx = []
start_idx = 0
for batch_idx in range(1, total+1):
idx.append((start_idx, batch_idx * batch_size))
start_idx += batch_size
# If remainder
if remainder > 0:
idx.append((start_idx, start_idx + remainder))
# For each pair, predict
predictions = []
ground_truth = []
for start_idx, stop_idx in idx:
# Get batch
inbatch = [dataset.__getitem__(idx) for idx in range(start_idx, stop_idx)]
# Process batch
seqs, lens = process_batch(inbatch, device = device)
# Batch size
bs = len(seqs)
# Predict
with torch.no_grad():
model.eval()
probs = model(seqs, lens, batch_size = bs)
# To classes
out = torch.argmax(probs, dim=1).cpu().numpy()
# Get true label
ytrue = [batch[1] for batch in inbatch]
ytrue = torch.tensor(ytrue).cpu().numpy()
# Cat together
predictions.append(out)
ground_truth.append(ytrue)
# Stack predictions & ground truth
return(np.hstack(predictions), np.hstack(ground_truth))
#%% On Train
# Predict train
yhat, ytrue = predict_LSTM(WikiLSTM_out, train, device = device)
# Print classification report
print(metrics.classification_report(ytrue, yhat, target_names = list(label_to_idx.keys())))
#%% On test
# Predict test
yhat, ytrue = predict_LSTM(WikiLSTM_out, test, device = device)
# Print classification report
print(metrics.classification_report(ytrue, yhat, target_names = list(label_to_idx.keys())))
#%% Save to csv
import pandas as pd
out_preds = pd.DataFrame({"yhat": yhat, "ytrue":ytrue})
out_preds.to_csv("predictions/LSTM.csv", index=False)
#%%
# Predict
with torch.no_grad():
WikiLSTM_out.eval()
probs = WikiLSTM_out(seqs, lens, batch_size = len(batche[0].X))
# %% Classes
# To classes
out = torch.argmax(probs, dim=1).cpu().numpy()
# Get true label
ytrue = [batch[1] for batch in valbatch]
ytrue = torch.tensor(ytrue).cpu().numpy()
# Accuracy
print(sum(out == ytrue)/len(out))
#%% Print classification report
# Print classification report
print(metrics.classification_report(ytrue, yhat, target_names = list(label_to_idx.keys())))
#%% Save model
torch.save(WikiLSTM_out.state_dict(), "models/LSTM.pt")