fn

randn_like

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

Return a N(0,1)\mathcal{N}(0,1) random tensor with the same shape, dtype, and device as t.

Equivalent to lucid.randn(t.shape, dtype=t.dtype, device=t.device), inferring all metadata from an existing tensor.

Each element is drawn independently from the standard normal:

XiN(0,1),fX(x)=12πex2/2X_i \sim \mathcal{N}(0, 1), \quad f_X(x) = \frac{1}{\sqrt{2\pi}}\,e^{-x^2/2}

Parameters

tTensor
Reference tensor whose shape, dtype, and device are inherited unless overridden.
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. Default: False.

Returns

Tensor

Standard-normal random tensor shaped like t.

Notes

A common pattern in noise injection regularisation is to add scaled Gaussian noise to activations or weights during training:

x~=x+ϵN(0,I),ϵ1\tilde{x} = x + \epsilon \cdot \mathcal{N}(0, I), \quad \epsilon \ll 1

randn_like makes this concise and device-safe:

.. code-block:: python

x_noisy = x + noise_scale * lucid.randn_like(x)

Examples

>>> import lucid
>>> w = lucid.zeros(256, 512, dtype=lucid.float16, device="metal")
>>> noise = lucid.randn_like(w)
>>> noise.shape, noise.dtype, noise.device
((256, 512), lucid.float16, lucid.device('metal'))