Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.eye

Creates a two-dimensional tensor with ones on its diagonal and zeros elsewhere. It can produce square or rectangular shapes and accepts options for dtype, layout, device, output reuse, and gradient tracking.

How it works

torch.eye creates a two-dimensional tensor of shape (n, m) whose diagonal entries are 1 and all other entries are 0. Omitting m makes the tensor square; keyword options control storage, device, data type, and gradient tracking.

Syntax

torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)

Parameters

NameTypeDescription
nintNumber of rows.
mint, optionalOptional number of columns; defaults to n.
outTensor, optionalOptional destination tensor.
dtypetorch.dtype, optionalOptional element type; defaults through the global default dtype when omitted.
layouttorch.layout, optionalOptional tensor layout; defaults to torch.strided.
devicetorch.device, optionalOptional target device.
requires_gradbool, optionalWhether autograd records operations on the returned tensor; defaults to False.

Returns

A 2-D Tensor containing ones on the diagonal and zeros elsewhere.

Key points

  • The result is always two-dimensional, with diagonal entries equal to one and off-diagonal entries equal to zero.
  • The signature exposes layout, device, and requires_grad controls in addition to shape and output options.
  • Use dtype to choose the element type of the created tensor.
  • Use layout to request the tensor storage layout.
  • When device is omitted, PyTorch selects the current device associated with the default tensor type.

Common mistakes

  • If m is omitted, the result has n columns and is square; provide m explicitly for a rectangular tensor.
  • Gradient tracking is disabled by default; enable it explicitly when the created tensor should participate in autograd.
  • Leaving dtype as None delegates the returned tensor's data type to PyTorch's global default dtype.

Example: Create identity-style tensors

import torch

identity = torch.eye(3, dtype=torch.float32)
rectangular = torch.eye(2, 3, dtype=torch.float64)
print(identity)
print(rectangular)

Creates square and rectangular two-dimensional tensors with ones on the main diagonal.

When to use it

Initialize an identity matrix for linear algebra or model components.

Identity matrices

Build a 2-D tensor whose diagonal extends across unequal row and column dimensions.

Rectangular diagonal masks

Specify dtype, layout, device, or gradient behavior at construction time.

Controlled tensor creation

Interview questions

QWhat does torch.eye return?

torch.eye(n, m=None, ...) returns a 2-D tensor with ones on the diagonal and zeros elsewhere. With only n, it produces an n-by-n tensor; m changes the column count.

Follow-up: How would you create a rectangular 2-by-3 tensor? Call torch.eye(2, 3), optionally supplying dtype or device keywords.

QWhat are the important parameters?

The function accepts n as the row count and optional m as the column count. It also supports out, dtype, layout, device, and requires_grad keyword arguments.

Follow-up: How can the tensor be placed on an accelerator? Pass an appropriate device, such as device='cuda' when CUDA is available.

QDoes torch.eye track gradients by default?

requires_grad defaults to False, so autograd does not record operations on the returned tensor unless requested. Set requires_grad=True when gradient tracking is needed.

Follow-up: What controls the element type if dtype is omitted? PyTorch uses the global default dtype.

  • torch.ones
  • torch.zeros
  • torch.empty
  • torch.full
  • torch.nn.init.eye_

Source: torch.eye — PyTorch 2.14 documentation. Checked 2026-10-16.