fn

normal

Tensor
normal(mean: _float = 0.0, std: _float = 1.0, size: list[_int] | tuple[_int, ...], dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False, generator: _C_engine.Generator | None = None)
source

Return a tensor of samples drawn from a parametric normal distribution.

Each element is drawn independently from N(μ,σ2)\mathcal{N}(\mu, \sigma^2):

XiN(μ,σ2),fX(x)=1σ2πexp ⁣((xμ)22σ2)X_i \sim \mathcal{N}(\mu,\, \sigma^2), \quad f_X(x) = \frac{1}{\sigma\sqrt{2\pi}}\, \exp\!\left(-\frac{(x-\mu)^2}{2\sigma^2}\right)

with mean E[Xi]=μ\mathbb{E}[X_i] = \mu and standard deviation Var[Xi]=σ\sqrt{\operatorname{Var}[X_i]} = \sigma.

Parameters

meanfloat
Mean μ\mu of the distribution. Default: 0.0.
stdfloat
Standard deviation σ>0\sigma > 0. Default: 1.0.
sizelist[int] or tuple[int, ...]
Shape of the output tensor. Required (keyword-only).
dtypelucid.dtype
Floating-point data type. Defaults to the global default.
devicestr or lucid.device
Target device — "cpu" or "metal".
requires_gradbool
Enable autograd tracking. Default: False.
generatorlucid._C.engine.Generator
Explicit generator. Defaults to the global default.

Returns

Tensor

Tensor of shape size with N(μ,σ2)\mathcal{N}(\mu, \sigma^2) samples.

Notes

This is a convenience wrapper around randn with an affine shift:

X=μ+σZ,ZN(0,1)X = \mu + \sigma \cdot Z, \quad Z \sim \mathcal{N}(0, 1)

The output is therefore equivalent to mean + std * lucid.randn(*size, ...).

Weight initialisation recipes commonly parameterise the normal distribution directly:

  • LeCun normal (LeCun et al., 1998): σ=1/nin\sigma = \sqrt{1 / n_\text{in}}
  • Glorot normal (Glorot & Bengio, 2010): σ=2/(nin+nout)\sigma = \sqrt{2 / (n_\text{in} + n_\text{out})}
  • He normal (He et al., 2015): σ=2/nin\sigma = \sqrt{2 / n_\text{in}}

Examples

>>> import lucid
>>> lucid.manual_seed(0)
>>> x = lucid.normal(mean=5.0, std=2.0, size=(1000,))
>>> abs(float(x.mean()) - 5.0) < 0.2
True
>>> abs(float(x.std()) - 2.0) < 0.2
True
He-normal initialisation for a Conv2d kernel:
>>> fan_in = 3 * 3 * 64          # kernel_h × kernel_w × C_in
>>> w = lucid.normal(0.0, (2.0 / fan_in) ** 0.5, size=(128, 64, 3, 3))