Skip to content

Commit

Permalink
Reduce number inferences in dice random (#127)
Browse files Browse the repository at this point in the history
* Redice number of inference calls DiceRandom

Signed-off-by: gaugup <gaugup@microsoft.com>

* Added comment

Signed-off-by: gaugup <gaugup@microsoft.com>

* corrected typo to model_predictions

Co-authored-by: Amit Sharma <amit_sharma@live.com>
  • Loading branch information
gaugup and amit-sharma committed Apr 28, 2021
1 parent e69f7ca commit 509802d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dice_ml/explainer_interfaces/dice_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ 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.
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 = self.predict_fn(query_instance).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 = self.predict_fn(query_instance)[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':
Expand Down

0 comments on commit 509802d

Please sign in to comment.