Lesson 5.9advanced2 h 30 min

Interpretability, robustness and adversarial examples

What is the model looking at, and how easily can it be fooled?

A model with 95% validation accuracy may be reading the hospital's watermark instead of the X-ray, or distinguishing huskies from wolves by the snow. Attribution methods like Grad-CAM show what a model attends to; adversarial examples show how brittle its decision boundary is; shift benchmarks show whether accuracy survives the real world. These tools catch failures before deployment, and they are the direct ancestors of mechanistic interpretability for LLMs (8.11 Interpretability: what is happening inside) and of the adversarial-input problems — prompt injection — that dominate agent security (10.10 Agent safety and security).

You'll be able to

  • Compute gradient saliency and Integrated Gradients, and derive Grad-CAM from class-score gradients
  • Explain feature visualization and use attributions to diagnose shortcut learning
  • Derive FGSM, explain why high-dimensional linearity makes adversarial examples inevitable, and describe PGD and adversarial training
  • Evaluate robustness under distribution shift and calibrate a model with temperature scaling
  • Audit a dataset and a trained model for bias and label errors

Key ideas

  1. Attribution asks which pixels mattered (saliency, SmoothGrad, Integrated Gradients with Σ IG = F(x) − F(x')); feature visualization asks what a unit detects (gradient ascent on activations).
  2. Grad-CAM: α_k = GAP of ∂y^c/∂A^k, heatmap = ReLU(Σ_k α_k A^k) upsampled; it reduces exactly to CAM for GAP+linear heads and works for any architecture.
  3. FGSM: x + ε·sign(∇_x L) flips predictions with imperceptible noise because linear scores change by ε‖w‖₁, which grows with dimension; PGD is the iterated, projected version.
Show 4 more key ideas
  1. Adversarial training (min over θ of max over ‖δ‖∞ ≤ ε of the loss) is the main defense; gradient masking is not a defense; attacks transfer across models.
  2. Shortcut learning (texture, backgrounds, artifacts) hides behind in-distribution accuracy — test on shifted sets (ImageNet-C/R/A, held-out sites) and report worst-group results.
  3. Modern nets are over-confident; temperature scaling (softmax(z/T) with T fit on validation) fixes calibration cheaply; ensembles give real uncertainty.
  4. Audit data (label noise ≈ 6% in ImageNet, duplicates, demographic coverage) and evaluate by subgroup before trusting a model.

Suggested path through this lesson