Hello! Welcome to the next lesson in our module on Emerging Architectures and Research Frontiers.
In the previous lesson, we delved into the architecture of Mamba, a state-of-the-art State Space Model that challenges the dominance of the Transformer. We saw how intricate design choices—like selectivity and hardware-aware parallelism—can lead to models that are both powerful and efficient. However, building these complex models is only half the battle. Once a model makes a prediction, how do we understand why it made that choice? This brings us to the crucial field of model interpretability.
This lesson directly addresses the learning outcome: Apply gradient-based attribution methods (saliency maps, Integrated Gradients) for model interpretability. We will explore techniques that leverage the gradients—the same quantities used for training—to "peek inside" the model and attribute its decisions back to the input features.
Our journey will cover:
- Saliency Maps: The most straightforward gradient-based approach.
- The Saturation Problem: Why simple gradients can be misleading.
- Integrated Gradients (IG): A more robust method that overcomes this problem.
- Practical Application: Using libraries like Captum to apply these techniques and explore related methods like Grad-CAM.
Let's begin our exploration into making these black boxes a little more transparent.
1. The Quest for Explanation: Attribution Methods
When a deep learning model, particularly in computer vision, classifies an image, we often want to ask: "Which part of the image was most responsible for this decision?" For example, if a model classifies an image as containing a "cat," did it look at the whiskers, the pointy ears, or the texture of the fur?
Attribution methods aim to answer this question by assigning an "importance" or "relevance" score to each input feature (in this case, each pixel).
28 Saliency Maps – Interpretable Machine Learning
The book 'Interpretable Machine Learning' provides an excellent high-level categorization of these techniques. This section will help you build a mental map of the field.
Please read the introduction and the first few paragraphs of the chapter, down to the heading 'Vanilla Gradient'. Focus on understanding the two main ways to categorize attribution methods: Occlusion/Perturbation-based vs. Gradient-based: What is the fundamental difference in their approach? Gradient-only vs. Path-attribution: This distinction is key to understanding the motivation for Integrated Gradients later on.
As you've read, we can broadly classify these methods. This lesson focuses on gradient-based methods, which use the derivatives of the model's output with respect to its input to determine feature importance.
2. Saliency Maps: The "Vanilla" Gradient Approach
The simplest gradient-based method is often called a Saliency Map or Vanilla Gradient. The idea is intuitive: if a small change in a pixel's value causes a large change in the score for a specific class, that pixel must be important for that class.
Mathematically, for an input image and a model that produces a score for class , the saliency map is simply the gradient of the class score with respect to the input pixels, evaluated at the image :
The absolute value is often taken so we can visualize the magnitude of importance.
Your background in calculus and deep learning should make this concept feel familiar. It's the same gradient we use in backpropagation, but instead of calculating it with respect to the model's weights () to update them, we calculate it with respect to the input pixels () to understand them.
Gradient with respect to input in PyTorch (FGSM attack + Integrated Gradients)
This video provides a concise, practical demonstration of how to compute gradients with respect to inputs in PyTorch. This is the core operation for all the methods we'll discuss today.
Watch the segment from 02:14 to 04:07. The presenter writes a generic utility function get_input_grad. Pay close attention to how input.requires_grad_() is used to tell PyTorch to track gradients for an input tensor, which is not done by default during inference.
This get_input_grad function is all you need to create a basic saliency map. You pass the model and the input, and it returns the gradients, which you can then visualize.
The Problem: Gradient Saturation
While simple, this method has a significant flaw: gradient saturation. As a model becomes more confident about a feature, the gradient for that feature can approach zero.
Integrated gradients | TensorFlow Core
This TensorFlow tutorial offers the best explanation of the gradient saturation problem. The concepts are framework-agnostic, so they apply directly to PyTorch as well.
Read the section 'Calculate Integrated Gradients'. Focus on the two plots and the accompanying explanation. Understand how a feature 'x' can be clearly important, yet its gradient drops to zero as the model's prediction saturates.
Imagine a neuron that activates when a pixel value is greater than 0.8. If the input pixel has a value of 0.9, it's already strongly activating the neuron. Increasing it to 0.95 might not change the output much, so the gradient will be near zero. The vanilla gradient method would incorrectly conclude this pixel is unimportant, even though its value being above 0.8 is critical.
This is a fundamental limitation of looking only at the local gradient around the input. We need a method that considers the "bigger picture."
3. Integrated Gradients: A Path to Better Attributions
Integrated Gradients (IG) solves the saturation problem by accumulating all the local gradients along a path from a starting point (a baseline) to the input image.
The baseline is typically an uninformative input, like a completely black image. It represents the "absence" of features. The IG method attributes the total change in the model's prediction between the baseline and the input to the individual pixels.

The core idea is to:
- Define a straight-line path from the baseline image to the input image .
- Generate many small steps (interpolated images) along this path.
- Calculate the gradient at each step.
- Combine these gradients (approximate an integral) to get the final attribution.
The formal equation for IG is:
Where:
- is the i-th pixel of the input image.
- is the i-th pixel of the baseline.
- is the interpolation constant from 0 to 1.
- is the model's prediction function.
The integral averages the gradients along the path, and the term scales the result.
Integrated gradients | TensorFlow Core
The same TensorFlow tutorial provides an excellent step-by-step breakdown of how this formula is implemented in practice.
Skim through the sections from 'Unpack formulas into code' to 'Putting it all together'. You don't need to dive deep into the TensorFlow code, but focus on understanding the conceptual steps: Interpolate images: How are the steps between baseline and input created? Compute gradients: Gradients are computed for each interpolated image. Accumulate gradients (integral approximation): How is the integral approximated (e.g., using the Trapezoidal rule or a simple average)? Putting it all together: See how these pieces combine into a single function.
Now, let's see how this is done in PyTorch.
Gradient with respect to input in PyTorch (FGSM attack + Integrated Gradients)
Let's return to the 'mildlyoverfitted' video, which implements Integrated Gradients from scratch in PyTorch.
Watch the segment from 13:17 to 19:21. This part covers: The theoretical motivation for IG (13:17). The step-by-step PyTorch implementation of the compute_integrated_gradients function (14:54). A visual comparison of the results from the simple gradient and Integrated Gradients (16:54). Notice how much cleaner and more interpretable the IG attribution map is.
Test your understanding!
A model is trained to detect vertical lines. You give it an image that is all black except for a single, bright white vertical line. A saliency map (vanilla gradient) highlights only the edges of the line, but not the center. Why might this happen, and how would Integrated Gradients likely produce a better result?
Show answer
This is a classic example of gradient saturation. The pixels at the center of the white line are already at maximum brightness. The model is very confident about them, so their gradients are near zero. The pixels at the edges, however, are where a small change would most significantly impact the prediction (transitioning from black to white), so their gradients are high.
Integrated Gradients would solve this by starting from a black baseline. As the interpolated images gradually "fade in" the white line, the gradients would be high for all pixels in the line at the beginning of the path ( is small). By averaging the gradients along the entire path, IG would correctly attribute importance to all pixels making up the line, not just its edges.
4. Practical Tooling: Captum and Layer Attributions
While implementing these methods from scratch is a great learning exercise, production environments and research often rely on specialized libraries. For PyTorch, the standard is Captum.
Captum provides robust, optimized implementations of dozens of attribution algorithms, including Integrated Gradients.
Model Understanding with Captum
The official PyTorch channel provides a good overview of Captum. This will show you how to apply IG using the library and introduce you to other important concepts.
Please watch from the beginning to 06:43. Pay attention to: The different types of attribution Captum supports (Feature, Layer, Neuron). How to use the IntegratedGradients object from the library. The visualization utilities provided by Captum.
Beyond Input Pixels: Grad-CAM
So far, we've focused on attributing predictions to input pixels. But sometimes we want to know what a hidden layer is focusing on. Grad-CAM (Gradient-weighted Class Activation Mapping) is a popular technique for this.
Instead of backpropagating gradients to the input, Grad-CAM backpropagates them to the last convolutional layer. It uses these gradients to weight the layer's feature maps, producing a coarse heatmap that shows which spatial locations in the feature maps were important for a given prediction. This is especially useful for understanding what a CNN is looking at, without the fine-grained noise of pixel-level methods.
Model Understanding with Captum
Let's continue with the Captum video to see a demonstration of Layer Attribution with Grad-CAM.
Watch the segment from 08:49 to 10:28. Notice how Grad-CAM produces a lower-resolution heatmap corresponding to the dimensions of the convolutional layer, which is then upsampled and overlaid on the original image.
Conclusion
In this lesson, we opened the "black box" of neural networks using gradient-based attribution. We saw that while these models are complex, we have powerful techniques to probe their reasoning.
Key Takeaways:
- Saliency Maps (Vanilla Gradients) are the simplest attribution method, but they suffer from the gradient saturation problem, which can make them misleading.
- Integrated Gradients (IG) is a more robust "path-attribution" method that overcomes saturation by integrating gradients along a path from a neutral baseline to the input.
- Captum is the standard PyTorch library for applying a wide range of attribution algorithms, including IG and Grad-CAM.
- Grad-CAM is a layer attribution method that helps visualize which parts of an image a convolutional layer is focusing on.
A crucial final thought: as highlighted in the "Interpretable Machine Learning" book chapter, these methods are not foolproof. Research has shown they can be fragile or insensitive in certain situations. It's always best practice to use them as a debugging and hypothesis-generation tool, and to compare results from multiple different methods rather than trusting a single one blindly.
Preview of the Next Lesson:
The methods we've studied today are intimately tied to the model's architecture because they rely on gradients. What if we want a method that can be applied to any model, regardless of whether we can compute gradients? In the next lesson, we will explore model-agnostic approaches by applying SHAP (SHapley Additive exPlanations), a powerful technique rooted in cooperative game theory.