attribench.masking.Masker
- class attribench.masking.Masker[source]
Bases:
objectBase class for all maskers. Maskers are used to “remove” features from a sample by masking them with some value. This can be a fixed baseline value, a random value, or some other value.
Note that a Masker object is not yet usable after creation. You need to call
set_batch()first, to set the samples and attributions. This allows the same Masker object to be used for multiple batches.Methods
Return the number of features in the samples.
Mask the
kleast important features, according to the attributions.Mask
krandom features.Mask the
kmost important features, according to the attributions.Set the samples and attributions for the next batch.
- get_num_features()[source]
Return the number of features in the samples.
- Returns:
- int
Number of features in the samples.
- Return type:
int
- mask_bot(k)[source]
Mask the
kleast important features, according to the attributions.- Parameters:
- kint
Number of features to mask.
- Returns:
- torch.Tensor
Samples with the bottom k features masked.
- Return type:
Tensor
- mask_rand(k, return_indices=False)[source]
Mask
krandom features.- Parameters:
- kint
Number of features to mask.
- return_indicesbool, optional
Whether to return the indices of the masked features, by default False
- Returns:
- torch.Tensor
Samples with k random features masked.
- Return type:
Union[Tensor,Tuple[Tensor,Tensor]]
- mask_top(k)[source]
Mask the
kmost important features, according to the attributions.- Parameters:
- kint
Number of features to mask.
- Returns:
- torch.Tensor
Samples with the top k features masked.
- Return type:
Tensor
- abstract set_batch(samples, attributions=None)[source]
Set the samples and attributions for the next batch.
- Parameters:
- samplestorch.Tensor
Samples of shape
[num_samples, *sample_shape].- attributionstorch.Tensor, optional
Attributions of shape
[num_samples, *sample_shape], by default None If None, themask_top()andmask_bot()methods will not be available.