115 lines
3.0 KiB
Python
115 lines
3.0 KiB
Python
import gensim.downloader
|
|
from nltk.tokenize import word_tokenize
|
|
import numpy as np
|
|
import pandas as pd
|
|
import torch
|
|
|
|
|
|
word2vec = gensim.downloader.load("word2vec-google-news-300")
|
|
|
|
|
|
def get_word2vec(document):
|
|
return np.mean(
|
|
[word2vec[token] for token in document if token in word2vec] or [np.zeros(300)],
|
|
axis=0,
|
|
)
|
|
|
|
|
|
class LogNetwork(torch.nn.Module):
|
|
def __init__(self, input_size, hidden_size, num_classes):
|
|
super(LogNetwork, self).__init__()
|
|
self.fc1 = torch.nn.Linear(input_size, hidden_size)
|
|
self.fc2 = torch.nn.Linear(hidden_size, num_classes)
|
|
|
|
def forward(self, x):
|
|
x = self.fc1(x)
|
|
x = torch.relu(x)
|
|
x = self.fc2(x)
|
|
x = torch.sigmoid(x)
|
|
return x
|
|
|
|
|
|
train_x = pd.read_table(
|
|
"train/in.tsv.xz",
|
|
error_bad_lines=False,
|
|
header=None,
|
|
quoting=3,
|
|
usecols=["content"],
|
|
names=["content", "id"],
|
|
nrows=225000,
|
|
)
|
|
train_y = pd.read_table(
|
|
"train/expected.tsv",
|
|
error_bad_lines=False,
|
|
header=None,
|
|
quoting=3,
|
|
usecols=["label"],
|
|
names=["label"],
|
|
nrows=225000,
|
|
)
|
|
dev_x = pd.read_table(
|
|
"dev-0/in.tsv.xz",
|
|
error_bad_lines=False,
|
|
header=None,
|
|
quoting=3,
|
|
usecols=["content"],
|
|
names=["content", "id"],
|
|
)
|
|
test_x = pd.read_table(
|
|
"test-A/in.tsv.xz",
|
|
error_bad_lines=False,
|
|
header=None,
|
|
quoting=3,
|
|
usecols=["content"],
|
|
names=["content", "id"],
|
|
)
|
|
|
|
train_x = [word_tokenize(row) for row in train_x.content.str.lower()]
|
|
dev_x = [word_tokenize(row) for row in dev_x.content.str.lower()]
|
|
test_x = [word_tokenize(row) for row in test_x.content.str.lower()]
|
|
|
|
train_x = [get_word2vec(document) for document in train_x]
|
|
dev_x = [get_word2vec(document) for document in dev_x]
|
|
test_x = [get_word2vec(document) for document in test_x]
|
|
|
|
network = LogNetwork(300, 600, 1)
|
|
criterion = torch.nn.BCELoss()
|
|
optim = torch.optim.SGD(network.parameters(), lr=0.01)
|
|
epochs = 7
|
|
batch_size = 15
|
|
|
|
for _ in range(epochs):
|
|
network.train()
|
|
for i in range(0, train_y.shape[0], batch_size):
|
|
x = train_x[i : i + batch_size]
|
|
x = torch.tensor(x)
|
|
y = train_y[i : i + batch_size]
|
|
y = torch.tensor(y.astype(np.float32).to_numpy()).reshape(-1, 1)
|
|
outputs = network(x.float())
|
|
loss = criterion(outputs, y)
|
|
optim.zero_grad()
|
|
loss.backward()
|
|
optim.step()
|
|
|
|
|
|
dev_pred = []
|
|
test_pred = []
|
|
with torch.no_grad():
|
|
for i in range(0, len(dev_x), batch_size):
|
|
x = dev_x[i : i + batch_size]
|
|
x = torch.tensor(x)
|
|
outputs = network(x.float())
|
|
prediction = outputs >= 0.5
|
|
dev_pred += prediction.tolist()
|
|
for i in range(0, len(test_x), batch_size):
|
|
x = test_x[i : i + batch_size]
|
|
x = torch.tensor(x)
|
|
outputs = network(x.float())
|
|
prediction = outputs >= 0.5
|
|
test_pred += prediction.tolist()
|
|
|
|
np.asarray(dev_pred, dtype=np.int32).tofile("./dev-0/out.tsv", sep="\n")
|
|
np.asarray(test_pred, dtype=np.int32).tofile("./test-A/out.tsv", sep="\n")
|
|
|
|
print("Saved")
|