fn

zeros

Tensor
zeros(size: _int | tuple[_int, ...] = (), dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

Return a tensor filled with the additive identity element, zero.

Allocates a new tensor of the requested shape and fills every element with the scalar value 00. The result is the unique tensor satisfying

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

where (s1,,sn)(s_1, \ldots, s_n) is the requested shape.

Zero-initialisation is the standard starting point for accumulation buffers (gradient accumulators, running statistics in batch norm, confusion matrices) and for masking computations where a neutral additive element is required.

Parameters

*sizeint or tuple[int, ...]
Shape of the output tensor. Can be passed as separate positional integers zeros(2, 3) or as a single tuple zeros((2, 3)).
dtypelucid.dtype
Scalar data type of the output. Defaults to the global default dtype (lucid.float32 unless changed with lucid.set_default_dtype).
devicestr or lucid.device
Target device — "cpu" (Apple Accelerate) or "metal" (Apple Metal GPU). Defaults to the global default device.
requires_gradbool
If True, operations on the returned tensor are recorded by the autograd engine. Default: False.

Returns

Tensor

Tensor of shape size filled with zeros.

Notes

On Metal, the allocation and fill are performed on-device without a host round-trip. Gradient of zeros with respect to any upstream variable is always zero by linearity, so it is never placed on the computation graph as a leaf that affects gradients — but downstream operations on it are tracked when requires_grad=True.

Examples

>>> import lucid
>>> lucid.zeros(3).tolist()
[0.0, 0.0, 0.0]
>>> lucid.zeros(2, 3).shape
(2, 3)
>>> lucid.zeros((4,), dtype=lucid.int32).dtype
lucid.int32
Accumulate squared errors manually:
>>> acc = lucid.zeros(1)
>>> for x in [1.0, -2.0, 3.0]:
...     acc = acc + lucid.tensor(x) ** 2