ngram rnn classification
This commit is contained in:
parent
55a1b9f552
commit
3203d96af0
4 changed files with 81 additions and 2 deletions
|
|
@ -37,6 +37,8 @@ class DataFrame:
|
||||||
text = (re.sub('[-\W]+', ' ', text))
|
text = (re.sub('[-\W]+', ' ', text))
|
||||||
text = (re.sub('(?<=\d) (?=\d)', '', text))
|
text = (re.sub('(?<=\d) (?=\d)', '', text))
|
||||||
text = (re.sub("([a-z]\d+)|(\d+)", '', text))
|
text = (re.sub("([a-z]\d+)|(\d+)", '', text))
|
||||||
|
|
||||||
|
|
||||||
return ''.join(
|
return ''.join(
|
||||||
c for c in unicodedata.normalize('NFD', text)
|
c for c in unicodedata.normalize('NFD', text)
|
||||||
if unicodedata.category(c) != 'Mn')
|
if unicodedata.category(c) != 'Mn')
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ import torch
|
||||||
import random
|
import random
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import matplotlib.ticker as ticker
|
||||||
|
|
||||||
from NN import RNN
|
from NN import RNN
|
||||||
|
|
||||||
class Train():
|
class Train():
|
||||||
|
|
@ -102,4 +105,51 @@ class Train():
|
||||||
|
|
||||||
if epoch % 1000 == 0:
|
if epoch % 1000 == 0:
|
||||||
self.all_losses.append(self.current_loss / 1000)
|
self.all_losses.append(self.current_loss / 1000)
|
||||||
self.current_loss = 0
|
self.current_loss = 0
|
||||||
|
|
||||||
|
# torch.save(self.rnn,'ngram-rnn-classification.pt')
|
||||||
|
|
||||||
|
|
||||||
|
def plot(data):
|
||||||
|
plt.figure()
|
||||||
|
plt.plot(data)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def confusionMatix(self,len):
|
||||||
|
# Keep track of correct guesses in a confusion matrix
|
||||||
|
confusion = torch.zeros(len, len)
|
||||||
|
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 = self.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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
27
rm_clasificationmodel/evaluate.py
Normal file
27
rm_clasificationmodel/evaluate.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
class Evaluate():
|
||||||
|
def __init__(self,rnn):
|
||||||
|
self.rnn=rnn
|
||||||
|
|
||||||
|
# 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 predict(self,line, n_predictions=3):
|
||||||
|
# output = self.evaluate((lineToTensor(line)))
|
||||||
|
|
||||||
|
# # Get top N categories
|
||||||
|
# topv, topi = output.data.topk(n_predictions, 1, True)
|
||||||
|
# predictions = []
|
||||||
|
|
||||||
|
# for i in range(n_predictions):
|
||||||
|
# value = topv[0][i]
|
||||||
|
# category_index = topi[0][i]
|
||||||
|
# print('(%.2f) %s' % (value, self.all_categories[category_index]))
|
||||||
|
# predictions.append([value, self.all_categories[category_index]])
|
||||||
|
|
||||||
|
# return predictions
|
||||||
|
|
@ -30,7 +30,7 @@ def main():
|
||||||
# bool_cat_series = pd.isnull(df_eng["name"])
|
# bool_cat_series = pd.isnull(df_eng["name"])
|
||||||
# print(df_eng[bool_cat_series])
|
# print(df_eng[bool_cat_series])
|
||||||
inputSize=vectorizer.getTransformedVectorSize()
|
inputSize=vectorizer.getTransformedVectorSize()
|
||||||
n_hidden = 256*3
|
n_hidden = 256*6
|
||||||
df_category = df_en.groupby('category')
|
df_category = df_en.groupby('category')
|
||||||
all_category = list(df_category.groups.keys())
|
all_category = list(df_category.groups.keys())
|
||||||
n_category = len(all_category)
|
n_category = len(all_category)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue