Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ca21607fbd | ||
|
ad803e1b81 |
94
Logistic-Regression.py
Normal file
94
Logistic-Regression.py
Normal file
@ -0,0 +1,94 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
import torch
|
||||
import gensim.downloader as gensim
|
||||
from nltk.tokenize import word_tokenize
|
||||
|
||||
x_train = pd.read_table('train/in.tsv', sep='\t', header = None, error_bad_lines = False, quoting = 3)
|
||||
y_train = pd.read_table('train/expected.tsv', sep='\t', header = None, quoting = 3)
|
||||
y_train = y_train[0]
|
||||
x_dev = pd.read_table('dev-0/in.tsv', sep='\t', header = None, quoting = 3)
|
||||
x_test = pd.read_table('test-A/in.tsv', sep='\t', header = None, quoting = 3)
|
||||
|
||||
x_train = x_train[0].str.lower()
|
||||
x_dev = x_dev[0].str.lower()
|
||||
x_test = x_test[0].str.lower()
|
||||
|
||||
x_train = [word_tokenize(x) for x in x_train]
|
||||
x_dev = [word_tokenize(x) for x in x_dev]
|
||||
x_test = [word_tokenize(x) for x in x_test]
|
||||
|
||||
word2vec = gensim.load('glove-wiki-gigaword-50')
|
||||
|
||||
def document_vector(doc):
|
||||
return np.mean([word2vec[word] for word in doc if word in word2vec] or [np.zeros(50)], axis=0)
|
||||
|
||||
x_train = [document_vector(doc) for doc in x_train]
|
||||
x_dev = [document_vector(doc) for doc in x_dev]
|
||||
x_test = [document_vector(doc) for doc in x_test]
|
||||
|
||||
class NeuralNetworkModel(torch.nn.Module):
|
||||
def __init__(self, features):
|
||||
super(NeuralNetworkModel, self).__init__()
|
||||
self.fc1 = torch.nn.Linear(50, features)
|
||||
self.fc2 = torch.nn.Linear(features, 1)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.fc1(x)
|
||||
x = torch.relu(x)
|
||||
x = self.fc2(x)
|
||||
x = torch.sigmoid(x)
|
||||
return x
|
||||
|
||||
nn_model = NeuralNetworkModel(100)
|
||||
BATCH_SIZE = 5
|
||||
criterion = torch.nn.BCELoss()
|
||||
optimizer = torch.optim.SGD(nn_model.parameters(), lr = 0.1)
|
||||
|
||||
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()
|
||||
|
||||
y_dev = []
|
||||
y_test = []
|
||||
|
||||
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())
|
||||
|
||||
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 = nn_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 = pd.DataFrame({'label':y_dev})
|
||||
Y_test = pd.DataFrame({'label':y_test})
|
||||
|
||||
Y_dev.to_csv(r'dev-0/out.tsv', sep='\t', index=False, header=False)
|
||||
Y_test.to_csv(r'test-A/out.tsv', sep='\t', index=False, header=False)
|
5272
dev-0/out.tsv
Normal file
5272
dev-0/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
5
geval-results.txt
Normal file
5
geval-results.txt
Normal file
@ -0,0 +1,5 @@
|
||||
Likelihood 0.0000
|
||||
Accuracy 0.7289
|
||||
F1.0 0.5594
|
||||
Precision 0.6587
|
||||
Recall 0.4861
|
5152
test-A/out.tsv
Normal file
5152
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user