This commit is contained in:
s406917 2021-05-31 20:41:40 +02:00
parent de84a1a2e7
commit 9ca2e731a8
3 changed files with 1431 additions and 1371 deletions

File diff suppressed because one or more lines are too long

100
main.py
View File

@ -1,34 +1,86 @@
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import pandas as pd import pandas as pd
import torch
from nltk.tokenize import word_tokenize
from gensim.models import Word2Vec
import gensim.downloader as gensim_downloader
train = pd.read_csv('train/train.tsv', sep='\t', header=None, error_bad_lines=False) class NeuralNetworkModel(torch.nn.Module):
X_train = train[0].astype(str).tolist() def __init__(self):
Y_train = train[1].astype(str).tolist() super(NeuralNetworkModel, self).__init__()
self.l01 = torch.nn.Linear(300,300)
self.l02 = torch.nn.Linear(300,1)
naive_b = MultinomialNB() def forward(self, x):
count_vec = CountVectorizer() x = self.l01(x)
x = torch.relu(x)
x = self.l02(x)
x = torch.sigmoid(x)
return x
Y_train=count_vec.fit_transform(Y_train) def doc2vec(doc):
naive_b.fit(Y_train, X_train) return np.mean([word2vec[word] for word in doc if word in word2vec] or [np.zeros(300)], axis=0)
dev = pd.read_csv('dev-0/in.tsv', sep='\n', header=None) train = pd.read_table('train/train.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
X_dev = dev[0].astype(str).tolist() X_dev = pd.read_table('dev-0/in.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
Y_dev = count_vec.transform(X_dev) Y_dev = pd.read_table('dev-0/expected.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
dev_predict = naive_b.predict(Y_dev) X_test = pd.read_table('test-A/in.tsv', error_bad_lines=False, sep='\t', header=None, quoting=3)
dev_out = open('dev-0/out.tsv', 'w')
for p in dev_predict: X_train = train[1].str.lower()
dev_out.write(p + '\n') Y_train = train[0]
X_dev = X_dev[0].str.lower()
X_test = X_test[0].str.lower()
test = pd.read_csv('test-A/in.tsv', sep='\n', header=None) X_train = [word_tokenize(x) for x in X_train]
X_test = test[0].astype(str).tolist() X_dev = [word_tokenize(x) for x in X_dev]
Y_test = count_vec.transform(X_test) X_test = [word_tokenize(x) for x in X_test]
test_predict = naive_b.predict(Y_test)
test_out = open('test-A/out.tsv', 'w')
for p in test_predict: word2vec = gensim_downloader.load('word2vec-google-news-300')
test_out.write(p + '\n')
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)

File diff suppressed because it is too large Load Diff