Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.linalg.inv function

PyTorch torch.linalg.inv function


PyTorch torch reference manual PyTorch torch reference manual

torch.linalg.inv is a function in PyTorch's linear algebra module used to compute the inverse of a matrix. It is the torch.inverse recommended replacement.

Function definition

torch.linalg.inv(A)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create an invertible matrix

A

=

torch.

tensor

(

[

[

1.0

,

2.0

]

,

                 

[

3.0

,

4.0

]

]

)

Compute the inverse matrix

A_inv

=

torch.

linalg

.

inv

(

A

)

print

(

"Matrix A:"

)

print

(

A

)

print

(

"nInverse matrix A^-1:"

)

print

(

A_inv

)

print

(

"nVerify: A @ A^-1 ="

)

print

(

A

@

A_inv

)

The output is:

矩阵 A:

tensor([[1., 2.],

        [3., 4.]])

逆矩阵 A^-1:

tensor([[-2.0000,  1.0000],

        [ 1.5000, -0.5000]])

验证: A @ A^-1 =

tensor([[1., 0.],

        [0., 1.]])


PyTorch torch reference manual PyTorch torch reference manual