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
40
run.py
40
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
|
# importy
|
||||||
from torchtext.vocab import build_vocab_from_iterator
|
from torchtext.vocab import build_vocab_from_iterator
|
||||||
from torch.utils.data import DataLoader
|
from torch.utils.data import DataLoader
|
||||||
@ -11,7 +22,7 @@ from os.path import exists
|
|||||||
|
|
||||||
vocab_size = 30000
|
vocab_size = 30000
|
||||||
embed_size = 150
|
embed_size = 150
|
||||||
#%%
|
|
||||||
# funkcje pomocnicze
|
# funkcje pomocnicze
|
||||||
def clean(text):
|
def clean(text):
|
||||||
text = str(text).strip().lower()
|
text = str(text).strip().lower()
|
||||||
@ -32,7 +43,7 @@ def get_word_lines_from_data(d):
|
|||||||
for line in d:
|
for line in d:
|
||||||
yield get_words_from_line(line)
|
yield get_words_from_line(line)
|
||||||
|
|
||||||
#%%
|
|
||||||
class Model(torch.nn.Module):
|
class Model(torch.nn.Module):
|
||||||
def __init__(self, vocabulary_size, embedding_size):
|
def __init__(self, vocabulary_size, embedding_size):
|
||||||
super(Model, self).__init__()
|
super(Model, self).__init__()
|
||||||
@ -45,7 +56,7 @@ class Model(torch.nn.Module):
|
|||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
return self.model(x)
|
return self.model(x)
|
||||||
|
|
||||||
#%%
|
|
||||||
class Trigrams(torch.utils.data.IterableDataset):
|
class Trigrams(torch.utils.data.IterableDataset):
|
||||||
def __init__(self, data, vocabulary_size):
|
def __init__(self, data, vocabulary_size):
|
||||||
self.vocab = build_vocab_from_iterator(
|
self.vocab = build_vocab_from_iterator(
|
||||||
@ -69,26 +80,27 @@ class Trigrams(torch.utils.data.IterableDataset):
|
|||||||
(self.vocab[t] for t in itertools.chain.from_iterable(get_word_lines_from_data(self.data))))
|
(self.vocab[t] for t in itertools.chain.from_iterable(get_word_lines_from_data(self.data))))
|
||||||
|
|
||||||
|
|
||||||
#%%
|
|
||||||
# ładowanie danych treningowych
|
# ł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_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("train/expected.tsv", sep='\t', header=None, encoding="UTF-8", on_bad_lines="skip", quoting=csv.QUOTE_NONE, nrows=300000)
|
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 = pd.concat([train_in, train_expected], axis=1)
|
||||||
train_data = train_data[6] + train_data[0] + train_data[7]
|
train_data = train_data[6] + train_data[0] + train_data[7]
|
||||||
train_data = train_data.apply(clean)
|
train_data = train_data.apply(clean)
|
||||||
train_dataset = Trigrams(train_data, vocab_size)
|
train_dataset = Trigrams(train_data, vocab_size)
|
||||||
|
|
||||||
#%%
|
|
||||||
# trenowanie/wczytywanie modelu
|
# trenowanie/wczytywanie modelu
|
||||||
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
||||||
model = Model(vocab_size, embed_size).to(device)
|
model = Model(vocab_size, embed_size).to(device)
|
||||||
|
print(device)
|
||||||
if(not exists('model1.bin')):
|
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())
|
optimizer = torch.optim.Adam(model.parameters())
|
||||||
criterion = torch.nn.NLLLoss()
|
criterion = torch.nn.NLLLoss()
|
||||||
|
|
||||||
model.train()
|
model.train()
|
||||||
step = 0
|
step = 0
|
||||||
|
for i in range(2):
|
||||||
|
print(f"EPOCH {i}=========================")
|
||||||
for x, y in data:
|
for x, y in data:
|
||||||
x = x.to(device)
|
x = x.to(device)
|
||||||
y = y.to(device)
|
y = y.to(device)
|
||||||
@ -105,9 +117,11 @@ if(not exists('model1.bin')):
|
|||||||
else:
|
else:
|
||||||
model.load_state_dict(torch.load('model1.bin'))
|
model.load_state_dict(torch.load('model1.bin'))
|
||||||
|
|
||||||
#%%
|
|
||||||
vocab = train_dataset.vocab
|
vocab = train_dataset.vocab
|
||||||
|
|
||||||
|
import nltk
|
||||||
|
nltk.download('punkt')
|
||||||
def predict(tokens):
|
def predict(tokens):
|
||||||
ixs = torch.tensor(vocab.forward(tokens)).to(device)
|
ixs = torch.tensor(vocab.forward(tokens)).to(device)
|
||||||
out = model(ixs)
|
out = model(ixs)
|
||||||
@ -134,11 +148,11 @@ def predict_file(result_path, data):
|
|||||||
f.write(result + "\n")
|
f.write(result + "\n")
|
||||||
print(result)
|
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)
|
dev_data = dev_data.apply(clean)
|
||||||
predict_file("dev-0/out.tsv", dev_data)
|
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)
|
test_data = test_data.apply(clean)
|
||||||
predict_file("test-A/out.tsv", test_data)
|
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