Python

Tensor Matrix functions linalg: inv, pinv, det, diagnol

In this PyTorch tutorial, we will discuss the torch.linalg.inv(), torch.linalg.pinv(), torch.linalg.det(), and torch.linalg.diagonal() functions performed on tensor matrix.

PyTorch is an open-source framework available with a Python programming language.

A tensor is a multidimensional array that is used to store the data. For using a tensor, we have to import the torch module.

To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where data is a multi-dimensional array.

torch.linalg.inv() Function

The torch.linalg.inv() function returns the inverse of the given matrix tensor.

Syntax:

torch.linalg.inv(tensor_object)

Parameter:

It takes tensor_object as a parameter. It has to be two-dimensional.

Example

In this example, we will create a matrix that has 4 rows and 4 columns and return the inverse matrix using the torch.linalg.inv().

#import torch module
import torch
 
 
#create  tensor matrix
data1 = torch.tensor([[2.0,4.0,5.0,3.2],[3.4,5.6,7.8,9.0], [2.1,3.2,4.3,5.6], [5.4,3.2,2.3,7.8]])  

#display
print("Actual Tensor matrix: ")
print(data1)

print("Inverse matrix: ")
#return inverse of the above matrix
print(torch.linalg.inv(data1))

Output:

Actual Tensor matrix:
tensor([[2.0000, 4.0000, 5.0000, 3.2000],
        [3.4000, 5.6000, 7.8000, 9.0000],
        [2.1000, 3.2000, 4.3000, 5.6000],
        [5.4000, 3.2000, 2.3000, 7.8000]])
Inverse matrix:
tensor([[-0.3627,  3.0709, -5.4110,  0.4902],
        [ 1.4628, -5.8971,  9.2172, -0.4132],
        [-0.7418,  4.0696, -6.3933,  0.1987],
        [-0.1303, -0.9067,  1.8498, -0.1002]])

The Inverse matrix is returned from the actual matrix.

torch.linalg.pinv() Function

The torch.linalg.inv() function returns the pseudo inverse matrix of the given matrix tensor.

Syntax:

torch.linalg.pinv(tensor_object)

Parameter:

It takes tensor_object as a parameter. It has to be two-dimensional.

Example

In this example, we will create a matrix that has 4 rows and 4 columns and return a pseudo inverse matrix using torch.linalg.pinv().

#import torch module
import torch
#create  tensor matrix
data1 = torch.tensor([[2.0,4.0,5.0,3.2], [3.4,5.6,7.8,9.0], [2.1,3.2,4.3,5.6], [5.4,3.2,2.3,7.8]])  

#display
print("Actual Tensor matrix: ")
print(data1)

print("Pseudo Inverse matrix: ")
#return Pseudo inverse of the above matrix
print(torch.linalg.pinv(data1))

Output:

Actual Tensor matrix:
tensor([[2.0000, 4.0000, 5.0000, 3.2000],
        [3.4000, 5.6000, 7.8000, 9.0000],
        [2.1000, 3.2000, 4.3000, 5.6000],
        [5.4000, 3.2000, 2.3000, 7.8000]])
Pseudo Inverse matrix:
tensor([[-0.3627,  3.0709, -5.4110,  0.4902],
        [ 1.4628, -5.8971,  9.2172, -0.4133],
        [-0.7418,  4.0696, -6.3933,  0.1987],
        [-0.1303, -0.9067,  1.8498, -0.1002]])

Pseudo Inverse matrix is returned from the actual matrix.

torch.linalg.det() Function

The torch.linalg.det() function is used to return the determinant from the given matrix tensor.

Syntax:

torch.linalg.det(tensor_object)

Parameter:

It takes tensor_object as a parameter. It has to be two-dimensional.

Example

In this example, we will create a matrix that has 4 rows and 4 columns and return the determinant using torch.linalg.det().

#import torch module
import torch
 
#create  tensor matrix
data1 = torch.tensor([[2.0,4.0,5.0,3.2], [3.4,5.6,7.8,9.0], [2.1,3.2,4.3,5.6], [5.4,3.2,2.3,7.8]])  

#display
print("Tensor matrix: ")
print(data1)

print("Determinant: ")
#return determinant of the above matrix
print(torch.linalg.det(data1))

Output:

Tensor matrix:
tensor([[2.0000, 4.0000, 5.0000, 3.2000],
        [3.4000, 5.6000, 7.8000, 9.0000],
        [2.1000, 3.2000, 4.3000, 5.6000],
        [5.4000, 3.2000, 2.3000, 7.8000]])
Determinant:
tensor(-8.7792)

Determinant is returned from the actual matrix.

torch.linalg.diagonal() Function

The torch.linalg.diagonal() function is used to return the diagonals from the given matrix tensor.

Syntax:

torch.linalg.diagonal(tensor_object)

Parameter:

It takes tensor_object as a parameter. It has to be two-dimensional.

Example

In this example, we will create a matrix that has 4 rows and 4 columns and return the diagonals using torch.linalg.diagonal().

#import torch module
import torch
 
#create  tensor matrix
data1 = torch.tensor([[2.0,4.0,5.0,3.2],[3.4,5.6,7.8,9.0],[2.1,3.2,4.3,5.6],[5.4,3.2,2.3,7.8]])  

#display
print("Tensor matrix: ")
print(data1)

print("Diagonals: ")
#return diagonals of the above matrix
print(torch.linalg.diagonal(data1))

Output:

Tensor matrix:
tensor([[2.0000, 4.0000, 5.0000, 3.2000],
        [3.4000, 5.6000, 7.8000, 9.0000],
        [2.1000, 3.2000, 4.3000, 5.6000],
        [5.4000, 3.2000, 2.3000, 7.8000]])
Diagonals:
tensor([2.0000, 5.6000, 4.3000, 7.8000])

Diagonals are returned from the actual matrix.

Conclusion

In this PyTorch lesson, we saw four different functions applied on a tensor matrix: torch.linalg.inv() is used to return the inverse of the given matrix tensor matrix; torch.linalg.pinv() is used to return the pseudo inverse of the given matrix tensor; torch.linalg.det() is used to return the determinant from the given matrix tensor, and torch.linalg.diagonal() is used to return the diagonals from the given matrix tensor.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain