PyTorch torch.linalg.cholesky function
PyTorch torch reference manual
torch.linalg.cholesky is a function in PyTorch's linear algebra module (linalg) used to compute the Cholesky decomposition of a symmetric positive-definite matrix. It is the torch.cholesky recommended replacement.
Function definition
torch.linalg.cholesky(A, upper=False, out=None)
Parameters:
A(Tensor): The input symmetric positive-definite matrix.upper(bool, optional): If True, return the upper triangular matrix; otherwise return the lower triangular matrix. Defaults to False.out(Tensor, optional): the output tensor.
Return Value:
torch.Tensor: Returns the triangular matrix of the Cholesky decomposition.
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.
linalg
.
cholesky
(
A
)
(
"Original matrix A:"
)
(
A
)
(
"nCholesky decomposition (lower triangular matrix L):"
)
(
L
)
(
"nVerify: L @ L.T = "
)
(
L
@
L.
T
)
The output is:
原矩阵 A:
tensor([[4., 2., 2.],
[2., 5., 3.],
[2., 3., 6.]], dtype=torch.float64)
Cholesky 分解 (下三角矩阵 L):
tensor([[2.0000, 0.0000, 0.0000],
[1.0000, 2.0000, 0.0000],
[1.0000, 1.0000, 2.0000]], dtype=torch.float64)
验证: L @ L.T =
tensor([[4., 2., 2.],
[2., 5., 3.],
[2., 3., 6.]], dtype=torch.float64)