train
This commit is contained in:
parent
3203d96af0
commit
3508d2b1f0
5 changed files with 80 additions and 16 deletions
BIN
histogram.png
Normal file
BIN
histogram.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 87 KiB |
BIN
ngram-rnn-classification.pt
Normal file
BIN
ngram-rnn-classification.pt
Normal file
Binary file not shown.
|
|
@ -92,7 +92,7 @@ class Train():
|
|||
output, loss = self.train(category_tensor, name_tensor)
|
||||
self.current_loss += loss
|
||||
|
||||
if epoch % 5000 == 0:
|
||||
if epoch % 10000 == 0:
|
||||
guess, guess_i = self.categoryFromOutput(output)
|
||||
correct = '✓' if guess == category else '✗ (%s)' % category
|
||||
|
||||
|
|
@ -103,23 +103,33 @@ class Train():
|
|||
guess,
|
||||
correct))
|
||||
|
||||
if epoch % 1000 == 0:
|
||||
if epoch % 5000 == 0:
|
||||
self.all_losses.append(self.current_loss / 1000)
|
||||
self.current_loss = 0
|
||||
|
||||
# torch.save(self.rnn,'ngram-rnn-classification.pt')
|
||||
|
||||
self.plot()
|
||||
torch.save(self.rnn,'ngram-rnn-classification.pt')
|
||||
|
||||
|
||||
def plot(data):
|
||||
def plot(self):
|
||||
plt.figure()
|
||||
plt.plot(data)
|
||||
plt.plot(self.all_losses)
|
||||
plt.savefig('histogram.png', dpi=400)
|
||||
|
||||
|
||||
|
||||
# Just return an output given a numpy array
|
||||
def evaluate(self,tensor):
|
||||
hidden = self.rnn.initHidden()
|
||||
|
||||
for i in range(tensor.size()[0]):
|
||||
output, hidden = self.rnn(tensor[i], hidden)
|
||||
|
||||
return output
|
||||
|
||||
def confusionMatix(self,len):
|
||||
def confusionMatix(self):
|
||||
# Keep track of correct guesses in a confusion matrix
|
||||
confusion = torch.zeros(len, len)
|
||||
n_categories = len(self.all_categories)
|
||||
confusion = torch.zeros(n_categories, n_categories)
|
||||
n_confusion = 10000
|
||||
|
||||
# Go through a bunch of examples and record which are correctly guessed
|
||||
|
|
@ -150,6 +160,12 @@ class Train():
|
|||
|
||||
# sphinx_gallery_thumbnail_number = 2
|
||||
plt.show()
|
||||
|
||||
plt.savefig('confusion.png', dpi=400)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
from matplotlib import pyplot as plt, ticker
|
||||
from TrainClass import Train
|
||||
import torch
|
||||
|
||||
|
||||
class Evaluate():
|
||||
def __init__(self,rnn):
|
||||
self.rnn=rnn
|
||||
def __init__(self,all_categories):
|
||||
|
||||
self.all_categories = all_categories
|
||||
self.rnn = torch.load('ngram-rnn-classification.pt')
|
||||
|
||||
# Just return an output given a numpy array
|
||||
def evaluate(self,tensor):
|
||||
|
|
@ -11,6 +18,42 @@ class Evaluate():
|
|||
|
||||
return output
|
||||
|
||||
def confusionMatix(self):
|
||||
# Keep track of correct guesses in a confusion matrix
|
||||
n_categories = len(self.all_categories)
|
||||
confusion = torch.zeros(n_categories, n_categories)
|
||||
n_confusion = 10000
|
||||
|
||||
# Go through a bunch of examples and record which are correctly guessed
|
||||
for i in range(n_confusion):
|
||||
category, name, category_tensor, name_tensor = Train.randomTrainingExample()
|
||||
output = self.evaluate(name_tensor)
|
||||
guess, guess_i = self.categoryFromOutput(output)
|
||||
category_i = self.all_categories.index(category)
|
||||
confusion[category_i][guess_i] += 1
|
||||
|
||||
# Normalize by dividing every row by its sum
|
||||
for i in range(len):
|
||||
confusion[i] = confusion[i] / confusion[i].sum()
|
||||
|
||||
# Set up plot
|
||||
fig = plt.figure()
|
||||
ax = fig.add_subplot(111)
|
||||
cax = ax.matshow(confusion.numpy())
|
||||
fig.colorbar(cax)
|
||||
|
||||
# Set up axes
|
||||
ax.set_xticklabels([''] + self.all_categories, rotation=90)
|
||||
ax.set_yticklabels([''] + self.all_categories)
|
||||
|
||||
# Force label at every tick
|
||||
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
|
||||
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
|
||||
|
||||
# sphinx_gallery_thumbnail_number = 2
|
||||
plt.show()
|
||||
|
||||
|
||||
# def predict(self,line, n_predictions=3):
|
||||
# output = self.evaluate((lineToTensor(line)))
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
|
||||
import torch
|
||||
from TrainClass import Train
|
||||
from ElasticSearchClass import ElasticSearchDb
|
||||
from DataFrameClass import DataFrame
|
||||
from VectorizeClass import Vectorize
|
||||
from NN import RNN
|
||||
from evaluate import Evaluate
|
||||
|
||||
|
||||
def main():
|
||||
|
|
@ -30,18 +32,21 @@ def main():
|
|||
# bool_cat_series = pd.isnull(df_eng["name"])
|
||||
# print(df_eng[bool_cat_series])
|
||||
inputSize=vectorizer.getTransformedVectorSize()
|
||||
n_hidden = 256*6
|
||||
n_hidden = 256*4
|
||||
df_category = df_en.groupby('category')
|
||||
all_category = list(df_category.groups.keys())
|
||||
n_category = len(all_category)
|
||||
rnn=RNN(inputSize, n_hidden, n_category)
|
||||
# rnn=RNN(inputSize, n_hidden, n_category)
|
||||
|
||||
|
||||
rnn=torch.load('ngram-rnn-classification.pt')
|
||||
train = Train(vectorizer,df_en,all_category,rnn)
|
||||
|
||||
train.run(200000)
|
||||
train.confusionMatix()
|
||||
|
||||
|
||||
|
||||
|
||||
# train.run(200000)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue