Interpretability
local graph
Chapter 15 — Interpretability
Section titled “Chapter 15 — Interpretability”Interpretability in LLMs refers to the model’s ability to understand and explain how the model processes inputs and generates outputs.
Interpretability is needed for LLMs for several reasons:
- Trust and transparency: Understanding how LLMs arrive at their outputs builds trust among users and stakeholders
- Debugging and improvement: Interpretability techniques can help identify model weaknesses and guide improvements
- Ethical considerations: Interpretable models allow for better assessment of potential biases and fairness issues
- Regulatory compliance: In some domains, interpretable AI models may be required for regulatory compliance
In this chapter, we will explore advanced techniques for understanding and explaining the outputs and behaviors of LLMs. We’ll discuss how to apply these techniques to transformer-based LLMs and examine the trade-offs between model performance and interpretability.
In this chapter, we’ll be covering the following topics:
- Attention visualization techniques
- Probing methods
- Explaining LLM predictions with attribution methods
- Interpretability in transformer-based LLMs
- Mechanistic interpretability
- Trade-offs between interpretability and performance
Attention visualization techniques
Section titled “Attention visualization techniques”Attention mechanisms are a key component of transformer-based LLMs (see Chapter 1). Visualizing attention patterns can provide insights into how the model processes and attends to different parts of the input.
Here’s an example of how to visualize attention in a transformer-based model:
import torchfrom transformers import BertTokenizer, BertModelimport matplotlib.pyplot as pltimport seaborn as snsdef visualize_attention(model, tokenizer, text): inputs = tokenizer(text, return_tensors="pt") outputs = model(inputs, output_attentions=True) attention = outputs.attentions[-1].squeeze().detach().numpy() tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) plt.figure(figsize=(10, 8)) sns.heatmap(attention, xticklabels=tokens, yticklabels=tokens, cmap="YlGnBu") plt.title("Attention Visualization") plt.show()# Example usagemodel_name = "bert-base-uncased"model = BertModel.from_pretrained(model_name)tokenizer = BertTokenizer.from_pretrained(model_name)text = "The cat sat on the mat."visualize_attention(model, tokenizer, text)This code provides a simple way to visualize the attention mechanism of a BERT model when processing a given input sentence. It begins by importing necessary libraries: PyTorch for model handling, Hugging Face’s transformers library for loading the BERT model and tokenizer, and Matplotlib and Seaborn for visualization. The visualize_attention function takes a BERT model, tokenizer, and input text. It first tokenizes the input using the tokenizer and feeds the tokenized input into the model with output_attentions=True to retrieve the attention weights. From the returned outputs, it extracts the attention matrix from the last layer (i.e., outputs.attentions[-1]), detaches it from the computation graph, and converts it to a NumPy array. This matrix represents how much attention each token in the sequence gives to every other token. The token IDs are then converted back to readable tokens for labeling the axes of a heatmap. Using Seaborn’s heatmap, the attention scores are visualized as a color-coded matrix, making it easier to interpret which words the model focuses on while processing each token. Finally, the code loads the pre-trained BERT base model and tokenizer, defines a sample sentence, and calls the visualization function to display the attention map, offering insights into BERT’s inner workings.
Keep in mind that attention maps do not always correlate with model reasoning in LLMs. While they show where the model focuses, they don’t necessarily explain why a decision is made. Attention can be diffused, inconsistent, or misleading, sometimes highlighting irrelevant tokens while still producing correct outputs. Since LLMs encode information in distributed representations, reasoning often occurs beyond direct attention, involving deep latent transformations across layers. Research also shows that attention maps can be manipulated without changing model behavior, proving they are not a definitive explanation of reasoning. For better interpretability, they should be combined with gradient-based methods, probing techniques, and causal analysis.
Probing methods
Section titled “Probing methods”Probing involves training simple models on the internal representations of an LLM to assess what linguistic properties are captured at different layers.
Different layers in a transformer specialize in different linguistic properties. Lower layers capture syntax and token identity; middle layers handle grammar and sentence structure; and higher layers focus on semantics, reasoning, and factual recall. This hierarchy emerges naturally during training, with lower layers excelling in syntactic tasks and higher layers in semantic reasoning. Probing studies confirm this specialization, aiding interpretability, fine-tuning, and model compression for task-specific optimizations.
Here’s an example of how to implement a probing task:
import torchfrom transformers import BertTokenizer, BertModelfrom sklearn.model_selection import train_test_splitfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import accuracy_scoredef probe_bert_layers(model, tokenizer, texts, labels, layer_nums): # Get BERT embeddings for each layer def get_embeddings(text): inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True) with torch.no_grad(): outputs = model(inputs, output_hidden_states=True) return outputs.hidden_states results = {} for layer in layer_nums: embeddings = [ get_embeddings(text)[layer] .squeeze() .mean(dim=0) .numpy() for text in texts ] # Split data X_train, X_test, y_train, y_test = train_test_split( embeddings, labels, test_size=0.2, random_state=42 ) # Train and evaluate probe probe = LogisticRegression(random_state=42) probe.fit(X_train, y_train) y_pred = probe.predict(X_test) accuracy = accuracy_score(y_test, y_pred) results[f"Layer_{layer}"] = accuracy return results# Example usagemodel_name = "bert-base-uncased"model = BertModel.from_pretrained(model_name)tokenizer = BertTokenizer.from_pretrained(model_name)texts = ["The cat sat on the mat.", "The dog chased the ball.", ...] # Add more exampleslabels = [0, 1, ...] # Corresponding labels (e.g., 0 for simple, 1 for complex sentences)layer_nums = [1, 6, 12] # Layers to probeprobe_results = probe_bert_layers(model, tokenizer, texts, labels, layer_nums)for layer, accuracy in probe_results.items(): print(f"{layer} Accuracy: {accuracy:.2f}")This code implements a simple probing task to assess how well different layers of a BERT model capture a specific linguistic property (in this case, sentence complexity).
Explaining LLM predictions with attribution methods
Section titled “Explaining LLM predictions with attribution methods”Attribution methods aim to identify which input features contribute most to a model’s prediction.
We need to discuss attribution methods because understanding why a model produces a particular prediction is critical for both interpretability and trustworthiness in real-world applications. Attribution methods provide a systematic way to trace the influence of specific input tokens on the model’s output, which is particularly important in LLMs where predictions are often made from complex, high-dimensional token embeddings and non-linear interactions across multiple attention layers. Without attribution, users and developers are left with a black-box model that produces outputs without any transparent rationale, making it difficult to validate decisions, debug behaviors, or ensure alignment with intended use cases.
One popular attribution method is integrated gradients.
Integrated gradients is an attribution method used to explain the predictions of neural networks by quantifying the contribution of each input feature to the model’s output. It computes feature attributions by integrating the gradients of the model’s output with respect to the input, along a straight path from a baseline to the actual input.
Keep in mind that gradient-based methods in LLMs can be noisy due to sensitivity to input perturbations, mini-batch variance, and gradient saturation, affecting both training stability and interpretability. In optimization, noise can cause oscillations or suboptimal convergence, while in interpretability, methods such as integrated gradients can produce inconsistent attributions across runs. This instability reduces trust in model insights, especially for similar inputs. Techniques such as gradient smoothing, averaging, and second-order optimization help mitigate noise but add computational overhead, creating a trade-off between efficiency and precision in LLM development.
Here’s an example of how to implement integrated gradients for a transformer-based model:
import torchfrom transformers import BertTokenizer, BertForSequenceClassificationimport numpy as npimport matplotlib.pyplot as pltdef integrated_gradients( model, tokenizer, text, target_class, steps=50): input_ids = tokenizer.encode(text, return_tensors="pt") baseline_ids = torch.zeros_like(input_ids) alphas = torch.linspace(0, 1, steps) delta = input_ids - baseline_ids accumulated_grads = 0 for alpha in alphas: interpolated_ids = baseline_ids + alpha * delta interpolated_ids.requires_grad_() outputs = model(interpolated_ids) pred = outputs.logits[:, target_class] model.zero_grad() pred.backward() accumulated_grads += interpolated_ids.grad attributions = \ (input_ids - baseline_ids) * accumulated_grads / steps return attributions.squeeze().detach().numpy()# Example usagemodel_name = "bert-base-uncased"model = BertForSequenceClassification.from_pretrained(model_name)tokenizer = BertTokenizer.from_pretrained(model_name)text = "This movie was fantastic!"target_class = 1 # Assuming 1 is the positive sentiment classattributions = integrated_gradients(model, tokenizer, text, target_class)# Visualize attributionstokens = tokenizer.convert_ids_to_tokens(tokenizer.encode(text))plt.figure(figsize=(10, 5))plt.bar(range(len(tokens)), attributions)plt.xticks(range(len(tokens)), tokens, rotation=45)plt.title("Integrated Gradients Attribution")plt.show()This code demonstrates how to use the integrated gradients method to interpret a BERT-based sequence classification model by attributing the model’s prediction to individual input tokens. The integrated_gradients function works by first encoding the input text into token IDs using the tokenizer and creating a baseline input of the same shape filled with zeros. It then interpolates between the baseline and actual input in small steps (default is 50) to compute gradients along this path. For each interpolated input, it calculates the model’s output for the specified target class, performs backpropagation to get the gradient with respect to the input, and accumulates these gradients. Finally, it computes the average of these gradients and multiplies it by the input difference (input – baseline) to get the attributions—this quantifies how much each input token contributes to the prediction. After defining the model and tokenizer, the code runs the attribution method on an example text and displays the results as a bar plot, where each bar corresponds to a token and its importance for the target prediction. This technique offers a more principled and model-aware way to understand which parts of the input were most influential, making it a powerful tool for interpretability and trust in model predictions.
Interpretability in transformer-based LLMs
Section titled “Interpretability in transformer-based LLMs”Transformer-based LLMs present unique challenges and opportunities for interpretability. Some key areas to consider are as follows:
- Multi-head attention: Analyzing individual attention heads to reveal specialized functions
- Positional embeddings: Understanding how models use positional information
- Layer-wise analysis: Examining how different linguistic features are captured across layers
Here’s an example of analyzing multi-head attention:
import torchfrom transformers import BertTokenizer, BertModelimport matplotlib.pyplot as pltdef analyze_multihead_attention(model, tokenizer, text): inputs = tokenizer(text, return_tensors="pt") outputs = model(inputs, output_attentions=True) attention = outputs.attentions[-1].squeeze().detach().numpy() tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0]) num_heads = attention.shape[0] fig, axs = plt.subplots(2, 4, figsize=(20, 10)) axs = axs.ravel() for i in range(num_heads): sns.heatmap(attention[i], xticklabels=tokens, yticklabels=tokens, ax=axs[i], cmap="YlGnBu") axs[i].set_title(f"Head {i+1}") plt.tight_layout() plt.show()# Example usagemodel_name = "bert-base-uncased"model = BertModel.from_pretrained(model_name)tokenizer = BertTokenizer.from_pretrained(model_name)text = "The president of the United States visited Paris last week."analyze_multihead_attention(model, tokenizer, text)This code visualizes the attention patterns of different heads in the last layer of a BERT model, allowing for comparison of their specialized functions.
Mechanistic interpretability
Section titled “Mechanistic interpretability”Mechanistic interpretability (MI) is an emerging field that aims to understand how neural networks process information at a detailed, component level—similar to how we might reverse-engineer a mechanical device. Rather than just observing inputs and outputs, MI seeks to trace how information flows through the network, identify specific computational patterns, and understand how different parts of the network (such as individual neurons or attention heads) contribute to the model’s behavior.
MI is important because it goes beyond surface-level explanations to uncover the internal mechanisms of how neural networks, particularly complex models like LLMs, actually work. By analyzing how specific components—like neurons, layers, or attention heads—process and transform information, MI helps researchers build a deeper, more principled understanding of model behavior. This insight is crucial for several reasons: it enhances trust by making models more transparent; it enables precise debugging and targeted improvements; it helps uncover and mitigate hidden biases or vulnerabilities; and it supports the development of safer, more controllable AI systems. Ultimately, MI brings us closer to treating neural networks not as black boxes, but as understandable systems that can be analyzed, interpreted, and refined with greater confidence.
Let’s build this up step by step:
- First, let’s create a simple interpretable
import torchimport torch.nn as nnclass InterpretableTransformer(nn.Module):def __init__(self, vocab_size, d_model, nhead, num_layers):super().__init__()self.embedding = nn.Embedding(vocab_size, d_model)encoder_layer = nn.TransformerEncoderLayer(d_model, nhead, batch_first=True)self.transformer = nn.TransformerEncoder(encoder_layer,num_layers)self.fc = nn.Linear(d_model, vocab_size)
- Now, let’s add a method to extract attention patterns, which are crucial for understanding how the model processes relationships
def get_attention_patterns(self, x):"""Extract attention weights from each layer"""x = self.embedding(x)attention_patterns = []for layer in self.transformer.layers:# Register a hook to capture attention weightsattention_weights = Nonedef hook(module, input, output):nonlocal attention_weightsattention_weights = output[1] # attention weightshandle = layer.self_attn.register_forward_hook(hook)x = layer(x)attention_patterns.append(attention_weights)handle.remove()return attention_patterns
- Let’s add a neuron activation analysis to understand which neurons are most active for
def analyze_neuron_activations(self, x, layer_idx):"""Analyze individual neuron activations in a specific layer"""activations = []def hook(module, input, output):activations.append(output.detach())# Register hook on specific layerhandle = list(self.transformer.layers)[layer_idx]\.register_forward_hook(hook)# Forward passwith torch.no_grad():self(x)handle.remove()layer_activations = activations[0]# Find most active neuronsmean_activation = layer_activations.mean(dim=(0,1)) # Average across batch and sequencetop_neurons = torch.topk(mean_activation, k=10)return top_neurons.indices, top_neurons.values
- We can add a method for causal intervention—temporarily modifying specific neurons to see how it affects
def intervention_study(self, x, layer_idx, neuron_idx):"""Study how zeroing out specific neurons affects the output"""original_output = Nonemodified_output = Nonedef hook_original(module, input, output):nonlocal original_outputoriginal_output = output.detach()def hook_modified(module, input, output):nonlocal modified_outputmodified = output.clone()modified[:,:,neuron_idx] = 0 # Zero out specific neuronmodified_output = modifiedreturn modifiedlayer = list(self.transformer.layers)[layer_idx]# Get original outputhandle = layer.register_forward_hook(hook_original)self(x)handle.remove()# Get modified outputhandle = layer.register_forward_hook(hook_modified)self(x)handle.remove()return original_output, modified_output
- Finally, let’s add a
import matplotlib.pyplot as pltdef visualize_attention(attention_weights, tokens=None):"""Visualize attention patterns"""plt.figure(figsize=(10, 8))plt.imshow(attention_weights[0].cpu(), cmap='viridis')if tokens is not None:plt.xticks(range(len(tokens)), tokens, rotation=45)plt.yticks(range(len(tokens)), tokens)plt.colorbar()plt.title('Attention Pattern')plt.show()
Here’s how to use these tools together:
# Initialize modelmodel = InterpretableTransformer(vocab_size=1000, d_model=256, nhead=8, num_layers=4)# Sample inputinput_ids = torch.randint(0, 1000, (1, 20)) # Batch size 1, sequence length 20# Get attention patternsattention_patterns = model.get_attention_patterns(input_ids)# Analyze neuron activationstop_neurons, activation_values = model.analyze_neuron_activations( input_ids, layer_idx=0)# Perform intervention studyoriginal, modified = model.intervention_study(input_ids, layer_idx=0, neuron_idx=42)# Visualize attentionvisualize_attention(attention_patterns[0]) # Visualize first layer's attentionEach component helps us understand different aspects of the model:
- Attention patterns show how the model relates different tokens to each other
- The neuron activation analysis reveals which neurons are most important for processing specific inputs
- Causal intervention helps us understand the role of specific neurons by observing how the output changes when we modify them
- Visualization tools help us interpret these patterns more intuitively
This is a basic implementation—real MI research often involves more sophisticated techniques, such as circuit analysis, activation patching, and detailed studies of how specific capabilities (such as induction or negation) are implemented in the network.
Trade-offs between interpretability and performance
Section titled “Trade-offs between interpretability and performance”There’s often a tension between model performance and interpretability. More complex models tend to perform better but are harder to interpret. Some approaches to balance this trade-off include the following:
- Distillation: Training smaller, more interpretable models to mimic larger LLMs
- Sparse models: Encouraging sparsity in model weights or activations for easier interpretation
- Modular architectures: Designing models with interpretable components
Here’s a simple example of model distillation:
import torchfrom transformers import ( BertForSequenceClassification, DistilBertForSequenceClassification, BertTokenizer)def distill_bert( teacher_model, student_model, tokenizer, texts, temperature=2.0): teacher_model.eval() student_model.train() optimizer = torch.optim.Adam(student_model.parameters(), lr=1e-4) loss_fn = torch.nn.KLDivLoss(reduction="batchmean") for text in texts: inputs = tokenizer( text, return_tensors="pt", padding=True, truncation=True ) with torch.no_grad(): teacher_outputs = teacher_model(inputs) teacher_logits = teacher_outputs.logits / temperature student_outputs = student_model(inputs) student_logits = student_outputs.logits / temperature loss = loss_fn(torch.log_softmax(student_logits, dim=-1), torch.softmax(teacher_logits, dim=-1)) optimizer.zero_grad() loss.backward() optimizer.step() return student_model# Example usageteacher_model = BertForSequenceClassification.from_pretrained( "bert-base-uncased")student_model = DistilBertForSequenceClassification.from_pretrained( "distilbert-base-uncased")tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")texts = ["This movie was great!", "I didn't like the book.", ...] # Add more examplesdistilled_model = distill_bert( teacher_model, student_model, tokenizer, texts)This code demonstrates a simple distillation process, where a smaller DistilBERT model learns to mimic the behavior of a larger BERT model.
In addition, we need to keep in mind trade-offs between compression and interpretability, which revolve around balancing efficiency, accuracy, and transparency. Compression techniques such as quantization, pruning, and knowledge distillation significantly reduce model size and inference latency, enabling LLMs to run on edge devices or with lower computational costs. However, these methods can degrade performance, particularly in long-context reasoning, rare token prediction, or domain-specific tasks, where preserving intricate weight structures is crucial. Moreover, heavily compressed models often become less interpretable since removing neurons or attention heads or reducing precision obscures the model’s internal representations, making it harder to analyze why certain outputs are generated.
Conversely, interpretability techniques, such as feature attribution, attention visualization, and probing, help researchers and users understand how LLMs process information, detect bias, or debug failures, but they typically require access to the full, unmodified model. Larger, uncompressed models retain more internal knowledge and nuanced representations, making them easier to analyze but harder to deploy efficiently. Furthermore, highly interpretable architectures sometimes impose constraints on model flexibility, limiting their ability to generalize across diverse tasks.
The key challenge is finding the optimal balance—for example, Low-Rank Adaptation (LoRA) allows for fine-tuning without modifying full model weights, helping maintain some interpretability while enabling efficient deployment. As LLMs scale, developers must weigh efficiency gains from compression against the risks of reduced transparency, especially in high-stakes applications such as healthcare, law, and AI safety, where understanding model decisions is as critical as performance.
Summary
Section titled “Summary”In this chapter, we equipped you with a toolkit of interpretability techniques to gain insights into your LLMs’ decision-making processes, which is crucial for developing more transparent and trustworthy AI systems.
As LLMs continue to grow in size and capability, interpretability research will play a crucial role in ensuring these powerful models can be understood, trusted, and safely deployed in real-world applications. Some key challenges and future directions in interpretability will include scaling such techniques for large models, understanding causal relationships, enabling interactive explorations, and developing techniques for specific downstream tasks.
In the next chapter, we will explore techniques for assessing and mitigating fairness and bias in LLMs. This is a critical aspect of responsible AI development, building on the interpretability methods we’ve discussed to ensure that LLMs are not only powerful and interpretable but also fair and unbiased in their outputs and decision-making processes.