word2vec
This commit is contained in:
parent
de84a1a2e7
commit
9ca2e731a8
1350
dev-0/out.tsv
1350
dev-0/out.tsv
File diff suppressed because one or more lines are too long
100
main.py
100
main.py
@ -1,34 +1,86 @@
|
||||
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
from sklearn.naive_bayes import MultinomialNB
|
||||
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
|
||||
|
||||
train = pd.read_csv('train/train.tsv', sep='\t', header=None, error_bad_lines=False)
|
||||
X_train = train[0].astype(str).tolist()
|
||||
Y_train = train[1].astype(str).tolist()
|
||||
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)
|
||||
|
||||
naive_b = MultinomialNB()
|
||||
count_vec = CountVectorizer()
|
||||
def forward(self, x):
|
||||
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)
|
||||
naive_b.fit(Y_train, X_train)
|
||||
def doc2vec(doc):
|
||||
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)
|
||||
X_dev = dev[0].astype(str).tolist()
|
||||
Y_dev = count_vec.transform(X_dev)
|
||||
dev_predict = naive_b.predict(Y_dev)
|
||||
dev_out = open('dev-0/out.tsv', 'w')
|
||||
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)
|
||||
|
||||
for p in dev_predict:
|
||||
dev_out.write(p + '\n')
|
||||
X_train = train[1].str.lower()
|
||||
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_test = test[0].astype(str).tolist()
|
||||
Y_test = count_vec.transform(X_test)
|
||||
test_predict = naive_b.predict(Y_test)
|
||||
test_out = open('test-A/out.tsv', 'w')
|
||||
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]
|
||||
|
||||
for p in test_predict:
|
||||
test_out.write(p + '\n')
|
||||
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)
|
1352
test-A/out.tsv
1352
test-A/out.tsv
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user