Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
9ca2e731a8 | ||
|
de84a1a2e7 |
5452
dev-0/out.tsv
Normal file
5452
dev-0/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
86
main.py
Normal file
86
main.py
Normal file
@ -0,0 +1,86 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import torch
|
||||
from nltk.tokenize import word_tokenize
|
||||
from gensim.models import Word2Vec
|
||||
import gensim.downloader as gensim_downloader
|
||||
|
||||
class NeuralNetworkModel(torch.nn.Module):
|
||||
def __init__(self):
|
||||
super(NeuralNetworkModel, self).__init__()
|
||||
self.l01 = torch.nn.Linear(300,300)
|
||||
self.l02 = torch.nn.Linear(300,1)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.l01(x)
|
||||
x = torch.relu(x)
|
||||
x = self.l02(x)
|
||||
x = torch.sigmoid(x)
|
||||
return x
|
||||
|
||||
def doc2vec(doc):
|
||||
return np.mean([word2vec[word] for word in doc if word in word2vec] or [np.zeros(300)], axis=0)
|
||||
|
||||
train = pd.read_table('train/train.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
|
||||
X_dev = pd.read_table('dev-0/in.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
|
||||
Y_dev = pd.read_table('dev-0/expected.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
|
||||
X_test = pd.read_table('test-A/in.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
|
||||
|
||||
X_train = train[1].str.lower()
|
||||
Y_train = train[0]
|
||||
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_downloader.load('word2vec-google-news-300')
|
||||
|
||||
X_train = [doc2vec(doc) for doc in X_train]
|
||||
X_dev = [doc2vec(doc) for doc in X_dev]
|
||||
X_test = [doc2vec(doc) for doc in X_test]
|
||||
|
||||
model = NeuralNetworkModel()
|
||||
BATCH_SIZE = 5
|
||||
criterion = torch.nn.BCELoss()
|
||||
optimizer = torch.optim.Adam(model.parameters())
|
||||
|
||||
for epoch in range(5):
|
||||
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)
|
||||
optimizer.zero_grad()
|
||||
outputs = model(X.float())
|
||||
loss = criterion(outputs, Y)
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
Y_dev = []
|
||||
Y_test = []
|
||||
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 = 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 = 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)
|
||||
dev = pd.DataFrame({'label': Y_dev})
|
||||
test = pd.DataFrame({'label': Y_test})
|
||||
dev.to_csv(r'dev-0/out.tsv', sep='\t', index=False, header=False)
|
||||
test.to_csv(r'test-A/out.tsv', sep='\t', index=False, header=False)
|
5445
test-A/out.tsv
Normal file
5445
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user