PyTorch torch.cholesky function
PyTorch torch reference manual
torch.cholesky is a function in PyTorch used to compute the Cholesky decomposition of a symmetric positive-definite matrix. Cholesky decomposition factorizes a positive-definite matrix A into A = L * L^T, where L is a lower triangular matrix.
Function definition
torch.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 lower (or upper) 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
]
]
)
Cholesky decomposition
L
=
torch.
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.]])
Cholesky 分解 (下三角矩阵 L):
tensor([[2.0000, 0.0000, 0.0000],
[1.0000, 2.0000, 0.0000],
[1.0000, 1.0000, 2.0000]])
验证: L @ L.T =
tensor([[4., 2., 2.],
[2., 5., 3.],
[2., 3., 6.]])
Example - Return the upper triangular matrix
import
torch
A
=
torch.
tensor
(
[
[
4.0
,
2.0
,
2.0
]
,
[
2.0
,
5.0
,
3.0
]
,
[
2.0
,
3.0
,
6.0
]
]
)
Return the upper triangular matrix
U
=
torch.
cholesky
(
A
,
upper
=
True
)
(
"Upper triangular matrix U:"
)
(
U
)