L^c = ReLU(sum_k alpha^c_k * A^k), where alpha^c_k = mean_ij(d(y^c)/d(A^k_ij)) and A is the last OpType::Conv node's activation.
More...
#include <grad_cam.hpp>
L^c = ReLU(sum_k alpha^c_k * A^k), where alpha^c_k = mean_ij(d(y^c)/d(A^k_ij)) and A is the last OpType::Conv node's activation.
- Note
- Finds the target layer via ComputationGraph::nodes_by_op_type(OpType::Conv)'s last entry – the theory file's stated default ("the last convolutional layer
before the classifier head"), found by op-type tag per charter Part 2 SS3, never by layer name.
-
Reads the target node's own activation/gradient as ComputationGraph tagged it – in this codebase Conv and its following ReLU are separate graph nodes, so "the
last conv layer" here means the pre-ReLU conv output specifically. This is a legitimate placement (Grad-CAM's formula doesn't require a post-ReLU activation), just worth noting for a reader used to "conv block" framing elsewhere.
-
A pure graph walker built entirely against ExplainerContext's public interface – no core (Tensor/ComputationGraph/Autograd/Module) changes needed.
-
Device-generic: the CAM weighting runs on host copies of the target node's activation/gradient; the seed and the CAM are uploaded beside the network's tensors.
◆ explain()
Computes the Grad-CAM map for one target class index.
- Parameters
-
| ctx | Context to run the forward/backward pass through. Its graph must contain at least one OpType::Conv node. |
| input | Input to explain. |
| target_index | Which output element (class score) to attribute (0-based, flat index into the network's final output). |
| backend | Backend to allocate the one-hot seed and result tensors through. |
- Returns
- An Attribution with method "grad_cam", values = the N x H x W CAM batch (same spatial size as the target conv layer's feature maps, one map per batch example), and metadata recording the target index and which node was used.
- Note
- Assumes a rank-2 (N, num_classes) network output and a rank-4 (N, channels, H, W) target conv layer activation – migrated by campaign_exai_dl_library_batch_dimension_support from the original rank-1/ rank-3 assumptions. Same single-shared-target_index scope boundary as Saliency's identical migration.
The documentation for this class was generated from the following file: