fn
ones
→Tensorones(size: _int | tuple[_int, ...] = (), dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)Return a tensor filled with the multiplicative identity element, one.
Allocates a new tensor of the requested shape and fills every element with the scalar value . Every entry satisfies
One-initialisation is the canonical starting point for multiplicative accumulators, scale parameters in normalisation layers (before any training), and homogeneous coordinate vectors in projective geometry.
Parameters
*sizeint or tuple[int, ...]Shape of the output tensor. Accepts separate ints
ones(2, 3) or a single tuple ones((2, 3)).dtypelucid.dtypeScalar data type. Defaults to the global default dtype
(
lucid.float32 unless overridden).devicestr or lucid.deviceTarget device —
"cpu" or "metal".requires_gradboolIf
True, downstream operations are tracked by autograd.
Default: False.Returns
TensorTensor of shape size filled with ones.
Notes
The all-ones vector plays an important role in matrix algebra: multiplying a matrix on the right by computes its row sums, . This is useful when implementing attention masks, normalisation denominators, and indicator functions.
Examples
>>> import lucid
>>> lucid.ones(4).tolist()
[1.0, 1.0, 1.0, 1.0]
>>> lucid.ones(2, 2, dtype=lucid.int8)
Tensor([[1, 1],
[1, 1]], dtype=int8)
Row-sum via matrix-vector product:
>>> A = lucid.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> row_sums = A @ lucid.ones(2)
>>> row_sums.tolist()
[3.0, 7.0]