LayerGradCAMExplainer<T>

Models & Types · AiDotNet.Interpretability.Explainers

Layer GradCAM (Gradient-weighted Class Activation Mapping) explainer.

Implements: IGPUAcceleratedExplainer<T>, ILocalExplainer<T, LayerGradCAMExplanation<T>>

For Beginners

GradCAM produces a coarse localization map showing which regions of an input (usually an image) are important for a prediction.

How it works:

  1. Get activations at a target layer (usually the last convolutional layer)
  2. Compute gradients of the target class with respect to these activations
  3. Global average pool the gradients to get importance weights for each channel
  4. Compute weighted combination of activation channels
  5. Apply ReLU to keep only positive influences

Why GradCAM is useful:

  • Shows WHERE the model is looking, not just WHAT features matter
  • Works with any CNN architecture
  • Produces interpretable heatmaps
  • Doesn’t require architectural changes or retraining

Layer choice:

  • Last conv layer: Best balance of semantic meaning and spatial detail
  • Earlier layers: More spatial detail but less semantic meaning
  • Later layers: More semantic but coarser resolution

Limitations:

  • Resolution limited by the target layer’s spatial dimensions
  • May miss fine-grained details (use GuidedGradCAM for that)

Constructors

Constructor Summary
LayerGradCAMExplainer(Func<Vector<T>, Vector<T>> predictFunction, Func<Vector<T>, Vector<T>> layerActivationFunction, Func<Vector<T>, int, Vector<T>> layerGradientFunction, int layerHeight, int layerWidth, int numChannels, int[] inputShape) Initializes a LayerGradCAM explainer.
LayerGradCAMExplainer(INeuralNetwork<T> network, int targetLayerIndex, int layerHeight, int layerWidth, int numChannels, int[] inputShape) Initializes a LayerGradCAM explainer from a neural network.

Properties

Property Summary
bool IsGPUAccelerated { get; }
bool SupportsGlobalExplanations { get; }
bool SupportsLocalExplanations { get; }
string MethodName { get; }

Methods

Method Summary
LayerGradCAMExplanation<T> Explain(Vector<T> input, int? targetClass) Generates a GradCAM explanation for an input.
LayerGradCAMExplanation<T>[] ExplainBatch(Matrix<T> instances)
void SetGPUHelper(GPUExplainerHelper<T> helper)