Fix dimensions to fit w2v

This commit is contained in:
Maciej Sobkowiak 2021-05-26 00:33:17 +02:00
parent 1b3c4dd9ef
commit 892f21fc34

30
main.py
View File

@ -4,12 +4,13 @@ import numpy as np
import torch import torch
from gensim import downloader from gensim import downloader
from nltk.tokenize import word_tokenize from nltk.tokenize import word_tokenize
import csv
class NeuralNetworkModel(torch.nn.Module): class NeuralNetworkModel(torch.nn.Module):
def __init__(self): def __init__(self):
dim = 100 dim = 200
super(NeuralNetworkModel, self).__init__() super(NeuralNetworkModel, self).__init__()
self.fc1 = torch.nn.Linear(dim, 500) self.fc1 = torch.nn.Linear(dim, 500)
self.fc2 = torch.nn.Linear(500, 1) self.fc2 = torch.nn.Linear(500, 1)
@ -27,24 +28,28 @@ def read_data():
y_labels = (pd.read_csv('out-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, x_train = pd.read_table('train/in.tsv', error_bad_lines=False,
header=None, quoting=3, names=x_labels) header=None, quoting=csv.QUOTE_NONE, names=x_labels)
y_train = pd.read_table('train/expected.tsv', error_bad_lines=False, y_train = pd.read_table('train/expected.tsv', error_bad_lines=False,
header=None, quoting=3, names=y_labels) header=None, quoting=csv.QUOTE_NONE, names=y_labels)
x_dev = pd.read_table('dev-0/in.tsv', error_bad_lines=False, x_dev = pd.read_table('dev-0/in.tsv', error_bad_lines=False,
header=None, quoting=3, names=x_labels) header=None, quoting=csv.QUOTE_NONE, names=x_labels)
x_test = pd.read_table('test-A/in.tsv', error_bad_lines=False, x_test = pd.read_table('test-A/in.tsv', error_bad_lines=False,
header=None, quoting=3, names=x_labels) header=None, quoting=csv.QUOTE_NONE, names=x_labels)
# remove some rows for faster development # remove some rows for faster development
remove_n = 200000 remove_n = 200000
drop_indices = np.random.choice(x_train.index, remove_n, replace=False) drop_indices = np.random.choice(x_train.index, remove_n, replace=False)
x_train = x_train.drop(drop_indices) x_train = x_train.drop(drop_indices)
y_train = y_train.drop(drop_indices)
return x_labels, y_labels, x_train, y_train, x_dev, x_test return x_labels, y_labels, x_train, y_train, x_dev, x_test
x_labels, y_labels, x_train, y_train, x_dev, x_test = read_data() x_labels, y_labels, x_train, y_train, x_dev, x_test = read_data()
print(len(y_train))
print(len(x_train))
x_train = x_train[x_labels[0]].str.lower() x_train = x_train[x_labels[0]].str.lower()
x_dev = x_dev[x_labels[0]].str.lower() x_dev = x_dev[x_labels[0]].str.lower()
x_test = x_test[x_labels[0]].str.lower() x_test = x_test[x_labels[0]].str.lower()
@ -57,11 +62,11 @@ x_test = [word_tokenize(x) for x in x_test]
w2v = downloader.load('glove-wiki-gigaword-200') w2v = downloader.load('glove-wiki-gigaword-200')
x_train = [np.mean([w2v[word] for word in doc if word in w2v] or [ x_train = [np.mean([w2v[word] for word in doc if word in w2v] or [
np.zeros(50)], axis=0) for doc in x_train] np.zeros(200)], axis=0) for doc in x_train]
x_dev = [np.mean([w2v[word] for word in doc if word in w2v] x_dev = [np.mean([w2v[word] for word in doc if word in w2v]
or [np.zeros(50)], axis=0) for doc in x_dev] or [np.zeros(200)], axis=0) for doc in x_dev]
x_test = [np.mean([w2v[word] for word in doc if word in w2v] x_test = [np.mean([w2v[word] for word in doc if word in w2v]
or [np.zeros(50)], axis=0) for doc in x_test] or [np.zeros(200)], axis=0) for doc in x_test]
nn_model = NeuralNetworkModel() nn_model = NeuralNetworkModel()
BATCH_SIZE = 5 BATCH_SIZE = 5
@ -72,15 +77,16 @@ for epoch in range(5):
nn_model.train() nn_model.train()
for i in range(0, y_train.shape[0], BATCH_SIZE): for i in range(0, y_train.shape[0], BATCH_SIZE):
X = x_train[i:i+BATCH_SIZE] X = x_train[i:i+BATCH_SIZE]
X = torch.tensor(X.astype(np.float32).todense()) X = torch.tensor(X)
Y = y_train[i:i+BATCH_SIZE] Y = y_train[i:i+BATCH_SIZE]
Y = torch.tensor(Y.astype(np.float32)).reshape(-1, 1) Y = torch.tensor(Y.astype(np.float32).to_numpy()).reshape(-1, 1)
Y_predictions = nn_model(X) Y_predictions = nn_model(X.float())
optimizer.zero_grad()
loss = criterion(Y_predictions, Y) loss = criterion(Y_predictions, Y)
optimizer.zero_grad()
loss.backward() loss.backward()
optimizer.step() optimizer.step()
print(Y_predictions) print(Y_predictions)