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 == 1 the model’s own feature_extractor is used when it has one (see peal.architectures.predictors.TorchvisionModel); otherwise the module tree is unwrapped until it branches and everything but the last child is re-assembled into a Sequential. Larger distances are served by get_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:

torch.Tensor