fn

ones

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

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 11. Every entry satisfies

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

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.dtype
Scalar data type. Defaults to the global default dtype (lucid.float32 unless overridden).
devicestr or lucid.device
Target device — "cpu" or "metal".
requires_gradbool
If True, downstream operations are tracked by autograd. Default: False.

Returns

Tensor

Tensor of shape size filled with ones.

Notes

The all-ones vector 1Rn\mathbf{1} \in \mathbb{R}^n plays an important role in matrix algebra: multiplying a matrix AA on the right by 1\mathbf{1} computes its row sums, A1=rowsum(A)A\mathbf{1} = \text{rowsum}(A). 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]