This repository provides the official implementation of the algorithms introduced in the paper "Guaranteed Optimal Compositional Explanations for Neurons." Biagio La Rosa and Leilani H. Gilpin. ICML 2026 (Oral).
The repository includes:
- the algorithm for computing guaranteed-optimal compositional explanations
- the proposed decomposition of the IoU score (
dIoU), - heuristics for estimating explanation quality
- heuristic-guided beam-search variants
- utilities for comparing optimal and non-optimal explanations,
- scripts for reproducing the analyses presented in the paper.
We also release a Python package for easy integration into other projects including both the optimal compositional explanations and the beam-search explanations. The package can be installed via pip:
pip install compositional_explanations
Compositional explanations aim to identify the logical combination of concepts that maximizes the spatial alignment between neuron activations and semantic annotations in a probing dataset.
The alignment objective is defined through the Intersection over Union (IoU):
where
To make optimal search feasible, the paper introduces a decomposition of the IoU score (dIoU) into fundamental quantities governing alignment quality. Specifically, the decomposition separates unique intersections (
The decomposition is mathematically equivalent to the original IoU score.
Implementation: the main utilities for computing the dIoU and for managing quantities are included in the optimal_utils.py file. For example the compute_quantities_vector function computes the concept quantities and the compute_max_iou_from_label_info function estimates the maximum achievable IoU using the previous formula.
The heuristics use the decomposed IoU quantities to estimate:
- the alignment of candidate expansion (i.e. IoU of
$L$ ) - the best possible alignment reachable from a node
$L$ in the search tree by attaching additional concepts to the current formula ($L^*$ ). - upper and lower bounds for unexplored paths (
$IoU_{min}$ and$IoU_{max}$ ).
Two estimation strategies are possible:
- sample-based estimation, which computes quantities per sample and is more precise;
- aggregated estimation, which operates on global statistics and is significantly faster.
These heuristics are admissible and therefore preserve the optimality guarantees of the search algorithm.
Implementation: the sample-based estimation is included in the compositional/optimal_sample_heuristic.py file, while the aggregated estimation is included in the compositional/optimal_sum_heuristic.py file.
The algorithm performs a bounded informed search guided by the proposed heuristic.
At a high level, the algorithm:
- Computes exact quantities for all concepts
- Estimates upper and lower bounds for explanation paths
- Expands only explanations whose maximum achievable alignment can improve over the current best solution
- Refines heuristic estimates progressively
- Prunes branches whose upper bound falls below the current optimum.
Unlike beam search methods, the proposed algorithm is guaranteed to recover the explanation with the highest possible IoU within the specified search space.
Implementation: the optimal algorithm is implemented in the compositional/optimal.py file, while the computation of path estimates and bounds is implemented in the compositional/path_heuristic.py file.
This repository also includes the beam-search variant guided by our proposed heuristics. This variant is implemented in the compositional/beam_optimal.py file.
The repository also includes the implementation of M-MESH in the compositional/mmesh.py file and a vanilla beam search without heuristic guidance in the compositional/vanilla_search.py file.
The easiest way to use the algorithms is to install the package via pip:
pip install compositional_explanations
In the home directory of the repository (https://pypi.org/project/compositional-explanations/), you can find the documentation for the package, including an usage example.
Follow step 1, 2, and 3 described in PAPER.md to set up the environment and download the required assets.
All the scripts share a set of user arguments that define the configuration for which explanations are generated.
Common flags are defined in utils/common_flags.py.
The main ones are:
--dataset: defaultcityscapes_fine_sem_seg_val(also supportsade20k_full_sem_seg_freq_val_allandbroden)--model: defaultresnet18(also supportsalexnet,densenet161)--layer: defaultlayer4(foralexnetanddensenet161usefeatures)--heuristic:optimal,beam_optimal,mmesh,none--units: list of units to explain (defaultnone, which means all the units)--random_units: number of random units to select (default:0, which means all units)--device: compute device (defaultcuda)
In addition, the user can set the following flags
--length: explanation length (default3)--beam_limit: beam size (default5)--num_clusters: number of clusters to consider (default and the settings used in the paper1)--seed: random seed (default0)--preload_masks: whether to preload masks in memory (defaultTrue, can speed up computation but can be memory intensive)--step_size: Number of concepts (and their masks across the whole dataset) to keep in memory during the generation of the masks from the annotation dataset (default 60). Larger values can speed up the computation but can be memory intensive.--fast_impl: whether to use a faster implementation for the generation of the masks from the annotation dataset (defaultFalse, can speed up computation but can be memory intensive)
Finally, the user can set the following flags to specify the output directories (if not using the default ones):
--root_datasets: root directory where the datasets are stored (defaultdata/dataset)--root_models: root directory where the models are stored (defaultdata/model)--root_results: root directory where the results are stored (defaultdata/results)--root_segmentations: root directory where the segmentation masks are stored (defaultdata/cache/segmentations)--root_activations: root directory where the activations are stored (defaultdata/cache/activations)--root_optimal_info: root directory where the optimal information is stored (defaultdata/cache/optimal_info)
To compute explanations, use the run_clustering.py script with the common flags set appropriately.
python3 run_clustering.py \
--dataset=<DATASET> \
--model=<MODEL> \
--layer=<LAYER> \
--heuristic=<HEURISTIC> \
--num_clusters=1Explanation are saved in data/results/. Specifically, the explanations for different heuristics are saved in separate subdirectories:
- data/results/optimal/
- data/results/beam_optimal/
- data/results/mmesh/
- data/results/no_heuristic/
To compare behavior between different heuristics and optimal:
python3 compare_methods.py \
--dataset=<DATASET> \
--model=<MODEL> \
--layer=<LAYER> \
--num_clusters=1This script uses all common flags except heuristic, since it searches for all available results across methods.
Note that the script computes statistics only for configurations (units, model, layer, and so on) for which run_clustering.py has already been executed with matching flags. The output is displayed in the terminal.
Generate qualitative difference figures between M-MESH and Optimal:
python3 generate_images.py \
--dataset=broden \
--num_clusters=1- Visualizations are saved under
output/explanations/.
- Set
--seed(default0) for deterministic behavior where possible. - Add
CUBLAS_WORKSPACE_CONFIG=:4096:8for CUDA reproducibility. - Larger concept spaces can be memory-intensive. In these cases, consider tuning either
--preload_masks,--step_sizeor--fast_impl.
If you use this repository, please cite:
@inproceedings{
LaRosa2026,
title={Guaranteed Optimal Compositional Explanations for Neurons},
author={Biagio {La Rosa} and Leilani H. Gilpin},
booktitle={Forty-third International Conference on Machine Learning},
year={2026},
url={https://openreview.net/forum?id=MHiiwC3oFR}
}