x
This commit is contained in:
parent
756ef4277a
commit
c8fcf6e4df
102
neural-network.py
Normal file
102
neural-network.py
Normal file
@ -0,0 +1,102 @@
|
||||
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)
|
151
neural-network2.py
Normal file
151
neural-network2.py
Normal file
@ -0,0 +1,151 @@
|
||||
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])
|
||||
|
Loading…
Reference in New Issue
Block a user