fn
randn
→Tensorrandn(size: _int | tuple[_int, ...] = (), dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False, generator: _C_engine.Generator | None = None)Return a tensor of samples drawn from the standard normal distribution.
Each element is drawn independently from , the standard Gaussian with zero mean and unit variance:
The distribution has mean , variance , and all odd central moments zero.
Parameters
*sizeint or tuple[int, ...]Shape of the output tensor.
dtypelucid.dtypeFloating-point data type. Defaults to the global default.
devicestr or lucid.deviceTarget device —
"cpu" or "metal".requires_gradboolEnable autograd tracking. Default:
False.generatorlucid._C.engine.GeneratorExplicit generator. Defaults to the global default generator.
Returns
TensorTensor of shape size with samples.
Notes
The engine uses the Box–Muller transform to convert pairs of uniform samples into standard normals:
This is exact (not approximate) and is highly efficient on SIMD hardware because the two outputs can be produced simultaneously from one pair of uniform draws.
Xavier / Glorot initialisation (Glorot & Bengio, 2010) uses with
which can be obtained as randn(fan_in, fan_out) * sigma.
He / Kaiming initialisation (He et al., 2015) for ReLU networks uses .
Examples
>>> import lucid
>>> lucid.manual_seed(0)
>>> x = lucid.randn(1000)
>>> abs(float(x.mean())) < 0.1 # ≈ 0
True
>>> abs(float(x.std()) - 1.0) < 0.1 # ≈ 1
True
Kaiming initialisation for a linear layer:
>>> fan_in = 256
>>> w = lucid.randn(fan_in, 512) * (2.0 / fan_in) ** 0.5