103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
|
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)
|