From 3af0c46bc8dd5feac3fd9eb253cd10695453be80 Mon Sep 17 00:00:00 2001 From: gaugup Date: Sun, 25 Apr 2021 10:39:24 -0700 Subject: [PATCH 1/3] Redice number of inference calls DiceRandom Signed-off-by: gaugup --- dice_ml/explainer_interfaces/dice_random.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dice_ml/explainer_interfaces/dice_random.py b/dice_ml/explainer_interfaces/dice_random.py index 6fd65a5f..cb3b5afe 100644 --- a/dice_ml/explainer_interfaces/dice_random.py +++ b/dice_ml/explainer_interfaces/dice_random.py @@ -63,14 +63,16 @@ def _generate_counterfactuals(self, query_instance, total_CFs, desired_range, d else: # compute the new ranges based on user input self.feature_range, feature_ranges_orig = self.data_interface.get_features_range(permitted_range) + modlel_predictions = self.predict_fn(query_instance) + # number of output nodes of ML model self.num_output_nodes = None if self.model.model_type == "classifier": - self.num_output_nodes = self.predict_fn(query_instance).shape[1] + self.num_output_nodes = modlel_predictions.shape[1] # query_instance need no transformation for generating CFs using random sampling. # find the predicted value of query_instance - test_pred = self.predict_fn(query_instance)[0] + test_pred = modlel_predictions[0] if self.model.model_type == 'classifier': self.target_cf_class = self.infer_target_cfs_class(desired_class, test_pred, self.num_output_nodes) elif self.model.model_type == 'regressor': From c1d485943112f7bc9887e7ecf948e3a8566ef701 Mon Sep 17 00:00:00 2001 From: gaugup Date: Sun, 25 Apr 2021 16:08:31 -0700 Subject: [PATCH 2/3] Added comment Signed-off-by: gaugup --- dice_ml/explainer_interfaces/dice_random.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dice_ml/explainer_interfaces/dice_random.py b/dice_ml/explainer_interfaces/dice_random.py index cb3b5afe..0cb2b9c7 100644 --- a/dice_ml/explainer_interfaces/dice_random.py +++ b/dice_ml/explainer_interfaces/dice_random.py @@ -63,6 +63,8 @@ def _generate_counterfactuals(self, query_instance, total_CFs, desired_range, d else: # compute the new ranges based on user input self.feature_range, feature_ranges_orig = self.data_interface.get_features_range(permitted_range) + # Do predictions once on the query_instance and reuse across to reduce the number + # inferences. modlel_predictions = self.predict_fn(query_instance) # number of output nodes of ML model From d3f6a6bb6598b0308ce5b97d12e0e77a7b3a84b5 Mon Sep 17 00:00:00 2001 From: Amit Sharma Date: Tue, 27 Apr 2021 15:08:56 +0530 Subject: [PATCH 3/3] corrected typo to model_predictions --- dice_ml/explainer_interfaces/dice_random.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dice_ml/explainer_interfaces/dice_random.py b/dice_ml/explainer_interfaces/dice_random.py index 0cb2b9c7..5fe72e79 100644 --- a/dice_ml/explainer_interfaces/dice_random.py +++ b/dice_ml/explainer_interfaces/dice_random.py @@ -65,16 +65,16 @@ def _generate_counterfactuals(self, query_instance, total_CFs, desired_range, d # Do predictions once on the query_instance and reuse across to reduce the number # inferences. - modlel_predictions = self.predict_fn(query_instance) + model_predictions = self.predict_fn(query_instance) # number of output nodes of ML model self.num_output_nodes = None if self.model.model_type == "classifier": - self.num_output_nodes = modlel_predictions.shape[1] + self.num_output_nodes = model_predictions.shape[1] # query_instance need no transformation for generating CFs using random sampling. # find the predicted value of query_instance - test_pred = modlel_predictions[0] + test_pred = model_predictions[0] if self.model.model_type == 'classifier': self.target_cf_class = self.infer_target_cfs_class(desired_class, test_pred, self.num_output_nodes) elif self.model.model_type == 'regressor':