paranormal-or-skeptic-ISI-s.../LogReg_Test.ipynb
2021-05-22 17:07:56 +02:00

6.2 KiB

import pandas as pd
import numpy as np
import torch
import csv
from nltk.tokenize import word_tokenize
#from gensim.models import Word2Vec
import gensim.downloader as api
#Sieć neuronowa z ćwiczeń 8
class NeuralNetwork(torch.nn.Module): 
    def __init__(self, hidden_size):
        super(NeuralNetwork, self).__init__()
        self.l1 = torch.nn.Linear(300, hidden_size) #Korzystamy z Googlowego word2vec-google-news-300 który ma zawsze na wejściu wymiar 300
        self.l2 = torch.nn.Linear(hidden_size, 1)

    def forward(self, x):
        x = self.l1(x)
        x = torch.relu(x)
        x = self.l2(x)
        x = torch.sigmoid(x)
        return x
# Wczytanie X i Y do Train oraz X do Dev i Test
X_train = pd.read_table('train/in.tsv', sep='\t', error_bad_lines=False, quoting=3, header=None, names=['content', 'id'], usecols=['content'])
y_train = pd.read_table('train/expected.tsv', sep='\t', error_bad_lines=False, quoting=3, header=None, names=['label'])
X_dev = pd.read_table('dev-0/in.tsv', sep='\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])
X_test = pd.read_table('test-A/in.tsv', sep='\t', error_bad_lines=False, header=None, quoting=3, names=['content', 'id'], usecols=['content'])
# Preprocessing danych
# lowercase
# https://www.datacamp.com/community/tutorials/case-conversion-python
X_train = X_train.content.str.lower()
y_train = y_train['label']
X_dev = X_dev.content.str.lower()
X_test = X_test.content.str.lower()
# tokenize
X_train = [word_tokenize(content) for content in X_train]
X_dev = [word_tokenize(content) for content in X_dev]
X_test = [word_tokenize(content) for content in X_test]
# word2vec zgodnie z poradą Pana Jakuba
# https://radimrehurek.com/gensim/auto_examples/howtos/run_downloader_api.html
# https://www.kaggle.com/kstathou/word-embeddings-logistic-regression
w2v = api.load('word2vec-google-news-300')
X_train = [np.mean([w2v[w] for w in content if w in w2v] or [np.zeros(300)], axis=0) for content in X_train]
X_dev = [np.mean([w2v[w] for w in content if w in w2v] or [np.zeros(300)], axis=0) for content in X_dev]
X_test = [np.mean([w2v[w] for w in content if w in w2v] or [np.zeros(300)], axis=0) for content in X_test]
model = NeuralNetwork(600)

criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)

batch_size = 15
# Trening modelu z ćwiczeń 8
for epoch in range(5):
    model.train()
    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)

        outputs = model(X.float())
        loss = criterion(outputs, y)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
y_dev = []
y_test = []

#model.eval() will notify all your layers that you are in eval mode
model.eval()

#torch.no_grad() impacts the autograd engine and deactivate it. It will reduce memory usage and speed up
with torch.no_grad():
    for i in range(0, len(X_dev), batch_size):
        X = X_dev[i:i+batch_size]
        X = torch.tensor(X)
        
        outputs = model(X.float())
        
        y = (outputs > 0.5)
        y_dev.extend(y)

    for i in range(0, len(X_test), batch_size):
        X = X_test[i:i+batch_size]
        X = torch.tensor(X)

        outputs = model(X.float())

        y = (outputs > 0.5)
        y_test.extend(y)
y_dev = np.asarray(y_dev, dtype=np.int32)
y_test = np.asarray(y_test, dtype=np.int32)

y_dev_df = pd.DataFrame({'label':y_dev})
y_test_df = pd.DataFrame({'label':y_test})

y_dev_df.to_csv(r'dev-0/out.tsv', sep='\t', index=False,  header=False)
y_test_df.to_csv(r'test-A/out.tsv', sep='\t', index=False,  header=False)