paranormal-or-skeptic-ISI-p.../main.py

118 lines
3.7 KiB
Python
Raw Permalink Normal View History

2021-05-25 22:27:39 +02:00
from nltk.util import pr
2021-05-25 21:34:03 +02:00
import pandas as pd
import numpy as np
2021-05-25 22:27:39 +02:00
import torch
2021-05-25 22:06:25 +02:00
from gensim import downloader
from nltk.tokenize import word_tokenize
2021-05-26 00:33:17 +02:00
import csv
2021-05-25 21:34:03 +02:00
2021-05-26 04:14:32 +02:00
BATCH_SIZE = 5
2021-05-25 21:34:03 +02:00
2021-05-25 22:27:39 +02:00
class NeuralNetworkModel(torch.nn.Module):
2021-05-25 21:34:03 +02:00
2021-05-25 22:27:39 +02:00
def __init__(self):
2021-05-26 00:33:17 +02:00
dim = 200
2021-05-25 22:27:39 +02:00
super(NeuralNetworkModel, self).__init__()
self.fc1 = torch.nn.Linear(dim, 500)
self.fc2 = torch.nn.Linear(500, 1)
def forward(self, x):
x = self.fc1(x)
x = torch.relu(x)
x = self.fc2(x)
x = torch.sigmoid(x)
return x
def read_data():
x_labels = (pd.read_csv('in-header.tsv', sep='\t')).columns
y_labels = (pd.read_csv('out-header.tsv', sep='\t')).columns
x_train = pd.read_table('train/in.tsv', error_bad_lines=False,
2021-05-26 00:33:17 +02:00
header=None, quoting=csv.QUOTE_NONE, names=x_labels)
2021-05-25 22:27:39 +02:00
y_train = pd.read_table('train/expected.tsv', error_bad_lines=False,
2021-05-26 00:33:17 +02:00
header=None, quoting=csv.QUOTE_NONE, names=y_labels)
2021-05-25 22:27:39 +02:00
x_dev = pd.read_table('dev-0/in.tsv', error_bad_lines=False,
2021-05-26 00:33:17 +02:00
header=None, quoting=csv.QUOTE_NONE, names=x_labels)
2021-05-25 22:27:39 +02:00
x_test = pd.read_table('test-A/in.tsv', error_bad_lines=False,
2021-05-26 00:33:17 +02:00
header=None, quoting=csv.QUOTE_NONE, names=x_labels)
2021-05-25 22:27:39 +02:00
# remove some rows for faster development
remove_n = 200000
drop_indices = np.random.choice(x_train.index, remove_n, replace=False)
x_train = x_train.drop(drop_indices)
2021-05-26 00:33:17 +02:00
y_train = y_train.drop(drop_indices)
2021-05-25 22:27:39 +02:00
return x_labels, y_labels, x_train, y_train, x_dev, x_test
2021-05-26 04:14:32 +02:00
def process_data(x_labels, y_labels, x_train, y_train, x_dev, x_test):
x_train = x_train[x_labels[0]].str.lower()
x_dev = x_dev[x_labels[0]].str.lower()
x_test = x_test[x_labels[0]].str.lower()
y_train = y_train[y_labels[0]]
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
x_train = [word_tokenize(x) for x in x_train]
x_dev = [word_tokenize(x) for x in x_dev]
x_test = [word_tokenize(x) for x in x_test]
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
w2v = downloader.load('glove-wiki-gigaword-200')
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
x_train = [np.mean([w2v[w] for w in d if w in w2v] or [
np.zeros(200)], axis=0) for d in x_train]
x_dev = [np.mean([w2v[w] for w in d if w in w2v]
or [np.zeros(200)], axis=0) for d in x_dev]
x_test = [np.mean([w2v[w] for w in d if w in w2v]
or [np.zeros(200)], axis=0) for d in x_test]
2021-05-25 22:27:39 +02:00
2021-05-26 04:14:32 +02:00
return x_train, y_train, x_dev, x_test
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
def predict(model, x_data, out_path):
y_out = []
model.eval()
with torch.no_grad():
for i in range(0, len(x_data), BATCH_SIZE):
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
x = x_data[i:i+BATCH_SIZE]
x = torch.tensor(x)
pred = nn_model(x.float())
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
y_pred = (pred > 0.5)
y_out.extend(y_pred)
2021-05-25 22:06:25 +02:00
2021-05-26 04:14:32 +02:00
y_data = np.asarray(y_out, dtype=np.int32)
pd.DataFrame(y_data).to_csv(out_path, sep='\t', index=False, header=False)
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
if __name__ == "__main__":
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
x_labels, y_labels, x_train, y_train, x_dev, x_test = read_data()
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
x_train, y_train, x_dev, x_test = process_data(
x_labels, y_labels, x_train, y_train, x_dev, x_test)
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
nn_model = NeuralNetworkModel()
criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(nn_model.parameters(), lr=0.1)
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
for epoch in range(5):
nn_model.train()
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
for i in range(0, y_train.shape[0], BATCH_SIZE):
X = x_train[i:i+BATCH_SIZE]
X = torch.tensor(X)
Y = y_train[i:i+BATCH_SIZE]
Y = torch.tensor(Y.astype(np.float32).to_numpy()).reshape(-1, 1)
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
Y_predictions = nn_model(X.float())
2021-05-26 02:44:21 +02:00
2021-05-26 04:14:32 +02:00
loss = criterion(Y_predictions, Y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
2021-05-26 00:33:17 +02:00
2021-05-26 04:14:32 +02:00
predict(nn_model, x_dev, 'dev-0/out.tsv')
predict(nn_model, x_test, 'test-A/out.tsv')