Python PyTorch – How to create an Identity Matrix?

PyTorchServer Side ProgrammingProgramming

Identity matrix, also known as Unit matrix, is a "n ☓ n" square matrix with 1's on the main diagonal and 0's elsewhere. It is the multiplicative identity of square matrices. because any square matrix multipliedUnit matrix is also called the identity matrix. Unit matrix is used as the multiplicative identity of square matrices in the matrices concept. When any square matrix is multiplied by the identity matrix, then the result doesn't change. In linear algebra, the unit matrix of size n is the n ☓ n square matrix with ones on the main diagonal and zeros elsewhere.

To create an identity matrix, we use the torch.eye() method. This method takes the number of rows as the parameter. The number of columns are by default set to the number of rows. You may change the number of rows by providing it as a parameter. This method returns a 2D tensor (matrix) whose diagonals are 1's and all other elements are 0.

Syntax

torch.eye(n)

where n is the number of rows of the matrix. By default, the number of columns are the same as the number of rows. We can provide a second parameter as the number of columns.

Steps

You can use the following steps to create a matrix with 1's on the diagonal and 0's elsewhere

  • Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
  • Create a 2-D tensor (matrix) with 1's on the diagonal and 0's elsewhere.

M = torch.eye(4)
  • Print the above computed matrix (2D tensor)

print(M)

Example 1

In the following example, we will create a set of square matrices with 1's at the diagonal and 0's elsewhere.

# Import the required library
import torch

# Create a 2D tensor with 1's on the diagonal and 0's elsewhere
t = torch.eye(4)

# print the computed tensor
print(t)

# other way to do above task
t1 = torch.eye(4,4)
print(t1)

t2 = torch.eye(3,4)
print(t2)

t3 = torch.eye(4,3)
print(t3)

Output

tensor([[1., 0., 0., 0.],
   [0., 1., 0., 0.],
   [0., 0., 1., 0.],
   [0., 0., 0., 1.]])
tensor([[1., 0., 0., 0.],
   [0., 1., 0., 0.],
   [0., 0., 1., 0.],
   [0., 0., 0., 1.]])
tensor([[1., 0., 0., 0.],
   [0., 1., 0., 0.],
   [0., 0., 1., 0.]])
tensor([[1., 0., 0.],
   [0., 1., 0.],
   [0., 0., 1.],
   [0., 0., 0.]])

Example 2

# Create tensor with requires_grad true
# Import the required library
import torch

# Create a 2D tensor with 1's on the diagonal and 0's elsewhere
t = torch.eye(5, requires_grad = True)

# print the above computed tensor
print(t)

# other way to do above task
t1 = torch.eye(4,5, requires_grad = True)
print(t1)

Output

tensor([[1., 0., 0., 0., 0.],
   [0., 1., 0., 0., 0.],
   [0., 0., 1., 0., 0.],
   [0., 0., 0., 1., 0.],
   [0., 0., 0., 0., 1.]], requires_grad=True)
tensor([[1., 0., 0., 0., 0.],
   [0., 1., 0., 0., 0.],
   [0., 0., 1., 0., 0.],
   [0., 0., 0., 1., 0.]], requires_grad=True)
raja
Updated on 20-Jan-2022 07:27:06

Advertisements