Skip to content
fn

orthogonal

→Tensor
orthogonal_(tensor: Tensor, gain: float = 1.0, generator: _C_engine.Generator | None = None)

Initialise tensor in-place with a (semi-)orthogonal matrix.

A random Gaussian matrix is drawn and orthonormalised via QR decomposition; the resulting matrix Q is multiplied by gain and written into tensor. For tensors with rank > 2 the leading axis is flattened against the remaining axes, the 2-D matrix is orthogonalised, and the original shape is restored.

Orthogonal initialisation, proposed by Saxe et al. (2013), preserves vector norms exactly through deep linear networks (∥Wx∥2=∥x∥2\|Wx\|_2 = \|x\|_2 because W has orthonormal columns), keeping the spectrum of the forward Jacobian on the unit sphere. This is particularly valuable for RNNs and pre-norm transformer blocks where activation magnitudes can otherwise drift exponentially with depth.

Parameters

tensorTensor
Tensor to initialise in place; must have at least 2 dimensions.
gainfloat= 1.0
Multiplicative scaling applied to Q. Default 1.0.
generatorGenerator= None
PRNG used to draw the pre-QR Gaussian matrix. Default None.

Returns

Tensor

tensor populated with a Haar-distributed orthogonal matrix.

Raises

ValueError
If tensor.ndim < 2.

Notes

The matrix satisfies Q⊤Q=gain2IQ^\top Q = \text{gain}^2 I (or QQ⊤=gain2IQ Q^\top = \text{gain}^2 I when fan-out exceeds fan-in).

Examples

>>> import lucid
>>> from lucid.nn.init import orthogonal_
>>> w = lucid.empty(64, 64)
>>> _ = orthogonal_(w, gain=1.0)
>>> bool(lucid.allclose(w.mT @ w, lucid.eye(64), atol=1e-4))
True