Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Explaining Bayesian Neural Networks (MC Dropout + UAI)

This repository contains a minimal, self-contained demo that reproduces the core idea of "Explaining Bayesian Neural Networks" on a single ImageNet example (a tiger cat image).

We take a pretrained VGG16 network, interpret its dropout layers as an approximate Bayesian posterior (MC Dropout), sample multiple explanations with LRP-CMP, and aggregate them into:

  • Mean explanation: average relevance over MC samples
  • Intersection (low percentile): features that are consistently relevant
  • Union (high percentile): features that are relevant in at least some samples
  • UAI⁺: pixelwise stability of positive relevance across samples

Everything is designed to run locally, without any Colab specific code.


1. Setup

1.1. Clone the repo

git clone https://github.com/your-user/explaining-bnns-demo.git
cd explaining-bnns-demo

1.2. Create and activate a virtual environment (optional but recommended)

python -m venv .venv
source .venv/bin/activate  # on Windows: .venv\Scripts\activate

1.3. Install dependencies

pip install -r requirements.txt

Note: You need a PyTorch build that matches your system. The simplest way is usually to install from pytorch.org and then add the remaining packages from requirements.txt.


2. Add the tiger cat image

Create the assets folder (if it does not exist yet) and place an ImageNet style tiger cat image there:

mkdir -p assets
cp /path/to/your/tiger_cat.jpg assets/tiger_cat.jpg

The notebook assumes the file exists at:

assets/tiger_cat.jpg

3. Running the notebook

Start Jupyter from the repository root:

jupyter lab
# or
jupyter notebook

Then open:

notebooks/01_bnn_uai_tiger_cat.ipynb

The notebook is structured into the following steps:

  1. Load a pretrained VGG16 model and send it to CPU or GPU.
  2. Load and preprocess assets/tiger_cat.jpg.
  3. Compute a baseline explanation with LRP-CMP (no dropout at inference time).
  4. Enable MC Dropout and sample multiple explanations for the same image.
  5. Aggregate these explanations into:
    • Mean map
    • Intersection map (5th percentile)
    • Union map (95th percentile)
    • UAI⁺ stability map
  6. Visualize all maps side by side.

4. Code layout

  • src/models.py
    Utilities for loading the pretrained VGG16 model.

  • src/explanations.py
    Implementation of LRP-CMP, normalization helpers, and an UnNormalize transform.

  • notebooks/01_bnn_uai_tiger_cat.ipynb
    The main demo notebook, with explanatory text and a simple, linear workflow.


5. Citation

If you use this code in academic work, please consider citing:

Bykov, K. et al., "Explaining Bayesian Neural Networks" (TMLR, 2025).

This repository is a small convenience wrapper around the core ideas from that paper, focused on a single, easy to run example.

About

Repository for the Explaining Bayesian Neural Networks paper

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages