PyTorch torch.cholesky_inverse function
PyTorch torch reference manual
torch.cholesky_inverse is a function in PyTorch used to compute the inverse of a Cholesky decomposition. It uses the result of the Cholesky decomposition to compute the inverse of a matrix efficiently.
Function definition
torch.cholesky_inverse(L, upper=False, out=None)
Parameters:
L(Tensor): The upper or lower triangular matrix obtained from the Cholesky decomposition.upper(bool, optional): If True, L is an upper triangular matrix; otherwise it is a lower triangular matrix. Defaults to False.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the inverse of the original matrix.
Usage examples
Example
import
torch
Create a symmetric positive-definite matrix
A
=
torch.
tensor
(
[
[
4.0
,
2.0
,
2.0
]
,
[
2.0
,
5.0
,
3.0
]
,
[
2.0
,
3.0
,
6.0
]
]
,
dtype
=
torch.
float64
)
Cholesky decomposition
L
=
torch.
cholesky
(
A
)
Compute the inverse matrix using Cholesky decomposition
A_inv
=
torch.
cholesky_inverse
(
L
)
(
"Original matrix A:"
)
(
A
)
(
"nInverse matrix A^-1:"
)
(
A_inv
)
(
"nVerify: A @ A^-1 ="
)
(
A
@
A_inv
)
The output is:
原矩阵 A:
tensor([[4., 2., 2.],
[2., 5., 3.],
[2., 3., 6.]], dtype=torch.float64)
逆矩阵 A^-1:
tensor([[ 0.7500, -0.5000, -0.2500],
[-0.5000, 1.0000, -0.0000],
[-0.2500, -0.0000, 0.2500]], dtype=torch.float64)
验证: A @ A^-1 =
tensor([[ 1.0000e+00, -1.4901e-08, 0.0000e+00],
[-7.4506e-09, 1.0000e+00, 1.4901e-08],
[ 0.0000e+00, 7.4506e-09, 1.0000e+00]], dtype=torch.float64)