PyTorch torch.linalg.inv Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.linalg.invis a function in PyTorch's linear algebra module used to compute the inverse of a matrix. It istorch.inversethe recommended alternative function.

Function Definition

torch.linalg.inv(A)

Parameters:

  • A(Tensor): Input square matrix, must be invertible.

Return Value:

  • torch.Tensor: Returns the inverse of the matrix.

Usage Example

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("\nVerification: A @ A^-1 =")
print(A @ A_inv)

The output result 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 参考手册Pytorch torch Reference Manual

Other Extensions