Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.cholesky function

PyTorch torch.cholesky function


PyTorch torch reference manual 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:

Return Value:


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

)

print

(

"Original matrix A:"

)

print

(

A

)

print

(

"nCholesky decomposition (lower triangular matrix L):"

)

print

(

L

)

print

(

"nVerify: L @ L.T = "

)

print

(

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

)

print

(

"Upper triangular matrix U:"

)

print

(

U

)


PyTorch torch reference manual PyTorch torch reference manual