add sub set for testing

This commit is contained in:
shoney 2023-06-30 22:18:02 +00:00
parent 2e71463618
commit c9e3017d41
8 changed files with 43 additions and 28 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 456 KiB

After

Width:  |  Height:  |  Size: 459 KiB

BIN
doc/images/confusion.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 456 KiB

BIN
doc/images/histogram.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

View file

@ -103,7 +103,7 @@
\item Preprocessing of the product and category data frame is performed, which involves step:- \item Preprocessing of the product and category data frame is performed, which involves step:-
\begin{enumerate} \begin{enumerate}
\item Normalization of text: lowercase, perform Tf-Idf, ngram vectorization to extract text. \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. \item Use Beautiful Soup a Pyhon library for pulling data out of HTLM and XML.
\end{enumerate} \end{enumerate}
\item The developed text based classification model successfully preditcs the product taxonomy based on the product features. \item The developed text based classification model successfully preditcs the product taxonomy based on the product features.
@ -125,6 +125,8 @@
\end{center} \end{center}
\includegraphics{histogram}
\end{multicols} \end{multicols}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 87 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

View file

@ -5,24 +5,35 @@ import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.ticker as ticker import matplotlib.ticker as ticker
from VectorizeClass import Vectorize
from NN import RNN from NN import RNN
class Train(): class Train():
def __init__(self,vectorizer,df_en,all_category,rnn): def __init__(self,df_en,withSaved=False,useSubset=False):
# self.rnn = rnn 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.learning_rate=0.005
self.criterion = nn.NLLLoss() 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.current_loss = 0
self.all_losses = [] self.all_losses = []
def train(self,category_tensor, name_tensor): def train(self,category_tensor, name_tensor):
@ -50,7 +61,7 @@ class Train():
def randomTrainingExample(self): def randomTrainingExample(self):
randcategory = random.choice(self.all_category) randcategory = random.choice(self.all_category)
# get feature name from the category # get feature name from the category
random_feature_indices = self.df_category.indices[randcategory] random_feature_indices = self.df_category.indices[randcategory]
@ -104,7 +115,7 @@ class Train():
correct)) correct))
if epoch % 5000 == 0: 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.current_loss = 0
self.plot() self.plot()
@ -128,13 +139,19 @@ class Train():
def confusionMatix(self): def confusionMatix(self):
# Keep track of correct guesses in a confusion matrix # 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) confusion = torch.zeros(n_categories, n_categories)
n_confusion = 10000 n_confusion = 10000
# Go through a bunch of examples and record which are correctly guessed # Go through a bunch of examples and record which are correctly guessed
for i in range(n_confusion): 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) output = self.evaluate(name_tensor)
guess, guess_i = self.categoryFromOutput(output) guess, guess_i = self.categoryFromOutput(output)
category_i =self.all_category.index(category) category_i =self.all_category.index(category)

View file

@ -15,41 +15,37 @@ def main():
[df_en, df_de]=df.create() [df_en, df_de]=df.create()
vectorizer = Vectorize(1,1) # vectorizer = Vectorize(1,1)
# vectorizer.setTfidFitTransform(df_en["name"]) # vectorizer.setTfidFitTransform(df_en["name"])
# x =vectorizer.getTfidFitTransform() # x =vectorizer.getTfidFitTransform()
# print(x.shape) # 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() # df =df_eng["category"].isnull()
# bool_name_series = pd.notnull(df_eng["name"]) # bool_name_series = pd.notnull(df_eng["name"])
# 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*4 # n_hidden = 256*4
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)
rnn=RNN(inputSize, n_hidden, n_category) # rnn=RNN(inputSize, n_hidden, n_category)
# rnn=torch.load('ngram-rnn-classification.pt') # rnn=torch.load('ngram-rnn-classification.pt')
train = Train(vectorizer,df_en,all_category,rnn) train = Train(df_en,True)
train.confusionMatix() train.confusionMatix()
# train.run(200000) # train.run(200000)
if __name__=="__main__": if __name__=="__main__":
main() main()