diff --git a/confusion.png b/confusion.png index 99a9385..28a849f 100644 Binary files a/confusion.png and b/confusion.png differ diff --git a/doc/images/confusion.png b/doc/images/confusion.png new file mode 100644 index 0000000..99a9385 Binary files /dev/null and b/doc/images/confusion.png differ diff --git a/doc/images/histogram.png b/doc/images/histogram.png new file mode 100644 index 0000000..c094183 Binary files /dev/null and b/doc/images/histogram.png differ diff --git a/doc/page.tex b/doc/page.tex index 0bf893f..f8826a7 100644 --- a/doc/page.tex +++ b/doc/page.tex @@ -103,7 +103,7 @@ \item Preprocessing of the product and category data frame is performed, which involves step:- \begin{enumerate} \item Normalization of text: lowercase, perform Tf-Idf, ngram vectorization to extract text. - \item To change \"A to A, the Normal form D (NFD) should be applied which translated each character into its decomposed form. + \item To change \"A to A, the Normal form D (NFD) should be applied which translates each character into its decomposed form. \item Use Beautiful Soup a Pyhon library for pulling data out of HTLM and XML. \end{enumerate} \item The developed text based classification model successfully preditcs the product taxonomy based on the product features. @@ -125,6 +125,8 @@ \end{center} + \includegraphics{histogram} + \end{multicols} diff --git a/histogram.png b/histogram.png index c094183..fe74cd2 100644 Binary files a/histogram.png and b/histogram.png differ diff --git a/ngram-rnn-classification.pt b/ngram-rnn-classification.pt index 14ea91c..83f6829 100644 Binary files a/ngram-rnn-classification.pt and b/ngram-rnn-classification.pt differ diff --git a/rm_clasificationmodel/TrainClass.py b/rm_clasificationmodel/TrainClass.py index 369da6e..783dfca 100644 --- a/rm_clasificationmodel/TrainClass.py +++ b/rm_clasificationmodel/TrainClass.py @@ -5,24 +5,35 @@ import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as ticker +from VectorizeClass import Vectorize from NN import RNN class Train(): - def __init__(self,vectorizer,df_en,all_category,rnn): - # self.rnn = rnn + def __init__(self,df_en,withSaved=False,useSubset=False): + self.df_en=df_en + self.useSubset=useSubset + self.vectorizer = Vectorize(1,1) + self.vectorizer.fit(doc=df_en["name"]) + self.vectorizer.transform(df_en["name"]) + self.inputSize=self.vectorizer.getTransformedVectorSize() + self.n_hidden = 256*4 + + self.df_category = self.df_en.groupby('category') + self.all_category = list(self.df_category.groups.keys()) + if self.useSubset: + self.all_category=random.choices(self.all_category,20) + + self.n_category = len(self.all_category) + self.rnn=RNN(self.inputSize, self.n_hidden, self.n_category) if withSaved else torch.load('ngram-rnn-classification.pt') self.learning_rate=0.005 self.criterion = nn.NLLLoss() - self.vectorizer =vectorizer - self.df_en=df_en - self.all_category=all_category - self.df_category = self.df_en.groupby('category') - self.inputSize = vectorizer.getTransformedVectorSize() - self.rnn = rnn + self.df_category = self.df_en.groupby('category') self.current_loss = 0 self.all_losses = [] - + + def train(self,category_tensor, name_tensor): @@ -50,7 +61,7 @@ class Train(): def randomTrainingExample(self): - + randcategory = random.choice(self.all_category) # get feature name from the category random_feature_indices = self.df_category.indices[randcategory] @@ -104,7 +115,7 @@ class Train(): correct)) if epoch % 5000 == 0: - self.all_losses.append(self.current_loss / 1000) + self.all_losses.append(self.current_loss / 5000) self.current_loss = 0 self.plot() @@ -128,13 +139,19 @@ class Train(): def confusionMatix(self): # Keep track of correct guesses in a confusion matrix - n_categories = len(self.all_category) + # Get only 30 categories + + + + + + n_categories = len(self.all_category_subset) 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 = self.randomTrainingExample() + category, name, category_tensor, name_tensor = self.randomTrainingExample(True) output = self.evaluate(name_tensor) guess, guess_i = self.categoryFromOutput(output) category_i =self.all_category.index(category) diff --git a/rm_clasificationmodel/main.py b/rm_clasificationmodel/main.py index 69c907d..7d9ecbe 100644 --- a/rm_clasificationmodel/main.py +++ b/rm_clasificationmodel/main.py @@ -15,41 +15,37 @@ def main(): [df_en, df_de]=df.create() - vectorizer = Vectorize(1,1) + # vectorizer = Vectorize(1,1) # vectorizer.setTfidFitTransform(df_en["name"]) # x =vectorizer.getTfidFitTransform() # print(x.shape) - vectorizer.fit(doc=df_en["name"]) + # vectorizer.fit(doc=df_en["name"]) - vectorizer.transform(df_en["name"]) + # vectorizer.transform(df_en["name"]) # df =df_eng["category"].isnull() # bool_name_series = pd.notnull(df_eng["name"]) # bool_cat_series = pd.isnull(df_eng["name"]) # print(df_eng[bool_cat_series]) - inputSize=vectorizer.getTransformedVectorSize() - 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) + # inputSize=vectorizer.getTransformedVectorSize() + # 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=torch.load('ngram-rnn-classification.pt') - train = Train(vectorizer,df_en,all_category,rnn) + train = Train(df_en,True) train.confusionMatix() - - # train.run(200000) - - if __name__=="__main__": main() \ No newline at end of file