CounterfactualTraining
Documentation for CounterfactualTraining.
CounterfactualTraining.jl
Teaching model plausible and actionable explanations.
TL;DR
CounterfactualTraining.jl can be used to train artificial neural networks that are inherently more explainable and robust than contenionally trained models.
Paper
The package was initially developed as part of our IEEE SaTML 2026 paper Counterfactual Training: Teaching Models Plausible and Actionable Explanations: open the preprint.
Abstract: We propose a novel training regime termed counterfactual training that leverages counterfactual explanations to increase the explanatory capacity of models. Counterfactual explanations have emerged as a popular post-hoc explanation method for opaque machine learning models: they inform how factual inputs would need to change in order for a model to produce some desired output. To be useful in real-world decision-making systems, counterfactuals should be plausible with respect to the underlying data and actionable with respect to the feature mutability constraints. Much existing research has therefore focused on developing post-hoc methods to generate counterfactuals that meet these desiderata. In this work, we instead hold models directly accountable for the desired end goal: counterfactual training employs counterfactuals during the training phase to minimize the divergence between learned representations and plausible, actionable explanations. We demonstrate empirically and theoretically that our proposed method facilitates training models that deliver inherently desirable counterfactual explanations and additionally exhibit improved adversarial robustness.
Package
The package provides GPU-friendly training routines for models trained in Flux.jl.
Installation
The package can be installed as follows:
using Pkg
Pkg.add("CounterfactualTraining")Introduction
A self-contained introductory example demonstrating counterfactual training with mutability protection is available on the Introduction page.
Training on GPU
The package provides two training approaches: a branch using CounterfactualExplanations.jl as the backend for generating counterfactuals and a performance-focused Native submodule with GPU support. The former approach was developed during the initial research stage of the project. See Training on GPU for a full walkthrough with MNIST.