Skip to main content

SLALOM Explanations for transformer models.

Project description

SLALOM: High-Fidelity Token-Level Explanations for Transformers

pytest

What is SLALOM?

  • SLALOM is a surrogate model explanation method that is specifically designed for the transformer architecture. It uses a surrogate model class that is specifically designed to model the non-linearies in attention-based models, resulting in high-fidelity explanations.
  • The explanation can be visualized in a 2D-plane that contains a dot for each token in an input sequence. One axis describes the token value (its impact on the classification on its own) and the other describes the token importance (its interaction weight when seen in combination with other tokens).

This repository accompanies the TMLR Paper

Attention Mechanisms Don’t Learn Additive Models: Rethinking Feature Importance For Transformers

by Tobias Leemann, Alina Fastowski, Felix Pfeiffer, and Gjergji Kasneci. The technical details of the method are described in the paper.

13 Jan 2025: Note: A refactoring of the codebase is currently in progress. We will seperate the code to reproduce experiments from the actual implementation of SLALOM explanations, to make the method more accessible.





Installing the full repository

First setup a new environment by installing the dependencies listed in the file and activate it.

conda env create -f environment.yml
conda activate slalom

You will additionally need to install the shap package by running

conda install -c conda-forge shap

Add the corresponding kernel to your existing JupyterLab installation by executing:

python -m ipykernel install --user --name slalom

Basic usage

SLALOM contains an easy-to-use interface. The main commands to compute SLALOM explanations for Huggingface transformer models (SequenceClassification) are as follows:

from slalom_explanations import SLALOMLocalExplanantions 
from slalom_explanations import slalom_scatter_plot

# Initialize explainer with an initialized SequenceClassification model and corresponding tokenizer
slalom_explainer = SLALOMLocalExplanantions(model, tokenizer, modes=["value", "imp"])

# Compute SLALOM explanation
example_text = "This was an amazing movie!"
res_explanation = slalom_explainer.tokenize_and_explain(example_text)

# Scatter plot
slalom_scatter_plot(res_explanation, sizey=8, sizex=8)

We provide the notebook notebooks/0_Quickstart.ipynb, which fully runs all the steps with a pretrained model from the Huggingface hub.

Important files

The important code files for this project are located in the folder slalom_explanations. The experiments in Sections 6.1. / 6.2. are organized as Jupyter Notebooks in the folder notebooks.

Follow these intructions to reproduce experiments:

Here is an overview over the most prominent ones:

  • train_models.py The main training script to train the transformer models used in this work.
  • A_Motivation.ipynb: The Recovery example with Shapley values in Appendix A.
  • B_LearningLinearModels.ipynb: Code for Figure 3 showing that transformers do not learn linear models. Train models using scripts/train_models_linear.sh
  • C_Explaining_SLALOM_synth.ipynb: Code to explain the outputs of the transformers trained on the linear model with SLALOM (Figure 4ab)
  • D_ComputeGroundTruthOffline and eval_faithfulness.py: Compute faithfulness Metrics as Deletion/Insertion Scores (Table 1c, Appendix) as well as correlation with linear scores.
    • First run the notebook to compute importances offline
    • Train models using scripts/train_all_models.sh
    • then use the faithfulness script as outlined in scripts/test_all_models.sh
  • F_Tables.ipynb to collect results from log-files for naive bayes and Human-Attention ROC
  • G_FidelityMetrics.ipynbto print tables with results for Fidelity metrics (Insertion/removal, multiremoval in Figure 5)
  • E_RealWorldPlots.ipynb: Create quantitative results for IMDB datasets in Figure 6
  • E_RealWorldPlotsHAT.ipynb: Create quantitative results for YELP-HAT (Appendix, Figure 11)
  • CaseStudy.ipynb.ipynb: A case study for how SLALOM can be used to identify vulnerability and spurious correlations and create adversarial examples.
  • SLALOM_OpenAI.ipynb: Use SLALOM to explain an OpenAI model. the models were stored.
  • scripts/runtime_eval_script.py for runtime experiments.

Our implementation of the Local SLALOM fitting can be found in slalom_explanations/slalom_helpers.py and the implementations of other methods are in slalom_explanations/attribution_methods.py

Reference

Please cite our paper if you find the work or the code provided here helpful, e.g., using the following BibTeX entry:

@article{
leemann2025attention,
title={Attention Mechanisms Don{\textquoteright}t Learn Additive Models: Rethinking Feature Importance for Transformers},
author={Tobias Leemann and Alina Fastowski and Felix Pfeiffer and Gjergji Kasneci},
journal={Transactions on Machine Learning Research},
issn={2835-8856},
year={2025},
url={https://openreview.net/forum?id=yawWz4qWkF},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slalom_explanations-0.1.1.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slalom_explanations-0.1.1-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file slalom_explanations-0.1.1.tar.gz.

File metadata

  • Download URL: slalom_explanations-0.1.1.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for slalom_explanations-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3c13eab02bd3b3936a3f9d3c030e3a122d2ec8c4052c26d900b8e102b5d12d66
MD5 879a2dcadfcfa0466b2050a14e4caa50
BLAKE2b-256 5f994bdf802edb6e6f29e17c1f165b4c11e0940c19ffd0805d9f9b4a732244f8

See more details on using hashes here.

File details

Details for the file slalom_explanations-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for slalom_explanations-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac2d03f18d9c81219415e5a579a8a756c8f4b7e71ffcf27e0b4a0c4634d785be
MD5 91422c2c4c16bc575f16486f684a6080
BLAKE2b-256 4c5fd601335e4234963a65a6e397273dd7f30dce05b0fd9263f68fe843529d71

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page