Trustworthy AI in Julia

The Alan Turing Institute

Delft University of Technology

2024-05-08

Introduction

Economist by training, previously Bank of England, currently 3rd year PhD in Trustworthy AI @ TU Delft.

Motivation

Why Trustworthy AI and why in Julia?

  • Opaque AI technologies have entered the public domain with far-reaching stakes.
  • These technologies are here to stay, so at best, we can make them more trustworthy.
  • Julia has an edge:
    • Transparency: most packages are written in pure Julia.
    • Intuitiveness: great Lisp-like support for symbolic computing.
    • Community: welcoming, supportive and diverse (sort of!).
    • Autodiff: top-notch support, which helps with common XAI approaches.

Outline

  • Taija: A brief overview of the Taija ecosystem.
    • Overview, Projects, Research.
  • Deep Dive: A closer look at some of our core packages.
    • Counterfactual Explanations, Conformal Prediction, Laplace Redux, Joint Energy Models.
  • The Journey: Julia throught my PhD
    • From “I’ll try this out” to “I’ll never go back”.

Taija

Taija is the organization that hosts software geared towards Trustworthy Artificial Intelligence in Julia.

Overview

Core Packages

Meta Packages

The TaijaBase.jl package provides common symbols, types and functions that are used across all or multiple Taija packages.

Milestones

2021

  • First small-scale project in Julia on Bayesian regression.

Milestones

2022

  • Presented CounterfactualExplanations.jl and LaplaceRedux.jl at JuliaCon.

2021

  • First small-scale project in Julia on Bayesian regression.

Milestones

2023

  • Presented ConformalPrediction.jl at JuliaCon.
  • TU Delft students work on CounterfactualExplanations.jl and LaPlaceRedux.jl.
  • CounterfactualExplanations.jl published in JuliaCon proceedings.

2022

  • Presented CounterfactualExplanations.jl and LaplaceRedux.jl at JuliaCon.

2021

  • First small-scale project in Julia on Bayesian regression.

Milestones

2024

  • Multiple presentations at JuliaCon this summer.
  • GSoC/JSoC projects on Causal Counterfactuals and Conformal Bayes.
  • TU Delft students working on TaijaInteractive.jl.

2023

  • Presented ConformalPrediction.jl at JuliaCon.
  • TU Delft students work on CounterfactualExplanations.jl and LaPlaceRedux.jl.
  • CounterfactualExplanations.jl published in JuliaCon proceedings.

2022

  • Presented CounterfactualExplanations.jl and LaplaceRedux.jl at JuliaCon.

2021

  • First small-scale project in Julia on Bayesian regression.

Research

Taija has been used in the following publications:

Counterfactual Explanations

CounterfactualExplanations.jl: A package for Counterfactual Explanations and Algorithmic Recourse in Julia.

Background

\[ \begin{aligned} \min_{\mathbf{Z}^\prime \in \mathcal{Z}^L} \{ {\text{yloss}(M_{\theta}(f(\mathbf{Z}^\prime)),\mathbf{y}^+)} + \lambda {\text{cost}(f(\mathbf{Z}^\prime)) } \} \end{aligned} \]

Counterfactual Explanations (CE) explain how inputs into a model need to change for it to produce different outputs.

📜 Altmeyer, Deursen, et al. (2023) @ JuliaCon 2022.

Figure 1: Gradient-based counterfactual search.

Examples

  • A motivating example.
  • A simple usage example.
  • Whistle-stop tour of different generators here.
  • Composing a custom generator using simple macros here.
  • Scaling things up with parallelization here.
  • Extension for differentiable tree-based models here.

Pick your Poison

All of these counterfactuals are valid explanations for the model’s prediction.

Which one would you pick?

Figure 2: Turning a 9 into a 7: Counterfactual explanations for an image classifier produced using Wachter (Wachter, Mittelstadt, and Russell 2017), Schut (Schut et al. 2021) and REVISE (Joshi et al. 2019).

ECCCos from the Black-Box

📜 Altmeyer, Farmanbar, et al. (2023) @ AAAI 2024

Key Idea

Use the hybrid objective of joint energy models (JEM) and a model-agnostic penalty for predictive uncertainty: Energy-Constrained (\(\mathcal{E}_{\theta}\)) Conformal (\(\Omega\)) Counterfactuals (ECCCo).

ECCCo objective1:

\[ \begin{aligned} & \min_{\mathbf{Z}^\prime \in \mathcal{Z}^L} \{ {L_{\text{clf}}(f(\mathbf{Z}^\prime);M_{\theta},\mathbf{y}^+)}+ \lambda_1 {\text{cost}(f(\mathbf{Z}^\prime)) } \\ &+ \lambda_2 \mathcal{E}_{\theta}(f(\mathbf{Z}^\prime)|\mathbf{y}^+) + \lambda_3 \Omega(C_{\theta}(f(\mathbf{Z}^\prime);\alpha)) \} \end{aligned} \]

Figure 3: Gradient fields and counterfactual paths for different generators.

Faithful Counterfactuals

Figure 4: Turning a 9 into a 7. ECCCo applied to MLP (a), Ensemble (b), JEM (c), JEM Ensemble (d).

ECCCo generates counterfactuals that

  • faithfully represent model quality (Figure 4).
  • achieve state-of-the-art plausibility (Figure 5).
Figure 5: Results for different generators (from 3 to 5).

Ongoing Work

  • JSoC 2024: From Counterfactuals to Interventions - Recourse through Minimal Causal Interventions with Jorge Luiz Franco and co-mentor Moritz Schauer (CausalInference.jl)
    • We typically make the implicit causal assumption that all features are independent.
    • If we have (imperfect) causal knowledge, we can use it to guide the search for counterfactuals.
  • What Makes Models Explainable?: using ECCCo to benchmark models.
  • Four master’s theses all using CounterfactualExplanations.jl (LLM explainability, imbalanced data, recourse in practice and counterfactuals for model testing).

Conformal Prediction

ConformalPrediction.jl: A package for Predictive Uncertainty Quantification through Conformal Prediction for Machine Learning models trained in MLJ.

Background

Intuitively, CP works under the premise of turning heuristic notions of uncertainty into rigorous uncertainty estimates through repeated sampling or the use of dedicated calibration data.

Conformal Prediction in action: prediction intervals at varying coverage rates. As coverage grows, so does the width of the prediction interval.

Examples

  • A simple usage example.
  • Conformalizing an image classifier here.
  • How to build a conformal chatbot here.

ConformalPrediction.jl meets SymbolicRegression.jl.

Ongoing Work

  • GSoC 2024: Add the support to Conformal Bayes to Taija with Pasquale Caterino and co-mentor Mojtaba Farmanbar
  • Support for conformal training (Stutz et al. 2022).
  • Code refactoring to allow adding support for parallelization using TaijaParallel.jl.
  • JuliaCon Proceedings paper (in planning).

Interactive Tour

… if time permits at the end.

First time here? Take a quick interactive tour to see what this package can do right on JuliaHub (To run the notebook, hit login and then edit).

This Pluto.jl 🎈 notebook won the 2nd Price in the JuliaCon 2023 Notebook Competition.

Laplace Redux

LaplaceRedux.jl: A package for Effortless Bayesian Deep Learning through Laplace Approximation for Flux.jl neural networks.

Background

We want BMA for neural networks underspecified by the data (Wilson 2020),

\[ p(y|x,\mathcal{D}) = \int p(y|x,\theta)p(\theta|\mathcal{D})d\theta \tag{1}\]

where \(p(y|x,\theta)\) is the likelihood, \(p(\theta|\mathcal{D})\) is the posterior and \(\mathcal{D}\) is the training data.

  • Problem 😢: Intractable posterior \(p(\theta|\mathcal{D})\).
  • Convention 🤔: Use MAP estimate \(p(y|x,\mathcal{D}) \approx p(y|x,\hat\theta)\) with \(\hat\theta=\arg\max_{\theta}p(\theta|\mathcal{D})\).
  • Idea 💡: Taylor approximation at the mode amounts to multivariate Gaussian centered around MAP.

Unnormalized log-posterior and corresponding Laplace Approximation. Source: Murphy (2022).

Model Assumptions

Example (binary classification): We assume Bernoulli likelihood and a Gaussian prior for our weights …

\[ p(y_n|\mathbf{x}_n;\mathbf{w})\sim\text{Ber}(y_n|\sigma(\mathbf{w}^T\mathbf{x}_n)) \tag{2}\]

\[ p(\theta) \sim \mathcal{N} \left( \theta | \mathbf{0}, \lambda^{-1} \mathbf{I} \right)=\mathcal{N} \left( \theta | \mathbf{0}, \mathbf{H}_0^{-1} \right) \tag{3}\]

… which yields the following negative log-likelihood (or energy) function (Murphy 2022):

\[ \psi(\theta)= - \sum_{n}^N [y_n \log \mu_n + (1-y_n)\log (1-\mu_n)] + \\ \frac{1}{2} (\theta-\theta_0)^T\mathbf{H}_0(\theta-\theta_0) \tag{4}\]

Taylor Expansion

We have the following normalized posterior:

\[ p(\theta|\mathcal{D}) = Z^{-1} p(\mathcal{D}|\theta)p(\theta) = Z^{-1} \exp(-\psi(\theta)) \tag{5}\]

with \(Z = \int p(\mathcal{D}|\theta) p(\theta)\).

Then, second-order Taylor expansion of the energy function around the mode \(\theta_0\) gives us the Laplace approximation (Murphy 2022):

\[ \psi(\theta) \approxeq \psi(\hat\theta) + \frac{1}{2} (\theta-\hat\theta)^T \mathbf{H}(\theta-\hat\theta) \tag{6}\]

Thus, we have

\[ p(\theta|\mathcal{D}) \approxeq \mathcal{N}(\theta;\hat\theta,-\mathbf{H}^{-1}) \tag{7}\]

Hessian Approximation

  • Challenge lies in computing the Hessian \(\mathbf{H}\).
  • We can use the Empirical Fisher or Generalized Gauss-Newton approximation (Murphy 2022).
    • Still quadratic in the number of parameters.
  • For scalable solutions, we can use the diagonal approximation or the K-FAC approximation or work with a subnetwork.

Examples

  • A basic usage example.
  • A binary classification example with prior tuning through empirical Bayes.
  • Counterfactual explanations with Laplace Redux here.

Joint Energy Models

JointEnergyModels.jl: A package for Joint Energy Models and Energy-Based Models in Julia.

Background

Joint Energy Models (JEM) are a class of energy-based models that learn a joint distribution over inputs and outputs (Grathwohl et al. 2020).

Training JEMs.

Package

  • Package used in Altmeyer et al. (2024), but less mature than the other packages.
  • Simple usage example can be found here.

Contributions welcome!

Samples generated through SGLD for JEM ensemble.

The Journey

Timeline

  • Background in R and Python (mostly R), a little bit of MATLAB during my master’s
  • First looked at Julia (v0.6) in 2017 during my master’s but quickly abandoned it.
  • During my master’s in Data Science (2021) I started to get frustrated with the speed of R and Python.
    • Did some work in C++ and Rcpp, but it was too cumbersome.
  • Started my PhD in Trustworthy AI in September 2021 and decided to give Julia another go.
  • Haven’t looked back since!

The Good

  • Julia is uniquely expressive and intuitive, making it easy to prototype and test ideas (especially in research).
  • The package manager will have you writing and shipping your own packages in no time.
  • Students at TU Delft have been able to pick up Julia quickly and contribute to Taija projects.
  • Multiple dispatch is a game-changer for writing clean and efficient code.
  • The release of Quarto in 2021 has made it easy to transition from R (Studio) to Julia.

The Bad

  • Expect to implement things from scratch (not always a bad thing!).
  • Some important packages are still in development and lack contributors (e.g. Transformers.jl).
  • I’m still sometimes puzzled by implicit imports (and I know my students are too!).
  • AI research is still dominated by Python/R, so developing in Julia does not always feel impactful.

Questions?

References

Altmeyer, Patrick, Giovan Angela, Aleksander Buszydlik, Karol Dobiczek, Arie van Deursen, and Cynthia CS Liem. 2023. “Endogenous Macrodynamics in Algorithmic Recourse.” In 2023 IEEE Conference on Secure and Trustworthy Machine Learning (SaTML), 418–31. IEEE.
Altmeyer, Patrick, Arie van Deursen, et al. 2023. “Explaining Black-Box Models Through Counterfactuals.” In Proceedings of the JuliaCon Conferences, 1:130. 1.
Altmeyer, Patrick, Mojtaba Farmanbar, Arie van Deursen, and Cynthia C. S. Liem. 2023. “Faithful Model Explanations Through Energy-Constrained Conformal Counterfactuals.” https://arxiv.org/abs/2312.10648.
Altmeyer, Patrick, Mojtaba Farmanbar, Arie van Deursen, and Cynthia CS Liem. 2024. “Faithful Model Explanations Through Energy-Constrained Conformal Counterfactuals.” In Proceedings of the AAAI Conference on Artificial Intelligence, 38:10829–37. 10.
Grathwohl, Will, Kuan-Chieh Wang, Joern-Henrik Jacobsen, David Duvenaud, Mohammad Norouzi, and Kevin Swersky. 2020. “Your Classifier Is Secretly an Energy Based Model and You Should Treat It Like One.” In International Conference on Learning Representations.
Hengst, Floris den, Ralf Wolter, Patrick Altmeyer, and Arda Kaygan. 2024. “Conformal Intent Classification and Clarification for Fast and Accurate Intent Recognition.” https://arxiv.org/abs/2403.18973.
Joshi, Shalmali, Oluwasanmi Koyejo, Warut Vijitbenjaronk, Been Kim, and Joydeep Ghosh. 2019. “Towards Realistic Individual Recourse and Actionable Explanations in Black-Box Decision Making Systems.” https://arxiv.org/abs/1907.09615.
Murphy, Kevin P. 2022. Probabilistic Machine Learning: An Introduction. MIT Press.
Schut, Lisa, Oscar Key, Rory Mc Grath, Luca Costabello, Bogdan Sacaleanu, Yarin Gal, et al. 2021. “Generating Interpretable Counterfactual Explanations By Implicit Minimisation of Epistemic and Aleatoric Uncertainties.” In International Conference on Artificial Intelligence and Statistics, 1756–64. PMLR.
Stutz, David, Krishnamurthy, Dvijotham, Ali Taylan Cemgil, and Arnaud Doucet. 2022. “Learning Optimal Conformal Classifiers.” https://arxiv.org/abs/2110.09192.
Wachter, Sandra, Brent Mittelstadt, and Chris Russell. 2017. “Counterfactual Explanations Without Opening the Black Box: Automated Decisions and the GDPR.” Harv. JL & Tech. 31: 841. https://doi.org/10.2139/ssrn.3063289.
Wilson, Andrew Gordon. 2020. “The Case for Bayesian Deep Learning.” https://arxiv.org/abs/2001.10995.