Knowledge Base Regulation Tracker Standards & Frameworks OSS Directory AI Incident Database Research Paper Library Case Studies Society Articles News Jobs About Charter Sign inBecome a Member
Home/OSS Directory/Transformers Interpret

Transformers Interpret

Explainability and feature attribution

Explainability tool that works with any Hugging Face Transformers model in two lines, producing word attributions and visualisations for classification, question answering and vision models.

transformersattribution

Transformers Interpret: Explainability for Hugging Face Models

Transformers Interpret is a Python library designed to provide interpretability and explainability for any Hugging Face Transformers model. Developed by Charles Pierse and first released in 2020, the tool operates under the Apache 2.0 license, ensuring broad accessibility and permissive usage. Its core functionality revolves around generating word attributions and visualizations for classification, question answering, and vision models, all achievable in minimal code—typically two lines for text models and three for vision models.

The library aligns with the philosophy of the Transformers package by maintaining simplicity and ease of integration. It supports both text-based and computer vision models, offering explanations through numerical attributions and interactive visualizations. These visualizations can be displayed directly in notebooks or saved as HTML or PNG files for further analysis.

This accessibility ensures that researchers, developers, and practitioners can quickly incorporate explainability into their workflows without extensive setup.

How It Works

Transformers Interpret functions by integrating with the Hugging Face Transformers library, allowing users to explain model predictions through attribution scores. These scores indicate how much each input token (word or pixel) contributes to the model’s output. The library provides multiple explainers tailored to different model types and tasks.

For text models, the library includes the SequenceClassificationExplainer, which computes attributions for individual tokens in a sequence. The PairwiseSequenceClassificationExplainer extends this functionality to models that process two input sequences, such as Natural Language Inference (NLI) models and cross-encoders used in question answering. This explainer calculates pairwise attributions to determine how each input influences the model’s similarity score.

For vision models, the ImageClassificationExplainer provides pixel-level attributions, allowing users to visualize which parts of an image contribute most to a classification decision. This is particularly useful for models like Vision Transformers (ViT) or Swin Transformers, where understanding spatial contributions is critical. The library uses feature extractors from the Transformers ecosystem to process images and generate attributions for every pixel in the input.

Visualizations are generated using Captum’s built-in visualization library, producing interactive HTML files or static images. Users can also customize visualizations by specifying file paths or rendering them directly in Jupyter notebooks.

Use Cases

Transformers Interpret is applicable across a wide range of tasks within the Hugging Face ecosystem, including but not limited to sentiment analysis, question answering, and image classification. Its versatility makes it a valuable tool for debugging, validating, and improving model performance.

In sentiment analysis, the SequenceClassificationExplainer can attribute positive or negative sentiment to specific words in a sentence.

For question answering and passage ranking, the PairwiseSequenceClassificationExplainer is particularly useful. It can explain why a model assigns a high similarity score to a query-context pair, such as determining that the word Berlin in both the query (How many people live in Berlin?) and context (Berlin has a population of 3,520,031...) strongly influences the model’s decision. The flip_sign parameter further clarifies cases where the model predicts low similarity by inverting attributions to highlight dissimilarity factors.

In computer vision, the ImageClassificationExplainer provides insights into which regions of an image contribute to classification decisions. For instance, when classifying an image of a golden retriever using a Vision Transformer, the explainer can highlight pixels corresponding to the dog’s face or fur as key contributors to the prediction. This capability is essential for validating model behavior in tasks like object detection or medical imaging, where interpretability is critical.

Key Features and Capabilities

  • Minimal Code Integration: Transformers Interpret requires only two lines of code for text models and three for vision models to generate explanations.
  • Comprehensive Model Support: The library supports all Hugging Face Transformers models, including text-based models like BERT, DistilBERT, and RoBERTa, as well as vision models such as Vision Transformers (ViT) and Swin Transformers.
  • Attribution Visualization: Visualizations are generated using Captum’s library, producing interactive HTML files or static images. These visualizations can be displayed in notebooks or saved for later review, enhancing usability in both exploratory and collaborative settings.
  • Pixel-Level and Token-Level Attributions: For vision models, attributions are provided at the pixel level, while text models receive token-level attributions. This granularity allows users to pinpoint exact contributions within their input data, whether textual or visual.
  • Pairwise and Multiclass Explanations: The library includes specialized explainers for pairwise sequence classification (e.g., NLI models) and multiclass scenarios. These features enable users to analyze complex interactions between inputs or inspect model behavior across multiple classes.
  • Flexible Output Formats: Attributions can be returned as numerical lists or visualized directly.
  • Versioned Updates: The library has undergone regular updates, with notable releases introducing support for computer vision models (v0.9.5) and enhancing functionality for pairwise sequence classification (v0.8.1).

Transformers Interpret’s design emphasizes accessibility and practicality, making it a robust tool for researchers and developers aiming to demystify model decisions. Its integration with the Transformers library ensures seamless adoption, while its explainers provide actionable insights for improving model performance and trustworthiness.

Sources

Contribute

Spot an error, or know a record we should add? Members can propose additions and submit corrections.