192 lines
3.7 KiB
Python
192 lines
3.7 KiB
Python
#!/usr/bin/env python
|
|
# coding: utf-8
|
|
|
|
# In[1]:
|
|
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
import torch
|
|
import csv
|
|
import lzma
|
|
import gensim.downloader
|
|
from nltk import word_tokenize
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
|
|
|
|
|
|
# In[2]:
|
|
|
|
|
|
def predict_year(x, path_out, model):
|
|
results = model.predict(x)
|
|
with open(path_out, 'wt') as file:
|
|
for r in results:
|
|
file.write(str(r) + '\n')
|
|
|
|
|
|
# In[3]:
|
|
|
|
|
|
def read_file(filename):
|
|
result = []
|
|
with open(filename, 'r', encoding="utf-8") as file:
|
|
for line in file:
|
|
text = line.split("\t")[0].strip()
|
|
result.append(text)
|
|
return result
|
|
|
|
|
|
# In[4]:
|
|
|
|
|
|
x_train = pd.read_table('train/in.tsv', sep='\t', header=None, quoting=3)
|
|
x_train = x_train[0:200000]
|
|
x_train
|
|
|
|
|
|
# In[5]:
|
|
|
|
|
|
with open('train/expected.tsv', 'r', encoding='utf8') as file:
|
|
y_train = pd.read_csv(file, sep='\t', header=None)
|
|
y_train = y_train[0:200000]
|
|
y_train
|
|
|
|
|
|
# In[6]:
|
|
|
|
|
|
with open('dev-0/in.tsv', 'r', encoding='utf8') as file:
|
|
x_dev = pd.read_csv(file, sep='\t', header=None)
|
|
x_dev
|
|
|
|
|
|
# In[7]:
|
|
|
|
|
|
with open('test-A/in.tsv', 'r', encoding='utf8') as file:
|
|
x_test = pd.read_csv(file, sep='\t', header=None)
|
|
x_test
|
|
|
|
|
|
# In[8]:
|
|
|
|
|
|
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
|
|
|
|
|
|
# In[9]:
|
|
|
|
|
|
x_train = x_train[0].str.lower()
|
|
y_train = y_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]
|
|
|
|
|
|
# In[11]:
|
|
|
|
|
|
from gensim.test.utils import common_texts
|
|
from gensim.models import Word2Vec
|
|
|
|
word2vec = gensim.downloader.load('word2vec-google-news-300')
|
|
x_train = [np.mean([word2vec[word] for word in content if word in word2vec] or [np.zeros(300)], axis=0) for content in x_train]
|
|
x_dev = [np.mean([word2vec[word] for word in content if word in word2vec] or [np.zeros(300)], axis=0) for content in x_dev]
|
|
x_test = [np.mean([word2vec[word] for word in content if word in word2vec] or [np.zeros(300)], axis=0) for content in x_test]
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
model = NeuralNetworkModel()
|
|
BATCH_SIZE = 5
|
|
criterion = torch.nn.BCELoss()
|
|
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
|
|
|
|
for epoch in range(BATCH_SIZE):
|
|
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()
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
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())
|
|
prediction = (outputs > 0.5)
|
|
y_dev += prediction.tolist()
|
|
|
|
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 += prediction.tolist()
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
y_dev = np.asarray(y_dev, dtype=np.int32)
|
|
y_test = np.asarray(y_test, dtype=np.int32)
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
with open('./dev-0/out.tsv', 'wt') as file:
|
|
for r in y_dev:
|
|
file.write(str(r) + '\n')
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
with open('./test-A/out.tsv', 'wt') as file:
|
|
for r in y_test:
|
|
file.write(str(r) + '\n')
|
|
|
|
|
|
# In[ ]:
|
|
|
|
|
|
get_ipython().system('jupyter nbconvert --to script run.ipynb')
|
|
|