From 7d52d0629b6fe3e890f46c2ed4bc85c23d6aa60b Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 19:05:31 +0200 Subject: [PATCH 01/13] Added Predictor code --- run.py | 4 +++ testing/__init__.py | 2 ++ testing/intent_predictor.py | 53 ++++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 run.py diff --git a/run.py b/run.py new file mode 100644 index 0000000..8e616e0 --- /dev/null +++ b/run.py @@ -0,0 +1,4 @@ +import classifier +import testing.intent_predictor as pred + +pred.load_model_and_run_predictions("/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4") \ No newline at end of file diff --git a/testing/__init__.py b/testing/__init__.py index e69de29..3fd94c3 100644 --- a/testing/__init__.py +++ b/testing/__init__.py @@ -0,0 +1,2 @@ +from utils.reader import * +from classifier.nn import * \ No newline at end of file diff --git a/testing/intent_predictor.py b/testing/intent_predictor.py index dd401e9..16d2603 100644 --- a/testing/intent_predictor.py +++ b/testing/intent_predictor.py @@ -1,17 +1,62 @@ +from typing import Dict, List, Tuple + from allennlp.common import JsonDict from allennlp.data import Instance from allennlp.predictors import Predictor from overrides import overrides +from allennlp.models import Model +from allennlp.data.dataset_readers import DatasetReader +from allennlp.models.archival import load_archive +from utils.reader import DataReaderJsonLines, CitationDataSetReader + +import os @Predictor.register('citation_intent_predictor') class IntentClassificationPredictor(Predictor): """"Predictor for Citation Intent Classifier""" + def predict(self, text: str, intent: str): + return self.predict_json({"citation_text": text, "intent": intent}) + @overrides def _json_to_instance(self, json_dict: JsonDict) -> Instance: - pass + return self._dataset_reader.text_to_instance(json_dict["citation_text"], json_dict["intent"]) - @overrides - def predict_json(self, inputs: JsonDict) -> JsonDict: - pass + +def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str) -> Tuple[ + List[Dict[str, float]], list]: + """Make predictions using the given model and dataset reader""" + + predictor = IntentClassificationPredictor(model, dataset_reader) + + prediction_list = [] + true_list = [] + + vocab = model.vocab + + jsonl_reader = DataReaderJsonLines(file_path) + i = 0 + for citation in jsonl_reader.read(): + i += 1 + true_list.append(citation.intent) + output = predictor.predict(citation.text, citation.intent) + prediction_list.append({vocab.get_token_from_index(label_id, 'labels'): prob + for label_id, prob in enumerate(output['probs'])}) + if i == 10: + break + + return prediction_list, true_list + + +def load_model_and_run_predictions(saved_model_dir: str): + project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + train_file_path = project_root + '/data/tsv/train.tsv' + test_file_path = project_root + '/data/tsv/test.tsv' + + model_archive = load_archive(os.path.join(saved_model_dir, 'model.tar.gz')) + citation_dataset_reader = CitationDataSetReader() + + y_pred, y_true = make_predictions(model_archive.model, citation_dataset_reader, test_file_path) + + print(y_pred) From 3916df452f252ef7883ee0ae9753d07fc51b5fe5 Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 19:16:00 +0200 Subject: [PATCH 02/13] Added JSONL open check --- utils/reader.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/reader.py b/utils/reader.py index ce606da..c208eac 100644 --- a/utils/reader.py +++ b/utils/reader.py @@ -75,8 +75,9 @@ class DataReaderJsonLines: This method opens the file, reads every line and returns a collection of lines :return: collection of Citation Objects, with the required data """ - for line in jsonlines.open(self.file_path): - yield read_json_line(line) + with jsonlines.open(self.file_path) as jl_reader: + for line in jl_reader: + yield read_json_line(line) def read_json_line(line): From 3455af4e400831dfed102271c29c6dac0f755480 Mon Sep 17 00:00:00 2001 From: Sai Pavan Mandava Date: Wed, 29 Jul 2020 19:21:56 +0200 Subject: [PATCH 03/13] Wrong Test file path fixed --- configs/basic_model.json | 6 +++--- testing/intent_predictor.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/configs/basic_model.json b/configs/basic_model.json index 55d9af8..55fac2a 100644 --- a/configs/basic_model.json +++ b/configs/basic_model.json @@ -48,10 +48,10 @@ }, "trainer": { "optimizer": { - "type": "adam", + "type": "adagrad", "lr": 0.001 }, - "num_epochs": 2, - "cuda_device": 1 + "num_epochs": 20, + "cuda_device": 3 } } diff --git a/testing/intent_predictor.py b/testing/intent_predictor.py index 16d2603..d1541d3 100644 --- a/testing/intent_predictor.py +++ b/testing/intent_predictor.py @@ -51,8 +51,8 @@ def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str def load_model_and_run_predictions(saved_model_dir: str): project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - train_file_path = project_root + '/data/tsv/train.tsv' - test_file_path = project_root + '/data/tsv/test.tsv' + dev_file_path = project_root + '/data/jsonl/dev.jsonl' + test_file_path = project_root + '/data/jsonl/test.jsonl' model_archive = load_archive(os.path.join(saved_model_dir, 'model.tar.gz')) citation_dataset_reader = CitationDataSetReader() From 6946077d940cde0a43d26c2ce7e4db477e7096ac Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 19:46:20 +0200 Subject: [PATCH 04/13] Commented unnecessary code --- classifier/nn.py | 8 ++++---- configs/basic_model.json | 4 ++-- testing/intent_predictor.py | 8 +++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/classifier/nn.py b/classifier/nn.py index 8550ccb..1f0f51e 100644 --- a/classifier/nn.py +++ b/classifier/nn.py @@ -104,10 +104,10 @@ class BiLstmClassifier(Model): output_dict['probabilities'] = class_probabilities output_dict['positive_label'] = label output_dict['prediction'] = label - citation_text = [] - for batch_text in output_dict['tokens']: - citation_text.append([self.vocab.get_token_from_index(token_id.item()) for token_id in batch_text]) - output_dict['tokens'] = citation_text + # citation_text = [] + # for batch_text in output_dict['tokens']: + # citation_text.append([self.vocab.get_token_from_index(token_id.item()) for token_id in batch_text]) + # output_dict['tokens'] = citation_text return output_dict diff --git a/configs/basic_model.json b/configs/basic_model.json index 55fac2a..e802261 100644 --- a/configs/basic_model.json +++ b/configs/basic_model.json @@ -49,9 +49,9 @@ "trainer": { "optimizer": { "type": "adagrad", - "lr": 0.001 + "lr": 0.005 }, - "num_epochs": 20, + "num_epochs": 10, "cuda_device": 3 } } diff --git a/testing/intent_predictor.py b/testing/intent_predictor.py index d1541d3..f7574cb 100644 --- a/testing/intent_predictor.py +++ b/testing/intent_predictor.py @@ -41,8 +41,9 @@ def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str i += 1 true_list.append(citation.intent) output = predictor.predict(citation.text, citation.intent) - prediction_list.append({vocab.get_token_from_index(label_id, 'labels'): prob - for label_id, prob in enumerate(output['probs'])}) + prediction_list.append(output['prediction']) + # prediction_list.append({vocab.get_token_from_index(label_id, 'labels'): prob + # for label_id, prob in enumerate(output['probabilities'])}) if i == 10: break @@ -59,4 +60,5 @@ def load_model_and_run_predictions(saved_model_dir: str): y_pred, y_true = make_predictions(model_archive.model, citation_dataset_reader, test_file_path) - print(y_pred) + print('Predictions ', y_pred) + print('True Labels ', y_true) From 64f787b5d7635696fae5f64e2c9ce9bffee1cdfb Mon Sep 17 00:00:00 2001 From: Sai Pavan Mandava Date: Wed, 29 Jul 2020 19:56:28 +0200 Subject: [PATCH 05/13] Removed Debug Code and Prints --- run.py | 2 +- testing/intent_predictor.py | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/run.py b/run.py index 8e616e0..f80a37b 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,4 @@ import classifier import testing.intent_predictor as pred -pred.load_model_and_run_predictions("/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4") \ No newline at end of file +y_pred, y_true = pred.load_model_and_predict_test_data("/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4") diff --git a/testing/intent_predictor.py b/testing/intent_predictor.py index f7574cb..4e00735 100644 --- a/testing/intent_predictor.py +++ b/testing/intent_predictor.py @@ -36,21 +36,15 @@ def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str vocab = model.vocab jsonl_reader = DataReaderJsonLines(file_path) - i = 0 for citation in jsonl_reader.read(): - i += 1 true_list.append(citation.intent) output = predictor.predict(citation.text, citation.intent) prediction_list.append(output['prediction']) - # prediction_list.append({vocab.get_token_from_index(label_id, 'labels'): prob - # for label_id, prob in enumerate(output['probabilities'])}) - if i == 10: - break return prediction_list, true_list -def load_model_and_run_predictions(saved_model_dir: str): +def load_model_and_predict_test_data(saved_model_dir: str): project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) dev_file_path = project_root + '/data/jsonl/dev.jsonl' test_file_path = project_root + '/data/jsonl/test.jsonl' @@ -60,5 +54,4 @@ def load_model_and_run_predictions(saved_model_dir: str): y_pred, y_true = make_predictions(model_archive.model, citation_dataset_reader, test_file_path) - print('Predictions ', y_pred) - print('True Labels ', y_true) + retun y_pred,y_true From 98e582444bbaedea437edc531ca1684047de818d Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 20:22:02 +0200 Subject: [PATCH 06/13] added confusion matrix and plot --- eval/metrics.py | 30 ++++++++++++++++++++++++++++++ run.py | 4 ++++ testing/intent_predictor.py | 7 ++----- utils/constants.py | 1 + 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/eval/metrics.py b/eval/metrics.py index f844a3d..072fc5a 100644 --- a/eval/metrics.py +++ b/eval/metrics.py @@ -1,4 +1,6 @@ import utils.constants as const +from sklearn.metrics import confusion_matrix +import matplotlib.pyplot as plt def f1_score(y_true, y_pred, labels, average): @@ -163,6 +165,34 @@ def calculate_f1_score(precision, recall): return 2 * (precision * recall) / (precision + recall) +def get_confusion_matrix(y_true, y_pred): + """ + takes predicted labels and true labels as parameters and returns Confusion Matrix + :param y_true: True labels + :param y_pred: Predicted labels + :return: returns Confusion Matrix + """ + return confusion_matrix(y_true, y_pred, const.CLASS_LABELS_LIST) + + +def plot_confusion_matrix(confusion_mat, classifier_name): + """ + Takes Confusion Matrix as a parameter and plots the matrix using matplotlib + :param confusion_mat: Confusion Matrix + :param classifier_name: Classifier Name to show it on the Top + """ + fig, ax = plt.subplots(2, 2) + ax.matshow(confusion_mat, cmap='Greens') + for x in (0, 2): + for y in (0, 2): + ax.text(x, y, confusion_mat[y, x]) + ax.set_xlabel('Predicted') + ax.set_ylabel('True/Gold') + ax.set_xticklabels([''] + const.CLASS_LABELS_LIST) + ax.set_yticklabels([''] + const.CLASS_LABELS_LIST) + ax.set_title(classifier_name) + + class Result: """ Model Class for carrying Evaluation Data (F1 Score, Precision, Recall, ....) diff --git a/run.py b/run.py index f80a37b..b002a47 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,8 @@ import classifier import testing.intent_predictor as pred +import eval.metrics as metrics + y_pred, y_true = pred.load_model_and_predict_test_data("/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4") + +metrics.plot_confusion_matrix(metrics.get_confusion_matrix(y_true, y_pred), "BiLSTM Classifier + Attention with ELMo") \ No newline at end of file diff --git a/testing/intent_predictor.py b/testing/intent_predictor.py index 4e00735..2c68414 100644 --- a/testing/intent_predictor.py +++ b/testing/intent_predictor.py @@ -24,8 +24,7 @@ class IntentClassificationPredictor(Predictor): return self._dataset_reader.text_to_instance(json_dict["citation_text"], json_dict["intent"]) -def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str) -> Tuple[ - List[Dict[str, float]], list]: +def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str) -> Tuple[List[Dict[str, float]], list]: """Make predictions using the given model and dataset reader""" predictor = IntentClassificationPredictor(model, dataset_reader) @@ -33,8 +32,6 @@ def make_predictions(model: Model, dataset_reader: DatasetReader, file_path: str prediction_list = [] true_list = [] - vocab = model.vocab - jsonl_reader = DataReaderJsonLines(file_path) for citation in jsonl_reader.read(): true_list.append(citation.intent) @@ -54,4 +51,4 @@ def load_model_and_predict_test_data(saved_model_dir: str): y_pred, y_true = make_predictions(model_archive.model, citation_dataset_reader, test_file_path) - retun y_pred,y_true + return y_pred, y_true diff --git a/utils/constants.py b/utils/constants.py index 5389eae..091ead2 100644 --- a/utils/constants.py +++ b/utils/constants.py @@ -34,3 +34,4 @@ REGEX_CONSTANTS = { } CLASS_LABELS = {"background": 0, "method": 1, "result": 2} +CLASS_LABELS_LIST = ['background', 'method', 'result'] From d18593f869c3048b917145c388e6ab28e5e9b2e6 Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 20:29:59 +0200 Subject: [PATCH 07/13] printing confusion matrix --- run.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/run.py b/run.py index b002a47..1cbccd2 100644 --- a/run.py +++ b/run.py @@ -5,4 +5,8 @@ import eval.metrics as metrics y_pred, y_true = pred.load_model_and_predict_test_data("/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4") -metrics.plot_confusion_matrix(metrics.get_confusion_matrix(y_true, y_pred), "BiLSTM Classifier + Attention with ELMo") \ No newline at end of file +confusion_matrix = metrics.get_confusion_matrix(y_true, y_pred) + +print(confusion_matrix) + +metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo") From 52efebe53ef3953c4b992856b88b4cea21378ef2 Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 20:47:09 +0200 Subject: [PATCH 08/13] WIP : Matplotlib confusion matrix plot has issues --- eval/metrics.py | 3 ++- run.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/eval/metrics.py b/eval/metrics.py index 072fc5a..f10ee8f 100644 --- a/eval/metrics.py +++ b/eval/metrics.py @@ -172,7 +172,7 @@ def get_confusion_matrix(y_true, y_pred): :param y_pred: Predicted labels :return: returns Confusion Matrix """ - return confusion_matrix(y_true, y_pred, const.CLASS_LABELS_LIST) + return confusion_matrix(y_true, y_pred, labels=const.CLASS_LABELS_LIST) def plot_confusion_matrix(confusion_mat, classifier_name): @@ -182,6 +182,7 @@ def plot_confusion_matrix(confusion_mat, classifier_name): :param classifier_name: Classifier Name to show it on the Top """ fig, ax = plt.subplots(2, 2) + plt.show() ax.matshow(confusion_mat, cmap='Greens') for x in (0, 2): for y in (0, 2): diff --git a/run.py b/run.py index 1cbccd2..1ee5a95 100644 --- a/run.py +++ b/run.py @@ -9,4 +9,4 @@ confusion_matrix = metrics.get_confusion_matrix(y_true, y_pred) print(confusion_matrix) -metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo") +# metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo") From 3089662a0aa9c792dc93020eb3561622164b31c9 Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 22:49:51 +0200 Subject: [PATCH 09/13] Improved plot confusion matrix --- eval/metrics.py | 41 ++++++++++++++++++++++++++--------------- run.py => predict.py | 2 +- 2 files changed, 27 insertions(+), 16 deletions(-) rename run.py => predict.py (77%) diff --git a/eval/metrics.py b/eval/metrics.py index f10ee8f..01597f1 100644 --- a/eval/metrics.py +++ b/eval/metrics.py @@ -1,6 +1,8 @@ import utils.constants as const from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt +import numpy as np +import itertools def f1_score(y_true, y_pred, labels, average): @@ -176,22 +178,31 @@ def get_confusion_matrix(y_true, y_pred): def plot_confusion_matrix(confusion_mat, classifier_name): - """ - Takes Confusion Matrix as a parameter and plots the matrix using matplotlib - :param confusion_mat: Confusion Matrix - :param classifier_name: Classifier Name to show it on the Top - """ - fig, ax = plt.subplots(2, 2) + + accuracy = np.trace(confusion_mat) / float(np.sum(confusion_mat)) + mis_class = 1 - accuracy + + plt.figure(figsize=(8, 6)) + plt.imshow(confusion_mat, interpolation='nearest', cmap=plt.get_cmap('Blues')) + plt.title(classifier_name) + plt.colorbar() + + target_names = const.CLASS_LABELS_LIST + if target_names is not None: + tick_marks = np.arange(len(target_names)) + plt.xticks(tick_marks, target_names, rotation=45) + plt.yticks(tick_marks, target_names) + + thresh = confusion_mat.max() / confusion_mat.max() / 2 + for i, j in itertools.product(range(confusion_mat.shape[0]), range(confusion_mat.shape[1])): + plt.text(j, i, "{:,}".format(confusion_mat[i, j]), + horizontalalignment="center", + color="white" if confusion_mat[i, j] > thresh else "black") + + plt.tight_layout() + plt.ylabel('True/Gold') + plt.xlabel('Predicted \nAccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, mis_class)) plt.show() - ax.matshow(confusion_mat, cmap='Greens') - for x in (0, 2): - for y in (0, 2): - ax.text(x, y, confusion_mat[y, x]) - ax.set_xlabel('Predicted') - ax.set_ylabel('True/Gold') - ax.set_xticklabels([''] + const.CLASS_LABELS_LIST) - ax.set_yticklabels([''] + const.CLASS_LABELS_LIST) - ax.set_title(classifier_name) class Result: diff --git a/run.py b/predict.py similarity index 77% rename from run.py rename to predict.py index 1ee5a95..1cbccd2 100644 --- a/run.py +++ b/predict.py @@ -9,4 +9,4 @@ confusion_matrix = metrics.get_confusion_matrix(y_true, y_pred) print(confusion_matrix) -# metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo") +metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo") From 244e27bee661a2884d9fbc993fe4961c9e41b4f9 Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 23:14:11 +0200 Subject: [PATCH 10/13] Added plot.show(block = True) --- eval/metrics.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/eval/metrics.py b/eval/metrics.py index 01597f1..9d2d520 100644 --- a/eval/metrics.py +++ b/eval/metrics.py @@ -179,9 +179,6 @@ def get_confusion_matrix(y_true, y_pred): def plot_confusion_matrix(confusion_mat, classifier_name): - accuracy = np.trace(confusion_mat) / float(np.sum(confusion_mat)) - mis_class = 1 - accuracy - plt.figure(figsize=(8, 6)) plt.imshow(confusion_mat, interpolation='nearest', cmap=plt.get_cmap('Blues')) plt.title(classifier_name) @@ -201,8 +198,8 @@ def plot_confusion_matrix(confusion_mat, classifier_name): plt.tight_layout() plt.ylabel('True/Gold') - plt.xlabel('Predicted \nAccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, mis_class)) - plt.show() + plt.xlabel('Predicted') + plt.show(block=True) class Result: From 87efce8f82937bfc68954e941aebef92232212fd Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 23:45:24 +0200 Subject: [PATCH 11/13] Saving Confusion Matrix Plot PNG --- eval/metrics.py | 6 +++--- predict.py | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/eval/metrics.py b/eval/metrics.py index 9d2d520..1b9cf73 100644 --- a/eval/metrics.py +++ b/eval/metrics.py @@ -177,7 +177,7 @@ def get_confusion_matrix(y_true, y_pred): return confusion_matrix(y_true, y_pred, labels=const.CLASS_LABELS_LIST) -def plot_confusion_matrix(confusion_mat, classifier_name): +def plot_confusion_matrix(confusion_mat, classifier_name, plot_file_name): plt.figure(figsize=(8, 6)) plt.imshow(confusion_mat, interpolation='nearest', cmap=plt.get_cmap('Blues')) @@ -196,10 +196,10 @@ def plot_confusion_matrix(confusion_mat, classifier_name): horizontalalignment="center", color="white" if confusion_mat[i, j] > thresh else "black") - plt.tight_layout() + plt.tight_layout(1.5) plt.ylabel('True/Gold') plt.xlabel('Predicted') - plt.show(block=True) + plt.savefig(plot_file_name) class Result: diff --git a/predict.py b/predict.py index 1cbccd2..c5c3a37 100644 --- a/predict.py +++ b/predict.py @@ -3,10 +3,11 @@ import testing.intent_predictor as pred import eval.metrics as metrics -y_pred, y_true = pred.load_model_and_predict_test_data("/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4") +model_path = '/mount/arbeitsdaten/studenten1/team-lab-nlp/mandavsi_rileyic/saved_models/experiment_4' +y_pred, y_true = pred.load_model_and_predict_test_data(model_path) confusion_matrix = metrics.get_confusion_matrix(y_true, y_pred) print(confusion_matrix) -metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo") +metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo", model_path+'/confusion_matrix_plot.png') From bd559e0d8afec4441b11f6926b6a0a37373f3fbc Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Wed, 29 Jul 2020 23:46:30 +0200 Subject: [PATCH 12/13] plot path print --- predict.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/predict.py b/predict.py index c5c3a37..394189c 100644 --- a/predict.py +++ b/predict.py @@ -10,4 +10,7 @@ confusion_matrix = metrics.get_confusion_matrix(y_true, y_pred) print(confusion_matrix) -metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo", model_path+'/confusion_matrix_plot.png') +plot_file_path = model_path+'/confusion_matrix_plot.png' +metrics.plot_confusion_matrix(confusion_matrix, "BiLSTM Classifier + Attention with ELMo", plot_file_path) + +print('Confusion Matrix Plot saved to :: ', plot_file_path) From 2b4b09864d17bfb0a5cbbad8039528606fc9e5ba Mon Sep 17 00:00:00 2001 From: Pavan Mandava Date: Thu, 30 Jul 2020 00:13:37 +0200 Subject: [PATCH 13/13] plot Threshold color fix --- eval/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eval/metrics.py b/eval/metrics.py index 1b9cf73..f7cdb4c 100644 --- a/eval/metrics.py +++ b/eval/metrics.py @@ -190,7 +190,7 @@ def plot_confusion_matrix(confusion_mat, classifier_name, plot_file_name): plt.xticks(tick_marks, target_names, rotation=45) plt.yticks(tick_marks, target_names) - thresh = confusion_mat.max() / confusion_mat.max() / 2 + thresh = confusion_mat.max() / 2 for i, j in itertools.product(range(confusion_mat.shape[0]), range(confusion_mat.shape[1])): plt.text(j, i, "{:,}".format(confusion_mat[i, j]), horizontalalignment="center",