import numpy as np import torch from gensim import downloader import gensim class NeuralNetworkModel(torch.nn.Module): def __init__(self): super(NeuralNetworkModel, self).__init__() self.fc1 = torch.nn.Linear(100, 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 BATCH_SIZE = 5 PATH = "new_model_full.pt" model = torch.load(PATH) model.eval() glove_vectors = downloader.load("glove-wiki-gigaword-100") with open(r"C:\Users\Kacper Dudzic\Desktop\paranormal-or-skeptic\dev-0\in.tsv", "r", encoding="utf-8") as dev_in, \ open(r"C:\Users\Kacper Dudzic\Desktop\paranormal-or-skeptic\test-A\in.tsv", "r", encoding="utf-8") as test_in: X_dev = [line for line in dev_in.readlines()] X_dev = [np.mean([glove_vectors[tk] for tk in gensim.utils.tokenize(text, lowercase=True) if tk in glove_vectors] or [np.zeros(100)], axis=0) for text in X_dev] X_test = [line for line in test_in.readlines()] X_test = [np.mean([glove_vectors[tk] for tk in gensim.utils.tokenize(text, lowercase=True) if tk in glove_vectors] or [np.zeros(100)], axis=0) for text in X_test] with open('dev_out.tsv', 'w', encoding='utf-8') as dev_out, open('test_out.tsv', 'w', encoding='utf-8') as test_out: dev_predictions = [] test_predictions = [] for i in range(0, len(X_dev), BATCH_SIZE): X = X_dev[i:i + BATCH_SIZE] X = torch.tensor(np.array(X).astype(np.float32)) Y_predictions = (model(X) > 0.5) dev_predictions.extend(Y_predictions) for i in range(0, len(X_test), BATCH_SIZE): X = X_test[i:i + BATCH_SIZE] X = torch.tensor(np.array(X).astype(np.float32)) Y_predictions = (model(X) > 0.5) test_predictions.extend(Y_predictions) for pred in dev_predictions: dev_out.write(str(pred.int()[0].item()) + '\n') for pred in test_predictions: test_out.write(str(pred.int()[0].item()) + '\n')