Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions dice_ml/explainer_interfaces/dice_genetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion dice_ml/explainer_interfaces/explainer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down