fn

zeros_like

Tensor
zeros_like(t: Tensor, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

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

Equivalent to lucid.zeros(t.shape, dtype=t.dtype, device=t.device), but infers the metadata from an existing tensor so the caller need not repeat it. Every element satisfies

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

where (s1,,sn)=t.shape(s_1, \ldots, s_n) = \texttt{t.shape}.

Parameters

tTensor
Reference tensor. Its shape, dtype, and device are used as defaults for the output.
dtypelucid.dtype
Override the data type. When None (default), inherits t.dtype.
devicestr or lucid.device
Override the device. When None (default), inherits t.device.
requires_gradbool
If True, downstream operations are tracked by autograd. Default: False.

Returns

Tensor

Zero tensor with the same shape (and optionally dtype/device) as t.

Notes

A common use case is zeroing out a gradient accumulator that has the same shape as a parameter tensor:

g0like(θ)g \leftarrow \mathbf{0}_{\text{like}(\theta)}

This avoids hard-coding shape constants and ensures dtype consistency (e.g. float16 parameters get float16 zero gradients).

Examples

>>> import lucid
>>> w = lucid.randn(3, 4, dtype=lucid.float16, device="metal")
>>> g = lucid.zeros_like(w)
>>> g.shape, g.dtype, g.device
((3, 4), lucid.float16, lucid.device('metal'))
Override dtype on-the-fly:
>>> mask = lucid.zeros_like(w, dtype=lucid.bool_)