-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
502 lines (424 loc) · 18.9 KB
/
testing.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import torch
import torch.nn.functional as F
import torch.nn as nn
import torch.optim as optim
import math
from tqdm import tqdm
import time
import numpy as np
import sys
import argparse
import os
import torchmetrics
from torch.utils.data import DataLoader
from torchmetrics.classification import BinaryConfusionMatrix
from sklearn.metrics import classification_report, precision_recall_fscore_support
import pickle
import csv
def decode(vocab,corpus):
text = ''
for i in range(len(corpus)):
wID = corpus[i]
text = text + vocab[wID] + ' '
return(text)
def encode(words,text):
corpus = []
tokens = text.split(' ')
for t in tokens:
try:
wID = words[t][0]
except:
wID = words['<unk>'][0]
corpus.append(wID)
return(corpus)
def read_encode(file_name,vocab,words,corpus,threshold):
wID = len(vocab)
if threshold > -1:
with open(file_name,'rt', encoding='utf-8') as f:
for line in f:
line = line.replace('\n','')
tokens = line.split(' ')
for t in tokens:
try:
elem = words[t]
except:
elem = [wID,0]
vocab.append(t)
wID = wID + 1
elem[1] = elem[1] + 1
words[t] = elem
temp = words
words = {}
vocab = []
wID = 0
words['<unk>'] = [wID,100]
vocab.append('<unk>')
for t in temp:
if temp[t][1] >= threshold:
vocab.append(t)
wID = wID + 1
words[t] = [wID,temp[t][1]]
with open(file_name,'rt', encoding='utf-8') as f:
for line in f:
line = line.replace('\n','')
tokens = line.split(' ')
for t in tokens:
try:
wID = words[t][0]
except:
wID = words['<unk>'][0]
corpus.append(wID)
return [vocab,words,corpus]
class FFNN(nn.Module):
def __init__(self, vocab, words,d_model, d_hidden, window_size, dropout):
super().__init__()
self.vocab = vocab
self.words = words
self.vocab_size = len(self.vocab)
self.d_model = d_model
self.d_hidden = d_hidden
self.dropout = nn.Dropout(p=dropout)
self.embeds = nn.Embedding(self.vocab_size,d_model)
self.hidden = nn.Linear(d_model * window_size, d_hidden)
self.output = nn.Linear(d_hidden, 1)
self.sigmoid = nn.Sigmoid()
self.window_size = window_size
def forward(self, src):
embeds = self.embeds(src)
embedded = self.dropout(embeds)
embedded = embedded.view(-1, self.window_size * self.d_model)
hidden = self.hidden(embedded)
output = self.output(hidden)
output = self.sigmoid(output)
return output
class LSTM(nn.Module):
def __init__(self,vocab,words,d_model,d_hidden,n_layers,dropout_rate):
super().__init__()
self.vocab = vocab
self.words = words
self.vocab_size = len(self.vocab)
self.n_layers = n_layers
self.d_hidden = d_hidden
self.d_model = d_model
self.embeds = nn.Embedding(self.vocab_size,d_model)
self.lstm = nn.LSTM(d_model, d_hidden, num_layers=n_layers,
dropout=dropout_rate, batch_first=True)
self.dropout = nn.Dropout(dropout_rate)
self.logprobs = nn.Linear(d_hidden, self.vocab_size)
assert d_model == d_hidden, 'cannot tie, check dims'
self.embeds.weight = self.logprobs.weight
def forward(self,src,h):
embeds = self.embeds(src)
out, h = self.lstm(self.dropout(embeds), h)
preds = self.logprobs(out)
return [preds,h]
def init_weights(self):
pass
def init_hidden(self, batch_size, device):
hidden = torch.zeros(self.n_layers, batch_size, self.d_hidden).to(device)
cell = torch.zeros(self.n_layers, batch_size, self.d_hidden).to(device)
return hidden, cell
def detach_hidden(self, hidden):
hidden, cell = hidden
hidden = hidden.detach()
cell = cell.detach()
return [hidden, cell]
def main():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# device = torch.device("cpu")
print("Device available for running: ")
print(device)
# for testing
d_model = 100
d_hidden = 100
model = 'FFNN'
epochs = 10
batch_size = 32
window = 10
lr = 0.0001
dropout = 0.2
n_layers = 2
savename = 'ffnnModel'
loadname = 'ffnnModel'
trainname = 'mix.train.txt'
validname = 'mix.valid.txt'
testname = 'mix.test.txt'
torch.manual_seed(0)
# ------------------ read data and encode ------------------
# vocab: list of words
# words: dictionary of words and their ids and counts
# train: the corpus in terms of word ids
[vocab,words,train] = read_encode('/content/' + trainname,[],{},[],3)
print('vocab: %d train: %d' % (len(vocab),len(train)))
[vocab,words,test] = read_encode('/content/' + testname,vocab,words,[],-1)
print('vocab: %d test: %d' % (len(vocab),len(test)))
vocab_size = len(vocab)
[vocab,words,valid] = read_encode('/content/' + validname,vocab,words,[],-1)
print('vocab: %d test: %d' % (len(vocab),len(valid)))
FAKE_TOKEN = "[FAKE]"
REAL_TOKEN = "[REAL]"
END_TOKEN = "<end_bio>"
# x: list of b io texts
# y: [REAL] (1) or [FAKE] (0)
def FFNN_getInputAndTarget(train):
input = []
temp = []
for token in train:
temp.append(token)
if token == words[FAKE_TOKEN][0] or token == words[REAL_TOKEN][0]:
input.append(temp)
temp = []
for i in range(len(input)):
if input[i][-1] == words[FAKE_TOKEN][0]:
input[i] = [input[i][:-1], 0]
else:
input[i] = [input[i][:-1], 1]
x = [i[0] for i in input]
y = [i[1] for i in input]
return x, y
def FFNN_getInput(train):
input = []
temp = []
for token in train:
temp.append(token)
if token == words[END_TOKEN][0]:
input.append(temp)
temp = []
return input
# split the input data with given target into windows of window_size
def FFNN_split_windows(window_size, inputs, target):
windows = []
labels = []
for j in range(len(inputs)):
data = inputs[j]
for i in range(len(data) - window_size + 1):
window = data[i:i + window_size]
windows.append(window)
label = target[j] # Assign binary label based on task
labels.append(label)
return windows, labels
# split the data into windows of window_size
def FFNN_split_window(window_size, data):
windows = []
for i in range(len(data) - window_size + 1):
window = data[i:i + window_size]
windows.append(window)
return windows
def LSTM_split_sequences(data, sequence_length, step):
sequences = []
for i in range(0, len(data) - sequence_length + step, step):
sequence = data[i:i + sequence_length]
if len(sequence) < sequence_length:
sequence = np.concatenate([sequence, np.zeros((sequence_length - len(sequence),))])
sequences.append(torch.Tensor(sequence))
return sequences
def LSTM_train(model, dataloader, optimizer, criterion, batch_size, seq, device):
training_loss = 0
model.train()
h = model.init_hidden(batch_size, device)
for idx, batch in enumerate(tqdm(dataloader, total=len(dataloader), position=0, leave=True)):
# if (idx+1) % 1000 == 0:
# print("Batch completed: " + str(idx+1))
optimizer.zero_grad()
h = model.detach_hidden(h)
input, labels = batch[:, :-1], batch[:, 1:]
input, labels = input.to(device).to(torch.long), labels.to(device).to(torch.long)
preds, h = model(input, h)
preds = preds.reshape(batch_size * seq, -1)
labels = labels.reshape(-1)
loss = criterion(preds, labels)
loss.backward()
optimizer.step()
training_loss += loss.item()
return training_loss / len(dataloader)
def LSTM_evaluate(model, dataload, criterion, batch_size, seq, device):
epoch_loss = 0
model.eval()
h = model.init_hidden(batch_size, device)
with torch.no_grad():
for idx, data in enumerate(tqdm(dataload, total=len(dataload), position=0, leave=True)):
h = model.detach_hidden(h)
src, labels = data[:,:-1], data[:,1:]
src, labels = src.to(device).to(torch.long), labels.to(device).to(torch.long)
preds, h = model(src, h)
preds = preds.reshape(batch_size * seq, -1)
labels = labels.reshape(-1)
loss = criterion(preds, labels)
epoch_loss += loss.item()
return epoch_loss / len(dataload)
def LSTM_generate(data):
model.eval()
hidden = model.init_hidden(1, device)
predict = []
for i, seq in enumerate(tqdm(data, total=len(data))):
with torch.no_grad():
src = torch.Tensor(seq).to(device).to(torch.long).unsqueeze(dim=0)
prediction, hidden = model(src, hidden)
if words[END_TOKEN][0] in seq:
probs = torch.softmax(prediction[:, seq.tolist().index(words[END_TOKEN][0])], dim=-1).squeeze()
if probs[words[FAKE_TOKEN][0]] > probs[words[REAL_TOKEN][0]]:
predict.append(0)
else:
predict.append(1)
return predict
def LSTM_preprocess(train):
input = []
temp = []
for token in train:
temp.append(token)
if token == words[FAKE_TOKEN][0] or token == words[REAL_TOKEN][0]:
input.append(temp)
temp = []
for i in range(len(input)):
if input[i][-1] == words[FAKE_TOKEN][0]:
input[i] = [input[i][3:len(input[i])-1], 0]
else:
input[i] = [input[i][3:len(input[i])-1], 1]
train_input = [i[0] for i in input]
train_target = [i[1] for i in input]
return train_input, train_target
if model == 'FFNN':
# {add code to instantiate the model, train for K epochs and save model to disk}
print("setup Training FFNN model...")
# get x_train, y_train, x_valid, y_valid, x_test, y_test
# split bios into windows of window_size with label of [REAL] or [FAKE]
train_input, train_target = FFNN_getInputAndTarget(train)
valid_input, valid_target = FFNN_getInputAndTarget(valid)
train_input = [torch.Tensor(i) for i in train_input]
valid_input = [torch.Tensor(i) for i in valid_input]
train_windows, train_labels = FFNN_split_windows(window, train_input, train_target)
valid_windows, valid_labels = FFNN_split_windows(window, valid_input, valid_target)
# initialize model and hyperparameters
model = FFNN(vocab, words, d_model, d_hidden, window, dropout)
model.to(device)
model.train()
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=lr)
# Create a DataLoader for parallel batch training
train_dataset = [(train_windows[i], train_labels[i]) for i in range(len(train_windows))]
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=2)
valid_dataset = [(valid_windows[i], valid_labels[i]) for i in range(len(valid_windows))]
valid_loader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=True, num_workers=2)
losses = []
valid_losses = []
# lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.5, patience=0)
print("begin Training...")
# train + validation for k epochs
for k in range(epochs):
print("epoch: ", k+1)
train_loss = 0
valid_loss = 0
for i, (batch_windows, batch_labels) in enumerate(tqdm(train_loader, total=len(train_loader))):
optimizer.zero_grad()
batch_outputs = model(batch_windows.to(torch.int64).to(device))
batch_labels = batch_labels.to(device).to(torch.float).unsqueeze(1)
loss = criterion(batch_outputs, batch_labels)
# L2 regularization
# _lambda = 0.0001
# l2_reg = torch.tensor(0.)
# for param in model.parameters():
# l2_reg += torch.norm(param)**2
# loss += 0.5 * _lambda * l2_reg
loss.backward()
train_loss += loss.item()
optimizer.step()
with torch.no_grad():
for i, (batch_windows, batch_labels) in enumerate(tqdm(valid_loader, total=len(valid_loader))):
inputs = batch_windows.to(torch.int64).to(device)
labels = batch_labels.to(device).to(torch.float).unsqueeze(1)
outputs = model(inputs)
loss = criterion(outputs, labels)
valid_loss += loss.item()
inputs = torch.Tensor(torch.stack(valid_windows)).to(torch.int64).to(device)
outputs = model(inputs)
# validation = criterion(outputs, torch.Tensor(valid_labels).to(device).to(torch.float).unsqueeze(1)).tolist()
# valid_losses.append(validation)
lr_scheduler.step(valid_loss)
losses.append(train_loss/len(train_loader))
valid_losses.append(valid_loss/len(valid_loader))
print("average training loss: ", train_loss/len(train_loader))
print("average validation loss: ", valid_loss/len(valid_loader))
# save data to disk
torch.save(model, './' + savename + '.pt')
if model == 'LSTM':
# {add code to instantiate the model, train for K epochs and save model to disk}
print("Setup Training LSTM model...")
train_sq = LSTM_split_sequences(train, window + 1, window + 1)
valid_sq = LSTM_split_sequences(valid, window + 1, window + 1)
dataloader = DataLoader(train_sq, batch_size=batch_size, num_workers = 2,shuffle=True, drop_last=True)
valid_loader = DataLoader(valid_sq, batch_size=batch_size, num_workers = 2,shuffle=True, drop_last=True)
model = LSTM(vocab, words, d_model, d_hidden, n_layers, dropout).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
criterion = nn.CrossEntropyLoss()
lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.5, patience=0)
train_losses = []
valid_losses = []
print("begin Training...")
for epoch in range(epochs):
print('epoch: ', epoch + 1)
train_loss = LSTM_train(model, dataloader, optimizer, criterion,
batch_size, window, device)
valid_loss = LSTM_evaluate(model, valid_loader, criterion, batch_size,
window, device)
lr_scheduler.step(valid_loss)
train_losses.append(train_loss)
valid_losses.append(valid_loss)
torch.save(model, '/content/'+ savename + '.pt')
if model == 'FFNN_CLASSIFY':
# {add code to instantiate the model, recall model parameters and perform/learn classification}
print("Testing FFNN model...")
model = torch.load('/content/'+ loadname + '.pt')
test_input, test_target = FFNN_getInputAndTarget(test)
test_input = [torch.Tensor(i) for i in test_input]
model.eval()
predictions = []
print("begin testing...")
for bio in test_input:
windows = FFNN_split_window(window, bio) # split each testing bio into windows
if len(bio) < window:
predictions.append(torch.mode(torch.zeros(1).to(torch.int64).to(device)).values)
else:
with torch.no_grad():
inputs = torch.Tensor(torch.stack(windows)).to(torch.int64).to(device)
outputs = model(inputs) # inference the windows
predictions.append(torch.mode(torch.round(outputs).squeeze(1)).values) # append the mode of the inference as the prediction of the bio
confmat = BinaryConfusionMatrix().to(device)
print(confmat(torch.stack(predictions), torch.Tensor(test_target).to(device)))
if model == 'LSTM_CLASSIFY':
# {add code to instantiate the model, recall model parameters and perform/learn classification}
print("Testing LSTM model...")
test_sq = LSTM_split_sequences(test, window, window)
test_sq_loss = LSTM_split_sequences(test, window + 1, window + 1)
model = torch.load('/content/'+ loadname + '.pt')
print("begin testing...")
predictions = LSTM_generate(test_sq)
test_input, test_targets = LSTM_preprocess(test)
confmat = BinaryConfusionMatrix().to(device)
print(confmat(torch.Tensor(predictions).to(device),torch.Tensor(test_targets).to(device)))
print(classification_report(test_targets,predictions))
if model == 'BLIND':
# {add code to instantiate the model, recall model parameters and perform/learn classification}
print("Testing blind set...")
model = torch.load('./FFNN_model.pt')
[vocab,words,blind] = read_encode("/content/blind.test.txt",vocab,words,[],-1)
test_input = FFNN_getInput(blind)
test_input = [torch.Tensor(i) for i in test_input]
model.eval()
predictions = []
for bio in test_input:
windows = FFNN_split_window(window, bio) # split each testing bio into windows
if len(bio) < window:
predictions.append(torch.mode(torch.zeros(1).to(torch.int64).to(device)).values)
else:
with torch.no_grad():
inputs = torch.Tensor(torch.stack(windows)).to(torch.int64).to(device)
outputs = model(inputs) # inference the windows
predictions.append(torch.mode(torch.round(outputs).squeeze(1)).values) # append the mode of the inference as the prediction of the bio
predictions = [i.to(torch.int64).tolist() for i in predictions]
with open('predictions.csv', mode='w') as file:
writer = csv.writer(file)
writer.writerow(['id', 'prediction'])
for i, pred in enumerate(predictions):
writer.writerow([i, pred.item()])
if __name__ == "__main__":
main()