PyTorch torch.cholesky_inverse Function
PyTorch torch Reference Manual
torch.cholesky_inverseIt is a function in PyTorch used to compute the inverse of a Cholesky decomposition. It uses the result of the Cholesky decomposition to efficiently compute the inverse of a matrix.
Function Definition
torch.cholesky_inverse(L, upper=False, out=None)
Parameters:
L(Tensor): The upper or lower triangular matrix obtained from Cholesky decomposition.upper(bool, optional): If True, L is an upper triangular matrix; otherwise, it is a lower triangular matrix. Default is False.out(Tensor, optional): Output tensor.
Return Value:
torch.Tensor: Returns the inverse of the original matrix.
Usage Example
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)
print("Original matrix A:")
print(A)
print("nInverse matrix A^-1:")
print(A_inv)
print("nVerification: A @ A^-1 =")
print(A @ A_inv)
# 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)
print("Original 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([[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)
Other Extensions