fn
eye
→Tensoreye(n: _int, m: _int | None = None, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)Return a 2-D matrix with ones on the main diagonal and zeros elsewhere.
The returned matrix is defined by the Kronecker delta:
When this is the identity matrix , which satisfies for any matrix . Rectangular variants () arise naturally as the pseudo-identity in least-squares problems and in constructing projection matrices.
Parameters
nintNumber of rows.
mintNumber of columns. Defaults to
n, yielding a square identity.dtypelucid.dtypeScalar data type. Defaults to the global default dtype
(
lucid.float32 unless overridden).devicestr or lucid.deviceTarget device —
"cpu" or "metal".requires_gradboolIf
True, downstream operations are tracked by autograd.
Default: False.Returns
Tensor2-D tensor of shape (n, m) with .
Notes
The identity matrix is its own inverse and its own transpose: . Its eigenvalues are all , and it is simultaneously orthogonal, symmetric, idempotent (), 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 is exactly .
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.)