66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
from gensim.test.utils import common_texts
|
|
from gensim.models import Word2Vec
|
|
|
|
from sklearn import preprocessing
|
|
|
|
import pandas as pd
|
|
import numpy as np
|
|
import gensim
|
|
import torch
|
|
|
|
class NeuralNetworkModel(torch.nn.Module):
|
|
|
|
def __init__(self):
|
|
super(NeuralNetworkModel, self).__init__()
|
|
self.fc1 = torch.nn.Linear(maxim, 500)
|
|
self.fc2 = torch.nn.Linear(500, 1)
|
|
|
|
def forward(self, x):
|
|
x = self.fc1(x)
|
|
x = torch.relu(x)
|
|
x = self.fc2(x)
|
|
x = torch.sigmoid(x)
|
|
return x
|
|
|
|
with open("train/in.tsv") as xd:
|
|
x1 = xd.readlines()
|
|
with open("train/expected.tsv") as xdd:
|
|
y1 = xdd.readlines()
|
|
with open("test-A/in.tsv") as xddd:
|
|
x = xddd.readlines()
|
|
|
|
maxim = 500
|
|
bLen = 5
|
|
|
|
exp = Word2Vec(x1, min_count = 1, vector_size = 500, workers = 3, window = 3, sg = 1)
|
|
exp1 = Word2Vec(x, min_count = 1, vector_size = 500, workers = 3, window = 3, sg = 1)
|
|
exp2 = NeuralNetworkModel()
|
|
|
|
x1 = exp.wv
|
|
x = exp1.wv
|
|
|
|
crt = torch.nn.BCELoss()
|
|
miz = torch.optim.SGD(exp2.parameters(), lr = 0.1)
|
|
|
|
for each in range(10):
|
|
lossScore, accScore, sums = 0
|
|
exp2.train()
|
|
for i in range(0, y1.shape[0], bLen):
|
|
x = x1[i : i + bLen]
|
|
x = torch.tensor(x.astype(np.float32).todense())
|
|
y = y1[i : i + bLen]
|
|
y = torch.tensor(y.astype(np.float32)).reshape(-1, 1)
|
|
y2 = exp2(x)
|
|
accScore += torch.sum((y2 > 0.5) == y).item()
|
|
sums += y.shape[0]
|
|
|
|
miz.zero_grad()
|
|
loss = crt(y2, Y)
|
|
loss.backward()
|
|
miz.step()
|
|
|
|
lossScore += loss.item() * Y.shape[0]
|
|
with open('test-A/out.tsv', 'w') as file:
|
|
for each in y2:
|
|
file.write("%f\n" % each)
|