fn

full_like

Tensor
full_like(t: Tensor, fill_value: _float, dtype: DTypeLike = None, device: DeviceLike = None)
source

Return a constant-filled tensor with the same shape, dtype, and device as t.

Every element of the output equals fill_value:

Fi1,,in=c  (i1,,in)k[0,sk)F_{i_1, \ldots, i_n} = c \quad \forall\; (i_1, \ldots, i_n) \in \prod_k [0, s_k)

where c=fill_valuec = \texttt{fill\_value} and (s1,,sn)=t.shape(s_1, \ldots, s_n) = \texttt{t.shape}. This is the shape-aware generalisation of full.

Parameters

tTensor
Reference tensor. Shape, dtype, and device are inherited unless overridden by the keyword arguments.
fill_valuefloat
Scalar constant to broadcast across all elements.
dtypelucid.dtype
Override the data type. When specified, the result is cast via astype after allocation, so the output dtype matches the override rather than t.dtype.
devicestr or lucid.device
Override the device. When specified, the result is moved via to after allocation.

Returns

Tensor

Constant tensor shaped like t.

Notes

A canonical application is initialising the attention bias mask before selectively unmasking positions. Starting from -\infty on a tensor shaped like the attention weight matrix ensures that masked positions become zero after softmax:

logitij{logitijunmaskedmasked    softmax(logit)i  =  0 for masked j\text{logit}_{ij} \leftarrow \begin{cases} \text{logit}_{ij} & \text{unmasked} \\ -\infty & \text{masked} \end{cases} \implies \text{softmax}(\text{logit})_i \;=\; 0 \text{ for masked } j

Examples

>>> import lucid
>>> scores = lucid.randn(4, 8)
>>> mask = lucid.full_like(scores, float("-inf"))
>>> mask.shape
(4, 8)
Constant padding value:
>>> x = lucid.randn(2, 3)
>>> padded = lucid.full_like(x, -1.0)