Open etheng-jiao opened 3 years ago
@etheng-jiao, can you share the sample code which generated this error and which version dice-ml are you using?
@etheng-jiao, BTW I tried to find the code line "temp_preds = self.model.get_output(input_instance).numpy()" in the DiCE code base and couldn't find it. It is possible that you are running an older version of dice-ml. Could you test with the release 0.6.0 of dice-ml?
Were you able to fix this? I'm getting the same error
@gaugup my code:
`import os from IPython.display import display import numpy as np import matplotlib.pyplot as plt import pandas as pd
import dice_ml import tensorflow as tf from dice_ml.utils import helpers # helper functions from keras import backend as K import tensorflow.compat.v1 as tf tf.disable_v2_behavior() tf.config.run_functions_eagerly(True)
import dice_ml from dice_ml.utils import helpers # helper functions
d = dice_ml.Data(dataframe=helpers.load_adult_income_dataset(), continuous_features=['age', 'hours_per_week'], outcome_name='income')
backend = 'TF'+tf.version[0] # TF2 in your case ML_modelpath = helpers.get_adult_income_modelpath(backend=backend) m = dice_ml.Model(model_path= ML_modelpath, backend=backend)
exp = dice_ml.Dice(d,m)
query_instance = {'age':22, 'workclass':'Private', 'education':'HS-grad', 'marital_status':'Single', 'occupation':'Service', 'race': 'White', 'gender':'Female', 'hours_per_week': 45} dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite") ` the full traceback:
`AttributeError Traceback (most recent call last)
3 frames /usr/local/lib/python3.7/dist-packages/dice_ml/explainer_interfaces/dice_tensorflow2.py in generate_counterfactuals(self, query_instance, total_CFs, desired_class, proximity_weight, diversity_weight, categorical_penalty, algorithm, features_to_vary, permitted_range, yloss_type, diversity_loss_type, feature_weights, optimizer, learning_rate, min_iter, max_iter, project_iter, loss_diff_thres, loss_converge_maxiter, verbose, init_near_query_instance, tie_random, stopping_threshold, posthoc_sparsity_param, posthoc_sparsity_algorithm) 134 loss_diff_thres, loss_converge_maxiter, verbose, 135 init_near_query_instance, tie_random, stopping_threshold, --> 136 posthoc_sparsity_param, posthoc_sparsity_algorithm) 137 138 counterfactual_explanations = exp.CounterfactualExamples(
/usr/local/lib/python3.7/dist-packages/dice_ml/explainer_interfaces/dice_tensorflow2.py in find_counterfactuals(self, query_instance, desired_class, optimizer, learning_rate, min_iter, max_iter, project_iter, loss_diff_thres, loss_converge_maxiter, verbose, init_near_query_instance, tie_random, stopping_threshold, posthoc_sparsity_param, posthoc_sparsity_algorithm) 431 432 # find the predicted value of query_instance --> 433 test_pred = self.predict_fn(tf.constant(query_instance, dtype=tf.float32))[0][0] 434 if desired_class == "opposite": 435 desired_class = 1.0 - round(test_pred)
/usr/local/lib/python3.7/dist-packages/dice_ml/explainer_interfaces/dice_tensorflow2.py in predict_fn(self, input_instance) 148 def predict_fn(self, input_instance): 149 """prediction function""" --> 150 temp_preds = self.model.get_output(input_instance).numpy() 151 return np.array([preds[(self.num_output_nodes-1):] for preds in temp_preds], dtype=np.float32) 152
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in getattr(self, name) 444 np_config.enable_numpy_behavior() 445 """) --> 446 self.getattribute(name) 447 448 @staticmethod
AttributeError: 'Tensor' object has no attribute 'numpy'`
Generate counterfactual examples
dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite")#,proximity_weight=1.5, diversity_weight=1.0)
Visualize counterfactual explanation
dice_exp.visualize_as_dataframe()
--> 116 temp_preds = self.model.get_output(input_instance).numpy() 117 return np.array([preds[(self.num_output_nodes-1):] for preds in temp_preds], dtype=np.float32) 118
AttributeError: 'Tensor' object has no attribute 'numpy'