Code Monkey home page Code Monkey logo

modeling-uncertainty-local-explainability's Introduction

Reliable Post hoc Explanations: Modeling Uncertainty in Explainability

Welcome to the code for our paper, Reliable Post hoc Explanations: Modeling Uncertainty in Explainability, published at NeurIPS 2021. We encourage you to read the full paper.

Visualizing the posteriors of BayesLIME explanations on an image of a dog and COMPAS:

Citation

If you found this work useful, please cite us:

@inproceedings{reliableposthoc:neurips21,
  author = {Dylan Slack and Sophie Hilgard and Sameer Singh and Himabindu Lakkaraju},
  title = { {Reliable Post hoc Explanations Modeling Uncertainty in Explainability} },
  booktitle = {Neural Information Processing Systems (NeurIPS)},
  year = {2021}
}

Examples

An example usage of the explainer is provided in ./visualization/image_posterior_example.py, where we visualize the posterior of a BayesLIME explanation on an image of the first author's dog.

Experiments

Data

Tabular Data

The German Credit + COMPAS datasets are included in the ./data folder. Within experiments, the german credit data set is called as --dataset german and compas is called as --dataset compas.

MNIST

The MNIST data is set to download automatically on the first run.

In places where the MNIST data is accepted, by specifying the --dataset flag, it is possible to select the digit on which to run the experiment by specifying, for example, --dataset mnist_1 for the 1 digit or --dataset mnist_3 for the 3 digit, and so on.

ImageNet

To download the ImageNet data, use this script, selecting the appropriate class indices (e.g., n02108915 is the French Bulldog class used in the paper). For example, to download the French Bulldog data, run:

python ./downloader.py 
    -data_root ./data/imagenet/frenchbulldog \
    -use_class_list True \
    -class_list n02108915 \
    -images_per_class 100 

Once the imagenet dataset is installed, it can be called with --dataset imagenet_classname where classname is the name of the folder where the data is stored (for instance frenchbulldog running the script above).

Models

The tabular models are trained when they are called in experiments. The pre-trained MNIST model is provided in the ./data/mnist subfolder. The VGG16 IMAGENET model will be downloaded when it is called.

Experiments

Code to run experiments from the paper is included in the ./experiments directory within the project.

Hardware Requirements

For image experiments, GPU/TPU acceleration is recommended. I ran most of the experiments for this paper with a single NVIDIA 2080TI and a few with a NVIDIA Titan RTX.

For the tabular experiments, it's possible to run them on CPU. I tested this using a 1.4 GHz Intel Core i5 from a 2019 MacBook Pro, and it seemed to work fine. In places in the experiments where multithreading is used (--n_threads) in the experiments, be careful to use a value less than the avaliable cores on your CPU. I noticed that if I set --n_threads value too high on the MacBook, it caused it to freeze.

Questions

You can reach out to [email protected] with any questions.

modeling-uncertainty-local-explainability's People

Contributors

dylan-slack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

modeling-uncertainty-local-explainability's Issues

Explain regression model

Hi Dylan,

Thanks a lot for releasing the code! I'm looking into applying your BayesSHAP to explain the input feature importance of a high-dimensional regression model. Based on my understanding on your method, the Bayesian inference is directly applicable to regression task. I've made the following minor edits to explanations.py in order to perform BayesSHAP on regression tasks.

  • line 37: the input argument categorical_features is set to None as there is no categorical inputs in my case
  • line 59: assert mode in ["classification"] -> assert mode in ["classification", "regression"]
  • line 553: the input argument label is set to 0 as there is only 1-d output for regression
  • use a regression prediction model for all classifier_f with output dimension [n_test, 1]

I'd deeply appreciate if you could double-check whether those changes are correct/sufficient. Thanks a lot.

Robin

Convergence error encountered at self.enumerate_initial_shap()

Hi Dylan,

When I run BayesSHAP on my regression task, I encounter the convergence error at the self.enumerate_initial_shap function. Any idea why this happens?
I saw enumerate_initial is set to True as default which leads to this self.enumerate_initial_shap. Is it necessary to do so when performing BayesSHAP?
Also is BayesSHAP scalable to high input dimensions?

Best
Robin

Traceback (most recent call last):
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-414d493a31bd>", line 14, in <module>
    e_i = exp.explain(X_bayesshap[i], **exp_kwargs)
  File "/home/Robin/BNN_Torch/bayesshap/explanations.py", line 651, in explain
    l2=l2)
  File "/home/Robin/BNN_Torch/bayesshap/explanations.py", line 457, in _explain_bayes_shap
    data_init, inverse_init = self._enumerate_initial_shap(data, max_coefs)
  File "/home/Robin/BNN_Torch/bayesshap/explanations.py", line 417, in _enumerate_initial_shap
    inverse = self.shap_info.discretizer.undiscretize(data)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/lime/discretize.py", line 145, in undiscretize
    feature, ret[:, feature].astype(int)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/lime/discretize.py", line 132, in get_undiscretize_values
    random_state=self.random_state
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py", line 980, in rvs
    vals = self._rvs(*args)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/scipy/stats/_distn_infrastructure.py", line 913, in _rvs
    Y = self._ppf(U, *args)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/scipy/stats/_continuous_distns.py", line 7163, in _ppf
    return _truncnorm_ppf(q, a, b)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/scipy/stats/_continuous_distns.py", line 6933, in vf_wrapper
    return vf(*args)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/numpy/lib/function_base.py", line 2163, in __call__
    return self._vectorize_call(func=func, args=vargs)
  File "/homeminiconda3/envs/hpmodel/lib/python3.7/site-packages/numpy/lib/function_base.py", line 2246, in _vectorize_call
    outputs = ufunc(*inputs)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/scipy/stats/_continuous_distns.py", line 7113, in _truncnorm_ppf
    maxiter=TRUNCNORM_MAX_BRENT_ITERS)
  File "/home/miniconda3/envs/hpmodel/lib/python3.7/site-packages/scipy/optimize/zeros.py", line 780, in brentq
    r = _zeros._brentq(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
RuntimeError: Failed to converge after 40 iterations.

z value of standard normal distribution hardcoded in regression.py (line 105)

Hi Dylan!

The value for z for the standard normal distribution used in the calculation of the PTG estimate has been hardcoded to 1.96 on line 105 in method get_ptg in regression.py. This would give an erroneous PTG estimate for the desired confidence levels other than 95%, hence this value needs to be dynamically calculated using self.percent.

Regards
Suchismita

Numpy "negative dimensions are not allowed" error in explanations.py (line 521)

Hi Dylan!

I was running your code on a number of datapoints to calculate SHAP estimates using KernelSHAP and got "negative dimensions are not allowed" numpy error on a particular datapoint after which the code crashed. The error was raised at line 521 in explanations.py. I believe the error is because the value for n_needed for the particular datapoint came out to be lower than the value of ptg_inital_points which is set it to its default value of 200. This is not happening for all datapoints because the n_needed value caluclated each time is different of course, but adding an if-else statement to handle this edge case and bypass the second call to _shap_tabular_perturb_n_samples fixed it.

Regards
Suchismita

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.