Skip to content
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)

Implementing kernel

C++ engine symbols that back this Python API.

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

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

Xi∼N(μ, σ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 or Tensor= 0.0
Mean μ\mu of the distribution. Default: 0.0. A tensor selects the reference's tensor overloads (see Notes).
stdfloat or Tensor= 1.0
Standard deviation σ≥0\sigma \ge 0. Default: 1.0.
sizelist[int] or tuple[int, ...]
Shape of the output tensor. Required with a float mean and std (keyword, or third positional); none with a tensor.
dtypelucid.dtype= None
Floating-point data type. Defaults to the global default.
devicestr or lucid.device= None
Target device — "cpu" or "metal".
requires_gradbool= False
Enable autograd tracking. Default: False.
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,Z∼N(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, ...).

With a tensor mean or std it draws one sample per element of their broadcast shape, in the dtype they promote to, with no gradient to either; only generator= is taken besides.

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
>>> lucid.normal(lucid.zeros(2, 3), lucid.ones(3)).shape  # per-element
(2, 3)
>>> lucid.normal(0.0, 1.0, (4,)).shape  # shape by position
(4,)
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))