attribench.functional.metrics.sensitivity_n

attribench.functional.metrics.sensitivity_n(model, attributions_dataset, batch_size, maskers, activation_fns, min_subset_size, max_subset_size, num_steps, num_subsets, segmented, device=device(type='cpu'))[source]

Computes the Sensitivity-n metric for a given AttributionsDataset and model.

Sensitivity-n is computed by iteratively masking a random subset of n features of the input samples and computing the output of the model on the masked samples.

For each random subset of masked features, the sum of the attributions is also computed. This results in two series of values: the model output and the sum of the attributions. The Sensitivity-n metric is the correlation between these two series.

This is repeated for different values of n between min_subset_size and max_subset_size in num_steps steps. min_subset_size and max_subset_size are percentages of the total number of features. For each value of n, num_subsets random subsets are generated.

If segmented is True, then the Seg-Sensitivity-n metric is computed. This metric is analogous to Sensitivity-n, but instead of using random subsets of features, the images are first segmented into superpixels and then random subsets of superpixels are masked. This improves the signal-to-noise ratio of the metric for high-resolution images.

The Sensitivity-n metric is computed for each masker in maskers and for each activation function in activation_fns.

Parameters:
modelnn.Module

Model to compute Sensitivity-n for.

attributions_datasetAttributionsDataset

Dataset containing the attributions to compute Sensitivity-n on.

batch_sizeint

Batch size to use when computing model output on masked samples.

maskersDict[str, Masker]

Dictionary of maskers to use. Keys are the names of the maskers.

activation_fnsUnion[Tuple[str], str]

Activation functions to use. If a single string is passed, then the it is converted to a single-element list.

min_subset_sizefloat

Minimum percentage of features to mask.

max_subset_sizefloat

Maximum percentage of features to mask.

num_stepsint

Number of steps between min_subset_size and max_subset_size.

num_subsetsint

Number of random subsets to generate for each value of n.

segmentedbool

If True, then the Seg-Sensitivity-n metric is computed.

devicetorch.device, optional

Device to use, by default torch.device(“cpu”)

Returns:
SensitivityNResult
Return type:

SensitivityNResult