fn

eye_

Tensor
eye_(tensor: Tensor)
source

Fill a 2-D tensor in-place with the identity matrix.

Writes ones on the main diagonal and zeros elsewhere. Identity initialisation is useful for square linear layers in residual blocks or for testing — at initialisation the layer is the identity map and gradients propagate untransformed.

Parameters

tensorTensor
Two-dimensional tensor to fill in place. Need not be square; the identity is written into the leading min(n, m) diagonal entries.

Returns

Tensor

tensor (mutated) for chaining.

Raises

ValueError
If tensor.ndim != 2.

Notes

For convolution weights that should act as the identity see dirac_, which is the spatial analogue.

Examples

>>> import lucid
>>> from lucid.nn.init import eye_
>>> w = lucid.empty(4, 4)
>>> eye_(w)