fn
zeros_like
→Tensorzeros_like(t: Tensor, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)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
where .
Parameters
tTensorReference tensor. Its
shape, dtype, and device are
used as defaults for the output.dtypelucid.dtypeOverride the data type. When
None (default), inherits
t.dtype.devicestr or lucid.deviceOverride the device. When
None (default), inherits
t.device.requires_gradboolIf
True, downstream operations are tracked by autograd.
Default: False.Returns
TensorZero 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:
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_)