Add final solution

This commit is contained in:
michalzareba 2021-05-22 17:46:18 +02:00
parent c8fcf6e4df
commit 3d78363674
5 changed files with 6305 additions and 1237 deletions

5272
dev-0/out.tsv Normal file

File diff suppressed because it is too large Load Diff

169
feed-forward-nn.py Normal file
View File

@ -0,0 +1,169 @@
import csv
import gensim.downloader
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from nltk import word_tokenize
# Feed forward neural network model
class FeedforwardNeuralNetModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(FeedforwardNeuralNetModel, self).__init__()
# Linear function 1: vocab_size --> 500
self.fc1 = nn.Linear(input_dim, hidden_dim)
# Non-linearity 1
self.relu1 = nn.ReLU()
# Linear function 2: 500 --> 500
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
# Non-linearity 2
self.relu2 = nn.ReLU()
# Linear function 3 (readout): 500 --> 3
self.fc3 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# Linear function 1
out = self.fc1(x)
# Non-linearity 1
out = self.relu1(out)
# Non-linearity 2
out = self.relu2(out)
# Linear function 3 (readout)
out = self.fc3(out)
return torch.sigmoid(out)
col_names = ["content", "id", "label"]
# Loading dataset
train_set_features = pd.read_table(
"train/in.tsv.xz",
error_bad_lines=False,
quoting=csv.QUOTE_NONE,
header=None,
names=col_names[:2],
)
train_set_labels = pd.read_table(
"train/expected.tsv",
error_bad_lines=False,
quoting=csv.QUOTE_NONE,
header=None,
names=col_names[2:],
)
dev_set = pd.read_table(
"dev-0/in.tsv.xz",
error_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
names=col_names[:2],
)
test_set = pd.read_table(
"test-A/in.tsv.xz",
error_bad_lines=False,
header=None,
quoting=csv.QUOTE_NONE,
names=col_names[:2],
)
# Lowercase text
X_train = train_set_features["content"].str.lower()
y_train = train_set_labels["label"]
X_dev = dev_set["content"].str.lower()
X_test = test_set["content"].str.lower()
# Tokenize text with nltk
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]
# Vectorize text
word2vec = gensim.downloader.load("word2vec-google-news-300")
X_train = [
np.mean(
[word2vec[word] for word in content if word in word2vec] or [np.zeros(300)],
axis=0,
)
for content in X_train
]
X_dev = [
np.mean(
[word2vec[word] for word in content if word in word2vec] or [np.zeros(300)],
axis=0,
)
for content in X_dev
]
X_test = [
np.mean(
[word2vec[word] for word in content if word in word2vec] or [np.zeros(300)],
axis=0,
)
for content in X_test
]
# Model config
input_dim = 300
hidden_layer = 600
output_dim = 1
batch_size = 10
epochs = 10
# Model init
model = FeedforwardNeuralNetModel(input_dim, hidden_layer, output_dim)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = torch.nn.BCELoss()
# Learning model
for epoch in range(epochs):
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()
# Making predictions for dev-0 & and test-A
test_prediction = []
dev_prediction = []
model.eval()
with torch.no_grad():
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())
prediction = outputs > 0.5
test_prediction += prediction.tolist()
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())
prediction = outputs > 0.5
dev_prediction += prediction.tolist()
test_prediction = np.asarray(test_prediction, dtype=np.int32)
dev_prediction = np.asarray(dev_prediction, dtype=np.int32)
test_prediction.tofile("./test-A/out.tsv", sep="\n")
dev_prediction.tofile("./dev-0/out.tsv", sep="\n")

View File

@ -1,102 +0,0 @@
import os
import pandas as pd
import tensorflow as tf
import numpy as np
import torch
import torch.nn as nn
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
print('debug 1')
train_df = pd.read_csv('train/in.tsv', header=None, sep='\t')
test_df = pd.read_csv('test-A/in.tsv', header=None, sep='\t')
dev_df = pd.read_csv('dev-0/in.tsv', header=None, sep='\t')
train_expected = pd.read_csv('train/expected.tsv', header=None, sep='\t')
train_text = train_df[0].tolist()
test_text = test_df[0].tolist()
dev_text = test_df[0].tolist()
text_data = train_text + test_text + dev_text
vectorize_layer = TextVectorization(max_tokens=5, output_mode="int")
text_data = tf.data.Dataset.from_tensor_slices(text_data)
vectorize_layer.adapt(text_data.batch(64))
inputs = tf.keras.layers.Input(shape=(1,), dtype=tf.string, name="text")
outputs = vectorize_layer(inputs)
model = tf.keras.Model(inputs, outputs)
print('uwaga debug')
x_train = list(map(model.predict, train_text))
y_train = train_expected[0]
x_test = list(map(model.predict, test_text))
loss_function = nn.CrossEntropyLoss()
x_train = pd.DataFrame(x_train)
x_test = pd.DataFrame(x_test)
y_train = pd.DataFrame(y_train[0])
# (model.predict(["Murder in the forset!"]))
class FeedforwardNeuralNetModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(FeedforwardNeuralNetModel, self).__init__()
# Linear function
self.fc1 = nn.Linear(input_dim, hidden_dim)
# Non-linearity
self.sigmoid = nn.Sigmoid()
# Linear function (readout)
self.fc2 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# Linear function # LINEAR
out = self.fc1(x)
# Non-linearity # NON-LINEAR
out = self.sigmoid(out)
# Linear function (readout) # LINEAR
out = self.fc2(out)
return out
num_epochs = 2
for epoch in range(num_epochs):
if (epoch + 1) % 25 == 0:
print("Epoch completed: " + str(epoch + 1))
print(f"Epoch number: {epoch}")
train_loss = 0
for index, row in x_train.iterrows():
print(index)
# Forward pass to get output
probs = x_train[0][index]
# Get the target label
target = y_train[0][index]
# Calculate Loss: softmax --> cross entropy loss
loss = loss_function(probs, target)
# Accumulating the loss over time
train_loss += loss.item()
# Getting gradients w.r.t. parameters
loss.backward()
train_loss = 0
bow_ff_nn_predictions = []
original_lables_ff_bow = []
with torch.no_grad():
for index, row in x_test.iterrows():
probs = x_test[0][index]
bow_ff_nn_predictions.append(torch.argmax(probs, dim=1).cpu().numpy()[0])
print(bow_ff_nn_predictions)

View File

@ -1,151 +0,0 @@
import pandas as pd
from gensim.utils import simple_preprocess
from gensim.parsing.porter import PorterStemmer
from gensim import corpora
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch
class FeedforwardNeuralNetModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super(FeedforwardNeuralNetModel, self).__init__()
# Linear function 1: vocab_size --> 500
self.fc1 = nn.Linear(input_dim, hidden_dim)
# Non-linearity 1
self.relu1 = nn.ReLU()
# Linear function 2: 500 --> 500
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
# Non-linearity 2
self.relu2 = nn.ReLU()
# Linear function 3 (readout): 500 --> 3
self.fc3 = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
# Linear function 1
out = self.fc1(x)
# Non-linearity 1
out = self.relu1(out)
# Non-linearity 2
out = self.relu2(out)
# Linear function 3 (readout)
out = self.fc3(out)
return F.softmax(out, dim=1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_expected = pd.read_csv('train/expected.tsv', header=None, sep='\t')
train_df = pd.read_csv('train/in.tsv', header=None, sep='\t')
# test_df = pd.read_csv('test-A/in.tsv', header=None, sep='\t')
test_df = pd.read_csv('dev-0/in.tsv', header=None, sep='\t')
y_train = pd.DataFrame(train_expected[0])
train_df[0] = [simple_preprocess(text, deacc=True) for text in train_df[0]]
porter_stemmer = PorterStemmer()
train_df['stemmed_tokens'] = [[porter_stemmer.stem(word) for word in tokens] for tokens in train_df[0]]
test_df[0] = [simple_preprocess(text, deacc=True) for text in test_df[0]]
test_df['stemmed_tokens'] = [[porter_stemmer.stem(word) for word in tokens] for tokens in test_df[0]]
x_test = pd.DataFrame(test_df['stemmed_tokens'])
x_train = pd.DataFrame(train_df['stemmed_tokens'])
def make_dict(top_data_df_small, padding=True):
if padding:
print("Dictionary with padded token added")
review_dict = corpora.Dictionary([['pad']])
review_dict.add_documents(top_data_df_small['stemmed_tokens'])
else:
print("Dictionary without padding")
review_dict = corpora.Dictionary(top_data_df_small['stemmed_tokens'])
return review_dict
# Make the dictionary without padding for the basic models
review_dict = make_dict(train_df, padding=False)
VOCAB_SIZE = len(review_dict)
NUM_LABELS = 2
# Function to make bow vector to be used as input to network
def make_bow_vector(review_dict, sentence):
vec = torch.zeros(VOCAB_SIZE, dtype=torch.float64, device=device)
for word in sentence:
vec[review_dict.token2id[word]] += 1
return vec.view(1, -1).float()
input_dim = VOCAB_SIZE
hidden_dim = 10
output_dim = 2
num_epochs = 2
ff_nn_bow_model = FeedforwardNeuralNetModel(input_dim, hidden_dim, output_dim)
ff_nn_bow_model.to(device)
loss_function = nn.CrossEntropyLoss()
optimizer = optim.SGD(ff_nn_bow_model.parameters(), lr=0.001)
losses = []
iter = 0
def make_target(label):
if label == 0:
return torch.tensor([0], dtype=torch.long, device=device)
elif label == 1:
return torch.tensor([1], dtype=torch.long, device=device)
# Start training
for epoch in range(num_epochs):
if (epoch + 1) % 25 == 0:
print("Epoch completed: " + str(epoch + 1))
print(f"Epoch number: {epoch}")
train_loss = 0
for index, row in x_train.iterrows():
print(index)
# Clearing the accumulated gradients
optimizer.zero_grad()
# Make the bag of words vector for stemmed tokens
bow_vec = make_bow_vector(review_dict, row['stemmed_tokens'])
# Forward pass to get output
probs = ff_nn_bow_model(bow_vec)
# Get the target label
target = make_target(y_train[0][index])
# Calculate Loss: softmax --> cross entropy loss
loss = loss_function(probs, target)
# Accumulating the loss over time
train_loss += loss.item()
# Getting gradients w.r.t. parameters
loss.backward()
# Updating parameters
optimizer.step()
train_loss = 0
bow_ff_nn_predictions = []
original_lables_ff_bow = []
with torch.no_grad():
for index, row in x_test.iterrows():
bow_vec = make_bow_vector(review_dict, row['stemmed_tokens'])
probs = ff_nn_bow_model(bow_vec)
bow_ff_nn_predictions.append(torch.argmax(probs, dim=1).cpu().numpy()[0])

File diff suppressed because it is too large Load Diff