diff --git a/dice_ml/explainer_interfaces/dice_genetic.py b/dice_ml/explainer_interfaces/dice_genetic.py index 0b1bc1b7..aec92790 100644 --- a/dice_ml/explainer_interfaces/dice_genetic.py +++ b/dice_ml/explainer_interfaces/dice_genetic.py @@ -458,7 +458,8 @@ def find_counterfactuals(self, query_instance, desired_range, desired_class, population_fitness = population_fitness[population_fitness[:, 1].argsort()] current_best_loss = population_fitness[0][1] - to_pred = np.array([population[int(tup[0])] for tup in population_fitness[:self.total_CFs]]) + to_pred = np.array([population[int(tup[0].item()) if hasattr(tup[0], 'item') else int(tup[0])] + for tup in population_fitness[:self.total_CFs]]) if self.total_CFs > 0: if self.model.model_type == ModelTypes.Classifier: @@ -468,7 +469,8 @@ def find_counterfactuals(self, query_instance, desired_range, desired_class, # self.total_CFS of the next generation obtained from the fittest members of current generation top_members = self.total_CFs - new_generation_1 = np.array([population[int(tup[0])] for tup in population_fitness[:top_members]]) + new_generation_1 = np.array([population[int(tup[0].item()) if hasattr(tup[0], 'item') else int(tup[0])] + for tup in population_fitness[:top_members]]) # rest of the next generation obtained from top 50% of fittest members of current generation rest_members = self.population_size - top_members diff --git a/dice_ml/explainer_interfaces/explainer_base.py b/dice_ml/explainer_interfaces/explainer_base.py index 59894f1f..f8dd9ee4 100644 --- a/dice_ml/explainer_interfaces/explainer_base.py +++ b/dice_ml/explainer_interfaces/explainer_base.py @@ -669,7 +669,10 @@ def misc_init(self, stopping_threshold, desired_class, desired_range, test_pred) self.target_cf_class = np.array( [[self.infer_target_cfs_class(desired_class, test_pred, self.num_output_nodes)]], dtype=np.float32) - desired_class = int(self.target_cf_class[0][0]) + target_cf_class_scalar = self.target_cf_class[0][0] + if hasattr(target_cf_class_scalar, "item"): + target_cf_class_scalar = target_cf_class_scalar.item() + desired_class = int(target_cf_class_scalar) if self.target_cf_class == 0 and self.stopping_threshold > 0.5: self.stopping_threshold = 0.25 elif self.target_cf_class == 1 and self.stopping_threshold < 0.5: @@ -692,12 +695,16 @@ def infer_target_cfs_class(self, desired_class_input, original_pred, num_output_ # already contains the class. if hasattr(original_pred, "__len__") and len(original_pred) > 1: original_pred_1 = np.argmax(original_pred) + if hasattr(original_pred_1, "item"): + original_pred_1 = original_pred_1.item() else: original_pred_1 = original_pred target_class = int(1 - original_pred_1) return target_class elif num_output_nodes == 1: # only for pytorch DL model original_pred_1 = np.round(original_pred) + if hasattr(original_pred_1, "item"): + original_pred_1 = original_pred_1.item() target_class = int(1-original_pred_1) return target_class elif num_output_nodes > 2: @@ -768,6 +775,8 @@ def is_cf_valid(self, model_score): target_cf_class = self.target_cf_class[0] elif len(self.target_cf_class.shape) == 2: target_cf_class = self.target_cf_class[0][0] + if hasattr(target_cf_class, "item"): + target_cf_class = target_cf_class.item() target_cf_class = int(target_cf_class) if len(model_score) == 1: # for tensorflow/pytorch models