Skip to content

maskers

Classes:

Name Description
BatchwiseChannelwisePatchwisePercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor per batch element, per color channel, per patch.

BatchwisePercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor per batch element.

Masker

Defines an interface for masking input values.

PercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor.

ThresholdMasker

Defines an interface for constructing a mask from a threshold computed over an input tensor.

BatchwiseChannelwisePatchwisePercentMasker

Bases: PercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor per batch element, per color channel, per patch.

This class is designed to be used with 2D convolutional layers that output tensors of shape (batch_size, color_channels * patch_area, patches_high, patches_wide).

Parameters:

Name Type Description Default

percent_to_mask

float

A scalar value between 0 and 1 inclusive, indicating the percentage of the largest input values to mask.

required

color_channels

int

The number of color channels in the input tensor.

required

patch_size

int | tuple[int, int] | Sequence[int]

The dimension of the non-overlapping rectangular patches the image is tiled with.

required

Methods:

Name Description
forward

Construct a mask over the input tensor.

reset_parameters

Reinitialize parameters and buffers.

forward

forward(
    input: Tensor, selector_mask: Tensor | None = None
) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default

input

Tensor

The tensor over which to compute the thresholds.

required

selector_mask

Tensor | None

Unsupported. In the parent class, a mask tensor to use to select a subset of the elements of input when computing the threshold. torch.Tensor.masked_select results in a flattened tensor, and because BatchwiseChannelwisePatchwisePercentMasker operates per patch, per color channel, it cannot accept a flattened input.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

Raises:

Type Description
ValueError

If selector_mask is not None.

reset_parameters

reset_parameters() -> None

Reinitialize parameters and buffers.

This method is useful for initializing tensors created on the meta device.

Raises:

Type Description
ValueError

If percent_to_mask is not a scalar.

ValueError

If percent_to_mask is not between 0 and 1 inclusive.

BatchwisePercentMasker

Bases: PercentMasker

Constructs a mask based on a percentage of the highest values in the input tensor per batch element.

Methods:

Name Description
forward

Construct a mask over the input tensor.

reset_parameters

Reinitialize parameters and buffers.

forward

forward(
    input: Tensor, selector_mask: Tensor | None = None
) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default

input

Tensor

The tensor over which to compute the threshold(s).

required

selector_mask

Tensor | None

A mask tensor to use to select a subset of the elements of input when computing the threshold. If None, all elements of input are used.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

reset_parameters

reset_parameters() -> None

Reinitialize parameters and buffers.

This method is useful for initializing tensors created on the meta device.

Raises:

Type Description
ValueError

If percent_to_mask is not a scalar.

ValueError

If percent_to_mask is not between 0 and 1 inclusive.

Masker

Bases: Module, ABC

Defines an interface for masking input values.

Maskers should return a boolean tensor of the same shape as the input tensor.

Added in version 0.30.0.

Methods:

Name Description
reset_parameters

Reinitialize parameters and buffers.

reset_parameters abstractmethod

reset_parameters() -> None

Reinitialize parameters and buffers.

This method is useful for initializing tensors created on the meta device.

PercentMasker

Bases: ThresholdMasker

Constructs a mask based on a percentage of the highest values in the input tensor.

Parameters:

Name Type Description Default

percent_to_mask

float

A scalar value between 0 and 1 inclusive, indicating the percentage of the largest input values to mask.

required

Methods:

Name Description
forward

Construct a mask over the input tensor.

reset_parameters

Reinitialize parameters and buffers.

forward

forward(
    input: Tensor, selector_mask: Tensor | None = None
) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default

input

Tensor

The tensor over which to compute the threshold(s).

required

selector_mask

Tensor | None

A mask tensor to use to select a subset of the elements of input when computing the threshold. If None, all elements of input are used.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

reset_parameters

reset_parameters() -> None

Reinitialize parameters and buffers.

This method is useful for initializing tensors created on the meta device.

Raises:

Type Description
ValueError

If percent_to_mask is not a scalar.

ValueError

If percent_to_mask is not between 0 and 1 inclusive.

ThresholdMasker

Bases: Masker

Defines an interface for constructing a mask from a threshold computed over an input tensor.

Methods:

Name Description
forward

Construct a mask over the input tensor.

reset_parameters

Reinitialize parameters and buffers.

forward

forward(
    input: Tensor, selector_mask: Tensor | None = None
) -> torch.Tensor

Construct a mask over the input tensor.

Parameters:

Name Type Description Default

input

Tensor

The tensor over which to compute the threshold(s).

required

selector_mask

Tensor | None

A mask tensor to use to select a subset of the elements of input when computing the threshold. If None, all elements of input are used.

None

Returns:

Type Description
torch.Tensor

A mask tensor of the same shape as input.

reset_parameters abstractmethod

reset_parameters() -> None

Reinitialize parameters and buffers.

This method is useful for initializing tensors created on the meta device.