interpretml / DiCE

Generate Diverse Counterfactual Explanations for any machine learning model.
https://interpretml.github.io/DiCE/
MIT License
1.31k stars 185 forks source link

TypeError: 'XGBClassifier' object is not callable #10

Open sina-salek opened 4 years ago

sina-salek commented 4 years ago

Sorry if this is a silly question, but I copied the notebook DiCE_with_advanced_options.ipynb and just changed the model to xgboost. When I try to run the line dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite") I get the error in the title. Would you be able to tell me what I'm doing wrong? Thanks

raam93 commented 4 years ago

Currently, DiCE supports classifiers based on TensorFlow or PyTorch frameworks only. If you want to use something like XGBoost, perhaps you can try BoostedTreeClassifier in TensorFlow and here is a nice tutorial on the same. For more info, this short paper compares TF's implementation of boosted trees with XGBoost and other related models.

sina-salek commented 4 years ago

Thanks for your prompt reply. I tried it with the BoostedTreeClassifier, but I still get a similar error message. I copy the entire message, in case you are so kind to help.


TypeError Traceback (most recent call last)

in 1 # generate counterfactuals ----> 2 dice_exp = exp.generate_counterfactuals(query_instance, total_CFs=4, desired_class="opposite") ~\Anaconda3\lib\site-packages\dice_ml\dice_interfaces\dice_tensorflow2.py in generate_counterfactuals(self, query_instance, total_CFs, desired_class, proximity_weight, diversity_weight, categorical_penalty, algorithm, features_to_vary, 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) 92 self.update_hyperparameters(proximity_weight, diversity_weight, categorical_penalty) 93 ---> 94 query_instance, test_pred = self.find_counterfactuals(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) 95 96 return exp.CounterfactualExamples(self.data_interface, query_instance, ~\Anaconda3\lib\site-packages\dice_ml\dice_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) 363 364 # find the predicted value of query_instance --> 365 test_pred = self.predict_fn(tf.constant(query_instance, dtype=tf.float32))[0][0] 366 if desired_class == "opposite": 367 desired_class = 1.0 - round(test_pred) ~\Anaconda3\lib\site-packages\dice_ml\dice_interfaces\dice_tensorflow2.py in predict_fn(self, input_instance) 99 def predict_fn(self, input_instance): 100 """prediction function""" --> 101 return self.model.get_output(input_instance).numpy() 102 103 def do_cf_initializations(self, total_CFs, algorithm, features_to_vary): ~\Anaconda3\lib\site-packages\dice_ml\model_interfaces\keras_tensorflow_model.py in get_output(self, input_tensor, training) 24 def get_output(self, input_tensor, training=False): 25 if self.backend == 'TF2': ---> 26 return self.model(input_tensor, training=training) 27 else: 28 return self.model(input_tensor) TypeError: 'BoostedTreesClassifier' object is not callable
raam93 commented 4 years ago

Hmm, okay. I will check and let you know.

sina-salek commented 4 years ago

Hi,

Sorry to bother you, I just wanted to check if you've managed to see if DiCE actually works with TF's BoostedTreeClassifier. If it doesn't at the moment, do you have plans to add the capability? Thanks

raam93 commented 4 years ago

Hey, sorry for the late response. You are right, DiCE currently doesn't support TF's BoostedTreeClassifier. I checked and it seems like the TF's estimator API is too abstract for the current DiCE implementation. DiCE works only when a model object is callable but estimator does not support that and instead has train and evaluate functions. We will try to add this feature in the future.

sina-salek commented 4 years ago

Thanks for getting back to me. Do I understand correctly that currently DiCE effectively works only with ANNs? I'm asking because I'm currently working on something where I need to train lots of different models, and ANNs are too slow to allow me to work with them properly, so it would be interesting to me if DiCE supports any other learning method.

raam93 commented 4 years ago

So to differentiate the model wrt input variables, we do model(x) in both PyTorch and TensorFlow. So any model that is callable in these libraries should work such as a linear or logistic regression which you can think of as single layer NNs. TF estimators should be doable, give us some time we will implement them and update DiCE soon.

drishyamlabs commented 3 years ago

Hi, thanks a lot for the wonderful library. Wanted to quickly check if any progress is made towards integration of tree based models direcly coming from scikit-learn?

aayesha-coder commented 3 years ago

I am getting the same error. TypeError: 'BoostedTreesClassifier' object is not callable Do you have any plan to resolve this issue soon?

amit-sharma commented 3 years ago

@aayesha-coder @drishyamlabs As of v0.5, we have included support for non-differentiable models using the parameter backend="sklearn" for the Model class. All sklearn classifiers/regressors are supported. In addition, since DiCE only needs the predict and predict_proba functions, any model that implements these two sklearn-style functions will also work (e.g., LightGBM).

Here's an example notebook with the sklearn backend. https://github.com/interpretml/DiCE/blob/master/docs/source/notebooks/DiCE_getting_started.ipynb

Hope this helps.

hadjipantelis commented 2 years ago

In fairness, this can now be closed. LightGBM/XGBoost work (mostly) fine now.

SylvesterBerry commented 1 year ago

I get TypeError "XGBClassifier" is not subscriptable.. Does anyone know solution for this?