peal.global_utils.extract_penultima_activation¶
- peal.global_utils.extract_penultima_activation(x, predictor, distance_from_last_layer=1)[source]¶
Return the activation feeding the predictor’s last layer.
For
distance_from_last_layer == 1the model’s ownfeature_extractoris used when it has one (seepeal.architectures.predictors.TorchvisionModel); otherwise the module tree is unwrapped until it branches and everything but the last child is re-assembled into aSequential. Larger distances are served byget_intermediate_output(), which hooks the corresponding leaf module.- Parameters:
x (
torch.Tensor) – Input batch in the predictor’s normalisation,(B, C, H, W).predictor (
torch.nn.Module) – The classifier.distance_from_last_layer (
int, optional) – How many layers before the output to tap.
- Returns:
The activations; the trailing spatial dimensions are kept, so a ResNet yields
(B, D, 1, 1). The result stays attached to the graph, which the orthogonality penalty of the explainer relies on.- Return type: