PyTorch torch.linalg.inv function
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:
A(Tensor): Input square matrix, which must be invertible.
Return Value:
torch.Tensor: Returns the inverse of the matrix.
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
)
(
"Matrix A:"
)
(
A
)
(
"nInverse matrix A^-1:"
)
(
A_inv
)
(
"nVerify: A @ A^-1 ="
)
(
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.]])