diff --git a/doc/References.bib b/doc/References.bib index 0a677d2..e0fbb92 100644 --- a/doc/References.bib +++ b/doc/References.bib @@ -202,10 +202,9 @@ @misc{Paszke.03122019, abstract = {Deep learning frameworks have often focused on either usability or speed, but not both. PyTorch is a machine learning library that shows that these two goals are in fact compatible: it provides an imperative and Pythonic programming style that supports code as a model, makes debugging easy and is consistent with other popular scientific computing libraries, while remaining efficient and supporting hardware accelerators such as GPUs. In this paper, we detail the principles that drove the implementation of PyTorch and how they are reflected in its architecture. We emphasize that every aspect of PyTorch is a regular Python program under the full control of its user. We also explain how the careful and pragmatic implementation of the key components of its runtime enables them to work together to achieve compelling performance. We demonstrate the efficiency of individual subsystems, as well as the overall speed of PyTorch on several common benchmarks.}, author = {Paszke, Adam and Gross, Sam and Massa, Francisco and Lerer, Adam and Bradbury, James and Chanan, Gregory and Killeen, Trevor and Lin, Zeming and Gimelshein, Natalia and Antiga, Luca and Desmaison, Alban and K{\"o}pf, Andreas and Yang, Edward and DeVito, Zach and Raison, Martin and Tejani, Alykhan and Chilamkurthy, Sasank and Steiner, Benoit and Fang, Lu and Bai, Junjie and Chintala, Soumith}, - date = {03/12/2019}, + year = {2019}, title = {PyTorch: An Imperative Style, High-Performance Deep Learning Library}, url = {https://arxiv.org/pdf/1912.01703.pdf}, - urldate = {30/06/2023}, file = {1912.01703:Attachments/1912.01703.pdf:application/pdf} } diff --git a/rm_clasificationmodel/TrainClass.py b/rm_clasificationmodel/TrainClass.py index 5939f4e..cb6c2ec 100644 --- a/rm_clasificationmodel/TrainClass.py +++ b/rm_clasificationmodel/TrainClass.py @@ -76,70 +76,3 @@ class Train(): self.helper.plot(self.all_losses,"loss.png") torch.save(self.rnn,'ngram-rnn-classification.pt') - - - # 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,name, n_predictions=1): - # print('\n> %s' % name) - # with torch.no_grad(): - # output = self.evaluate(self.helper.nameToTensor(name,self.vectorizer)) - - # # Get top N categories - # topv, topi = output.topk(n_predictions, 1, True) - # predictions = [] - - # for i in range(n_predictions): - # value = topv[0][i].item() - # category_index = topi[0][i].item() - # print('(%.2f) %s' % (value, self.all_category[category_index])) - # predictions.append([value, self.all_category[category_index]]) - - # def confusionMatix(self): - # # Keep track of correct guesses in a confusion matrix - - # n_categories = len(self.all_category) - # 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.helper.randomTrainingExample() - # output = self.evaluate(name_tensor) - # guess, guess_i = self.helper.categoryFromOutput(output) - # category_i =self.all_category.index(category) - # confusion[category_i][guess_i] += 1 - - # # Normalize by dividing every row by its sum - # for i in range(n_categories): - # 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_category, rotation=45) - # ax.set_yticklabels([''] + self.all_category) - - # # 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() - # plt.savefig('confusion.png', dpi=400) - - - \ No newline at end of file diff --git a/rm_clasificationmodel/main.py b/rm_clasificationmodel/main.py index 7e45d95..1888a64 100644 --- a/rm_clasificationmodel/main.py +++ b/rm_clasificationmodel/main.py @@ -15,32 +15,34 @@ def main(): es =ElasticSearchDb("http://172.17.212.35:9200") df=DataFrame(es) - dfProductTaxonomyEn=df.getProductTaxonomy(0,10000) - # df_en = df.getNormal() + # dfProductTaxonomyEn=df.getProductTaxonomy(0,10000) + df_en = df.getNormal() # train = Train(df_en) - # train.run(200000,20000,10000) + # train.run(300000,30000,3000) predict = Predict() - # df_prediction=pd.DataFrame(columns=["id",'name','category','predicted','value']) - # list_row_en = dict (id=None,name=None,category=None,predicted=None,value=None) - list =[] - for index, row in dfProductTaxonomyEn.iterrows(): - predicted =predict.start(row["name"].lower()) - if(predicted[0][1]!=row["category"].lower()): - list_row = dict (id=None,name=None,category=None,predicted=None,value=None) - list_row["id"] =row["id"] - list_row["name"] =row["name"] - list_row["category"] =row["category"] - list_row["predicted"] =predicted[0][1] - list_row["value"] =predicted[0][0] + predict.confusionMatix(df_en) + # # df_prediction=pd.DataFrame(columns=["id",'name','category','predicted','value']) + # # list_row_en = dict (id=None,name=None,category=None,predicted=None,value=None) + # list =[] + # for index, row in dfProductTaxonomyEn.iterrows(): + # predicted =predict.start(row["name"].lower()) - list.append(list_row) + # if(predicted[0][1]!=row["category"].lower()): + # list_row = dict (id=None,name=None,category=None,predicted=None,value=None) + # list_row["id"] =row["id"] + # list_row["name"] =row["name"] + # list_row["category"] =row["category"] + # list_row["predicted"] =predicted[0][1] + # list_row["value"] =predicted[0][0] + + # list.append(list_row) - # new_row = pd.Series(list_row) - # df_prediction=pd.concat([df_prediction, new_row.to_frame().T], ignore_index=True) - es.createIndexerPrediction(list) + # # new_row = pd.Series(list_row) + # # df_prediction=pd.concat([df_prediction, new_row.to_frame().T], ignore_index=True) + # es.createIndexerPrediction(list) if __name__=="__main__": diff --git a/rm_clasificationmodel/predict.py b/rm_clasificationmodel/predict.py index f17c523..c014552 100644 --- a/rm_clasificationmodel/predict.py +++ b/rm_clasificationmodel/predict.py @@ -1,6 +1,8 @@ +from matplotlib import ticker from NN import * import sys from helper import * +from data import * class Predict(): def __init__(self): @@ -9,12 +11,7 @@ class Predict(): def evaluate(self,tensor): hidden = self.rnn.initHidden() - - # for i in range(tensor.size()[0]): - # output, hidden = self.rnn(tensor[i], hidden) output, hidden = self.rnn(tensor, hidden) - - return output def start(self,name, n_predictions=1): @@ -33,4 +30,49 @@ class Predict(): predictions.append([value, self.helper.getCategoryByIndex(category_index)]) return predictions + + def confusionMatix(self,df_en): + # Keep track of correct guesses in a confusion matrix + data = Data(df_en) + n_categories = len(data.all_category) + + batch = random.choices(data.all_category,k=20) + + 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 = data.randomTrainingExample() + if category in batch: + output = self.evaluate(name_tensor) + guess, guess_i = data.categoryFromOutput(output) + category_i =data.all_category.index(category) + confusion[category_i][guess_i] += 1 + + # Normalize by dividing every row by its sum + for i in range(20): + 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([''] + data.all_category, rotation=45) + ax.set_yticklabels([''] + data.all_category) + + # 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() + plt.savefig('confusion.png', dpi=400) + + +