liveBook · Manning

Feature attribution

As explained in Transformers in Action

A feature attribution method assigns importance scores to input features to indicate their contribution to a model prediction.

Feature attribution assigns importance scores to input features, indicating how they contribute to a model prediction. For language models, perturbation-based attribution removes or switches input features and observes changes in the target prediction. Captum exposes this workflow through FeatureAblation and LLMAttribution, producing token-level scores that can be visualized as input-to-output heat maps. section 10.2.1

Apply feature attribution through several complementary workflows:

  • Wrap a language model with LLMAttribution, pass tokenized input and a target string to attribute(), and inspect the resulting feature scores. section 10.2.1
  • Visualize token attribution with a heat map to compare how input tokens influence generated target tokens. section 10.2.1
  • Replace token-level input with TextTemplateInput when word-piece segmentation makes the explanation difficult to read. section 10.2.1
  • Use baselines to represent absent feature states and compare predictions against a chosen reference input. section 10.2.1
  • Use LIME for model-agnostic local explanations in text classification, sentiment analysis, or named entity recognition. section 10.2.2

Explaining generated interests

Given a prompt describing Nicole as a data scientist in Zurich, attribute the target “hiking, skiing, and playing the piano.” The resulting heat map can show that “Data scientist” contributes positively to “playing” but negatively to “hiking.” A template can keep “data scientist” together instead of exposing fragmented word pieces. section 10.2.1

Explaining sentiment

Run a sentiment classifier on “This movie was absolutely amazing!”, provide its probability function to LimeTextExplainer, and display word-level weights for the local positive prediction. section 10.2.2

Listing 10.6 Using FeatureAblation and LLMAttribution from Captum
fa = FeatureAblation(model)
llm_attr = LLMAttribution(fa, tokenizer)

Inspect the minimal Captum setup that wraps FeatureAblation with LLMAttribution before requesting language-model explanations. section 10.2.1

Listing 10.12 Using LIME to understand local model decisions
def predict_proba(texts): #1
    inputs = tokenizer(texts, return_tensors="pt", 
			   padding=True, truncation=True)
    outputs = model(**inputs)
    probabilities = torch.softmax(outputs.logits, dim=-1).detach().numpy()
    return probabilities

explainer = LimeTextExplainer(class_names=['negative', 'positive']) #2

input_text = "This movie was absolutely amazing!" #3
explanation = explainer.explain_instance(input_text, 
					      predict_proba, num_features=10)

explanation.show_in_notebook() #4

Inspect the LIME workflow that supplies prediction probabilities, explains one text instance, and renders the explanation. section 10.2.2

How Transformers in Action covers it

How deep it goes
Explains perturbation, baselines, token visualization, templates, and LIME workflows, but does not derive attribution mathematics or algorithm internals.
Examples
Demonstrates generated interests, token heat maps, templated inputs, baseline comparisons, and sentiment explanations for a positive movie review.
What it assumes
Assumes familiarity with loading models, tokenizers, predictions, and probabilities while demonstrating attribution APIs from working examples.
What you can do after
Diagnose/debug with Captum or LIME by generating local feature scores, visualizing influential tokens, and evaluating model limitations.
How it connects
Connects feature attribution with perturbation methods, baselines, tokenization, Captum, LIME, sentiment analysis, and named entity recognition.

Also explained in

Compare how 2 books explain feature attribution →

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.