nn - bigrams
This commit is contained in:
parent
27944ca2c8
commit
3161a6a902
21038
dev-0/out.tsv
21038
dev-0/out.tsv
File diff suppressed because it is too large
Load Diff
62
run.py
62
run.py
@ -1,4 +1,15 @@
|
||||
#%%
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Untitled0.ipynb
|
||||
|
||||
Automatically generated by Colaboratory.
|
||||
|
||||
Original file is located at
|
||||
https://colab.research.google.com/drive/1vjpmLsNPjPLM1_5fBGbBYg-ZqdXQeGQH
|
||||
"""
|
||||
|
||||
from google.colab import drive
|
||||
drive.mount('/content/gdrive/')
|
||||
|
||||
# importy
|
||||
from torchtext.vocab import build_vocab_from_iterator
|
||||
from torch.utils.data import DataLoader
|
||||
@ -11,7 +22,7 @@ from os.path import exists
|
||||
|
||||
vocab_size = 30000
|
||||
embed_size = 150
|
||||
#%%
|
||||
|
||||
# funkcje pomocnicze
|
||||
def clean(text):
|
||||
text = str(text).strip().lower()
|
||||
@ -32,7 +43,7 @@ def get_word_lines_from_data(d):
|
||||
for line in d:
|
||||
yield get_words_from_line(line)
|
||||
|
||||
#%%
|
||||
|
||||
class Model(torch.nn.Module):
|
||||
def __init__(self, vocabulary_size, embedding_size):
|
||||
super(Model, self).__init__()
|
||||
@ -45,7 +56,7 @@ class Model(torch.nn.Module):
|
||||
def forward(self, x):
|
||||
return self.model(x)
|
||||
|
||||
#%%
|
||||
|
||||
class Trigrams(torch.utils.data.IterableDataset):
|
||||
def __init__(self, data, vocabulary_size):
|
||||
self.vocab = build_vocab_from_iterator(
|
||||
@ -69,45 +80,48 @@ class Trigrams(torch.utils.data.IterableDataset):
|
||||
(self.vocab[t] for t in itertools.chain.from_iterable(get_word_lines_from_data(self.data))))
|
||||
|
||||
|
||||
#%%
|
||||
# ładowanie danych treningowych
|
||||
train_in = pd.read_csv("train/in.tsv.xz", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)[[6, 7]]
|
||||
train_expected = pd.read_csv("train/expected.tsv", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)
|
||||
train_in = pd.read_csv("gdrive/MyDrive/train/in.tsv.xz", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)[[6, 7]]
|
||||
train_expected = pd.read_csv("gdrive/MyDrive/train/expected.tsv", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)
|
||||
train_data = pd.concat([train_in, train_expected], axis=1)
|
||||
train_data = train_data[6] + train_data[0] + train_data[7]
|
||||
train_data = train_data.apply(clean)
|
||||
train_dataset = Trigrams(train_data, vocab_size)
|
||||
|
||||
#%%
|
||||
# trenowanie/wczytywanie modelu
|
||||
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
||||
model = Model(vocab_size, embed_size).to(device)
|
||||
print(device)
|
||||
if(not exists('model1.bin')):
|
||||
data = DataLoader(train_dataset, batch_size=200)
|
||||
data = DataLoader(train_dataset, batch_size=8000)
|
||||
optimizer = torch.optim.Adam(model.parameters())
|
||||
criterion = torch.nn.NLLLoss()
|
||||
|
||||
model.train()
|
||||
step = 0
|
||||
for x, y in data:
|
||||
x = x.to(device)
|
||||
y = y.to(device)
|
||||
optimizer.zero_grad()
|
||||
ypredicted = model(x)
|
||||
loss = criterion(torch.log(ypredicted), y)
|
||||
if step % 100 == 0:
|
||||
print(step, loss)
|
||||
step += 1
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
for i in range(2):
|
||||
print(f"EPOCH {i}=========================")
|
||||
for x, y in data:
|
||||
x = x.to(device)
|
||||
y = y.to(device)
|
||||
optimizer.zero_grad()
|
||||
ypredicted = model(x)
|
||||
loss = criterion(torch.log(ypredicted), y)
|
||||
if step % 100 == 0:
|
||||
print(step, loss)
|
||||
step += 1
|
||||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
torch.save(model.state_dict(), 'model1.bin')
|
||||
else:
|
||||
model.load_state_dict(torch.load('model1.bin'))
|
||||
|
||||
#%%
|
||||
|
||||
vocab = train_dataset.vocab
|
||||
|
||||
import nltk
|
||||
nltk.download('punkt')
|
||||
def predict(tokens):
|
||||
ixs = torch.tensor(vocab.forward(tokens)).to(device)
|
||||
out = model(ixs)
|
||||
@ -134,11 +148,11 @@ def predict_file(result_path, data):
|
||||
f.write(result + "\n")
|
||||
print(result)
|
||||
|
||||
#%%
|
||||
dev_data = pd.read_csv("dev-0/in.tsv.xz", sep='\t', header=None, quoting=csv.QUOTE_NONE)[6]
|
||||
|
||||
dev_data = pd.read_csv("gdrive/MyDrive/dev-0/in.tsv.xz", sep='\t', header=None, quoting=csv.QUOTE_NONE)[6]
|
||||
dev_data = dev_data.apply(clean)
|
||||
predict_file("dev-0/out.tsv", dev_data)
|
||||
|
||||
test_data = pd.read_csv("test-A/in.tsv.xz", sep='\t', header=None, quoting=csv.QUOTE_NONE)[6]
|
||||
test_data = pd.read_csv("gdrive/MyDrive/test-A/in.tsv.xz", sep='\t', header=None, quoting=csv.QUOTE_NONE)[6]
|
||||
test_data = test_data.apply(clean)
|
||||
predict_file("test-A/out.tsv", test_data)
|
14828
test-A/out.tsv
14828
test-A/out.tsv
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user