fn

eye

Tensor
eye(n: _int, m: _int | None = None, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

Return a 2-D matrix with ones on the main diagonal and zeros elsewhere.

The returned matrix ERn×mE \in \mathbb{R}^{n \times m} is defined by the Kronecker delta:

Eij=δij={1if i=j0if ijE_{ij} = \delta_{ij} = \begin{cases} 1 & \text{if } i = j \\ 0 & \text{if } i \neq j \end{cases}

When n=mn = m this is the identity matrix InI_n, which satisfies InA=AIn=AI_n A = A I_n = A for any n×nn \times n matrix AA. Rectangular variants (nmn \neq m) arise naturally as the pseudo-identity in least-squares problems and in constructing projection matrices.

Parameters

nint
Number of rows.
mint
Number of columns. Defaults to n, yielding a square identity.
dtypelucid.dtype
Scalar data type. Defaults to the global default dtype (lucid.float32 unless overridden).
devicestr or lucid.device
Target device — "cpu" or "metal".
requires_gradbool
If True, downstream operations are tracked by autograd. Default: False.

Returns

Tensor

2-D tensor of shape (n, m) with Eij=δijE_{ij} = \delta_{ij}.

Notes

The identity matrix is its own inverse and its own transpose: In1=In=InI_n^{-1} = I_n^\top = I_n. Its eigenvalues are all 11, and it is simultaneously orthogonal, symmetric, idempotent (I2=II^2 = I), and unitary.

In deep learning, identity initialisations are used in recurrent networks (e.g. the IRNN, Le et al. 2015) to preserve gradient norms across time steps, exploiting the fact that the spectral radius of II is exactly 11.

Examples

>>> import lucid
>>> lucid.eye(3)
Tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
Rectangular variant:
>>> lucid.eye(2, 4)
Tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.]])
Verify $I A = A$:
>>> A = lucid.tensor([[1., 2.], [3., 4.]])
>>> (lucid.eye(2) @ A - A).abs().max()
Tensor(0.)