Compute the Pseudoinverse of a Matrix in PyTorch

[ad_1]

On this article, we’re going to focus on find out how to compute the pseudoinverse of a matrix in Python utilizing PyTorch.

torch.linalg.pinv() methodology

torch.linalg.pinv() methodology accepts a matrix and a batch of matrices as enter and returns a brand new tensor with the pseudoinverse of the enter matrix. if the enter is a batch of matrices then the output tensor additionally has the identical batch dimensions. This methodology additionally helps the enter of float, double, cfloat, and cdouble dtypes. The under syntax is used to compute the pseudoinverse of a matrix.

Syntax: torch.linalg.pinv(inp)

Parameters:

  • inp: The place inp is a Matrix of Order M x N or a batch of matrices.

Returns: it would returns a brand new tensor with the pseudoinverse of the enter matrix.

Instance 1:

On this instance, we’ll perceive find out how to compute the pseudoinverse of a matrix in PyTorch.

Python3

import torch

  

inp = torch.tensor([[0.1150, -1.1121

                     0.2334, -0.2321],

                    [1.2753, 1.0699,

                     0.2335, 1.0177],

                    [0.3942, -1.0498

                     -0.0486, 0.3240]])

  

print("n Enter matrix: n", inp)

  

output = torch.linalg.pinv(inp)

  

print("n After Compute pseudoinverse of matrix: n",

      output)

Output:

 

Instance 2:

On this instance, we’ll compute the pseudoinverse of a batch of matrices in PyTorch.

Python3

import torch

  

inp = torch.tensor([[[1.1232, 0.2341, 0.1323],

                     [-1.0562, 0.1897, 0.1276]],

                    [[-1.0200, -1.1121, 1.0321],

                     [1.0887, -1.0564, 0.1798]]])

  

print("n Batch of matrix: n", inp)

  

output = torch.linalg.pinv(inp)

  

print("n After Compute pseudoinverse: n",

      output)

Output:

 

[ad_2]

Leave a Reply