liveBook · Manning

Captum

As explained in Transformers in Action

Captum is an open-source library for interpreting machine-learning model predictions through attribution and explainability methods.

Captum is an open-source PyTorch library for interpreting model predictions. It exposes attribution methods that help inspect how language models prioritize input information and produce text, supporting analysis of bias, errors, and transparency. section 10.2 section 10.2.1

Captum separates attribution approaches by the access they require:

  • Perturbation-based methods repeatedly evaluate a black-box model with varied inputs and do not require access to model weights. section 10.2.1
  • Gradient-based methods estimate attribution by backpropagating gradients and therefore require access to model weights. section 10.2.1
  • Attribution results can be visualized across layers, attention heads, or individual tokens to inspect which information influenced generated output. section 10.2

Attribute generated text with FeatureAblation

Load Llama 3, create a prompt about Nicole, and generate a continuation. Wrap FeatureAblation with LLMAttribution, represent the prompt with TextTokenInput, and request attribution for the target phrase “hiking, skiing, and playing the piano.” Plot the result as a token-level heat map. TextTemplateInput can instead separate prompt values, while baselines such as alternate names, locations, occupations, and pronouns support refined comparisons.

How Transformers in Action covers it

How deep it goes
Demonstrates perturbation and gradient distinctions plus token-level attribution workflows, without detailing attribution algorithm internals.
Examples
Demonstrates Llama 3 text generation, FeatureAblation, LLMAttribution, token inputs, templates, baselines, and attribution heat maps.
What it assumes
Assumes PyTorch and Hugging Face access, including an approved account token for loading the Llama 3 model.
What you can do after
Diagnose generated text by measuring and plotting how input tokens influence selected language-model outputs.
How it connects
Connects model explainability with PyTorch, LLMs, attribution, attention visualization, perturbation methods, gradient methods, and LIME.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.