fn

ones_like

Tensor
ones_like(t: Tensor, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

Return an all-ones tensor with the same shape, dtype, and device as t.

Equivalent to lucid.ones(t.shape, dtype=t.dtype, device=t.device), but infers metadata from an existing tensor. Every element 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)

Parameters

tTensor
Reference tensor whose shape, dtype, and device are used as defaults.
dtypelucid.dtype
Override the data type. Defaults to t.dtype.
devicestr or lucid.device
Override the device. Defaults to t.device.
requires_gradbool
Enable autograd tracking on the output. Default: False.

Returns

Tensor

All-ones tensor shaped like t.

Notes

Multiplicative identity initialisation is used in layer-normalisation and group-normalisation to set the learnable scale parameter γ\gamma to 11 at the start of training, so the network begins as a pure normalisation with no learned rescaling:

x^=γxμσ+β,γ0=1,  β0=0\hat{x} = \gamma \cdot \frac{x - \mu}{\sigma} + \beta, \quad \gamma_0 = \mathbf{1},\; \beta_0 = \mathbf{0}

Examples

>>> import lucid
>>> x = lucid.randn(2, 8)
>>> gamma = lucid.ones_like(x)
>>> gamma.shape
(2, 8)
Initialise scale parameters of a normalisation layer:
>>> weight = lucid.ones_like(x, requires_grad=True)