zeros
→Tensorzeros(size: _int | tuple[_int, ...] = (), dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)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 . The result is the unique tensor satisfying
where 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, ...]zeros(2, 3) or as a single tuple zeros((2, 3)).dtypelucid.dtypelucid.float32 unless changed with
lucid.set_default_dtype).devicestr or lucid.device"cpu" (Apple Accelerate) or "metal"
(Apple Metal GPU). Defaults to the global default device.requires_gradboolTrue, operations on the returned tensor are recorded by the
autograd engine. Default: False.Returns
TensorTensor 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