duma_ex_10/logistic-regression.py
2021-05-27 10:31:00 +02:00

113 lines
3.5 KiB
Python

import pandas as pd
import numpy as np
import csv
import torch
from nltk.tokenize import word_tokenize
from gensim import downloader
FEATURES = ['content', 'id', 'label']
PATHS = ['train/in.tsv', 'train/expected.tsv', 'dev-0/in.tsv', './dev-0/out.tsv']
PRE_TRAINED = 'word2vec-google-news-300'
class NeuralNetwork(torch.nn.Module):
def __init__(self, INPUT_DIM):
super(NeuralNetwork, self).__init__()
self.l1 = torch.nn.Linear(INPUT_DIM, 500)
self.l2 = torch.nn.Linear(500, 1)
def forward(self, x):
x = self.l1(x)
x = torch.relu(x)
x = self.l2(x)
x = torch.sigmoid(x)
return x
def get_data(FEATURES, PATHS):
x_train = pd.read_table(PATHS[0], error_bad_lines = False, header = None, quoting = csv.QUOTE_NONE, names = FEATURES[:2])
y_train = pd.read_table(PATHS[1], error_bad_lines = False, header = None, quoting = csv.QUOTE_NONE, names = FEATURES[2:])
x_dev = pd.read_table(PATHS[2], error_bad_lines = False, header = None, quoting = csv.QUOTE_NONE, names = FEATURES[:2])
return x_train, y_train, x_dev
def preprocess(x_train, y_train, x_dev):
x_train = x_train[FEATURES[0]].str.lower()
x_dev = x_dev[FEATURES[0]].str.lower()
y_train = y_train[FEATURES[2]]
return x_train, y_train, x_dev
def tokenize(x_train, x_dev):
x_train = [word_tokenize(i) for i in x_train]
x_dev = [word_tokenize(i) for i in x_dev]
return x_train, x_dev
def use_word2vec():
w2v = downloader.load(PRE_TRAINED)
return w2v
def document_vector(w2v, x_train, x_dev):
x_train = [np.mean([w2v[word] for word in doc if word in w2v] or [np.zeros(300)], axis = 0) for doc in x_train]
x_dev = [np.mean([w2v[word] for word in doc if word in w2v] or [np.zeros(300)], axis = 0) for doc in x_dev]
return x_train, x_dev
def basic_config():
INPUT_DIM = 300
BATCH_SIZE = 5
return INPUT_DIM, BATCH_SIZE
def init_model(INPUT_DIM):
nn_model = NeuralNetwork(INPUT_DIM)
criterion = torch.nn.BCELoss()
optimizer = torch.optim.SGD(nn_model.parameters(), lr = 0.1)
return nn_model, optimizer, criterion
def train(nn_model, BATCH_SIZE, criterion, optimizer, x_train, y_train):
for epoch in range(5):
nn_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 = nn_model(X.float())
loss = criterion(outputs, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
def prediction(nn_model, BATCH_SIZE, x_dev):
y_dev = []
nn_model.eval()
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 = nn_model(X.float())
prediction = (outputs > 0.5)
y_dev += prediction.tolist()
return y_dev
def get_result(y_dev):
np.asarray(y_dev, dtype = np.int32).tofile(PATHS[3], sep='\n')
def main():
x_train, y_train, x_dev = get_data(FEATURES, PATHS)
x_train, y_train, x_dev = preprocess(x_train, y_train, x_dev)
x_train, x_dev = tokenize(x_train, x_dev)
w2v = use_word2vec()
x_train, x_dev = document_vector(w2v, x_train, x_dev)
INPUT_DIM, BATCH_SIZE = basic_config()
nn_model, optimizer, criterion = init_model(INPUT_DIM)
train(nn_model, BATCH_SIZE, criterion, optimizer, x_train, y_train)
y_dev = prediction(nn_model, BATCH_SIZE, x_dev)
get_result(y_dev)
if _name_ == '_main_':
main()