diff --git a/rm_clasificationmodel/DataFrameClass.py b/rm_clasificationmodel/DataFrameClass.py index a6de64b..efc40b0 100644 --- a/rm_clasificationmodel/DataFrameClass.py +++ b/rm_clasificationmodel/DataFrameClass.py @@ -37,6 +37,8 @@ class DataFrame: text = (re.sub('[-\W]+', ' ', text)) text = (re.sub('(?<=\d) (?=\d)', '', text)) text = (re.sub("([a-z]\d+)|(\d+)", '', text)) + + return ''.join( c for c in unicodedata.normalize('NFD', text) if unicodedata.category(c) != 'Mn') diff --git a/rm_clasificationmodel/TrainClass.py b/rm_clasificationmodel/TrainClass.py index 734a181..bb3b08b 100644 --- a/rm_clasificationmodel/TrainClass.py +++ b/rm_clasificationmodel/TrainClass.py @@ -3,6 +3,9 @@ import torch import random import numpy as np +import matplotlib.pyplot as plt +import matplotlib.ticker as ticker + from NN import RNN class Train(): @@ -102,4 +105,51 @@ class Train(): if epoch % 1000 == 0: self.all_losses.append(self.current_loss / 1000) - self.current_loss = 0 \ No newline at end of file + 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() + + + diff --git a/rm_clasificationmodel/evaluate.py b/rm_clasificationmodel/evaluate.py new file mode 100644 index 0000000..af25e81 --- /dev/null +++ b/rm_clasificationmodel/evaluate.py @@ -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 diff --git a/rm_clasificationmodel/main.py b/rm_clasificationmodel/main.py index 809ffdf..bc04a08 100644 --- a/rm_clasificationmodel/main.py +++ b/rm_clasificationmodel/main.py @@ -30,7 +30,7 @@ def main(): # bool_cat_series = pd.isnull(df_eng["name"]) # print(df_eng[bool_cat_series]) inputSize=vectorizer.getTransformedVectorSize() - n_hidden = 256*3 + n_hidden = 256*6 df_category = df_en.groupby('category') all_category = list(df_category.groups.keys()) n_category = len(all_category)