fn

masked_fill

Tensor
masked_fill(input: Tensor, mask: Tensor, value: float)
source

Return input with positions where mask is True set to value.

The mask must be broadcast-compatible with input. Out-of-place: a fresh tensor is returned; input is not modified.

Parameters

inputTensor
Source tensor.
maskTensor
Boolean tensor broadcastable to input.
valuefloat
Scalar fill value.

Returns

Tensor

Tensor of the same shape and dtype as input.

Notes

Often used to apply attention masks:

outi  =  {valueif maskiinputiotherwise.\text{out}_i \;=\; \begin{cases} \text{value} & \text{if } \text{mask}_i \\ \text{input}_i & \text{otherwise} . \end{cases}

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> m = lucid.tensor([True, False, True])
>>> lucid.masked_fill(x, m, 0.0)
Tensor([0., 2., 0.])