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

PyTorch torch.cholesky_solve function

PyTorch torch.cholesky_solve function


PyTorch torch reference manual PyTorch torch reference manual

torch.cholesky_solve is a function in PyTorch used to solve systems of linear equations using Cholesky decomposition. It uses the Cholesky decomposition to solve AX = B efficiently.

Function definition

torch.cholesky_solve(B, L, upper=False, out=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create a symmetric positive-definite matrix A

A

=

torch.

tensor

(

[

[

4.0

,

2.0

,

2.0

]

,

                 

[

2.0

,

5.0

,

3.0

]

,

                 

[

2.0

,

3.0

,

6.0

]

]

,

dtype

=

torch.

float64

)

Create the right-hand-side vector B

B

=

torch.

tensor

(

[

3

,

4

,

5

]

,

dtype

=

torch.

float64

)

Cholesky decomposition

L

=

torch.

cholesky

(

A

)

Solve AX = B using Cholesky

X

=

torch.

cholesky_solve

(

B

,

L

)

print

(

"Matrix A:"

)

print

(

A

)

print

(

"nRight-hand-side vector B:"

)

print

(

B

)

print

(

"nSolution X:"

)

print

(

X

)

print

(

"nVerify: A @ X ="

)

print

(

A

@

X

)

The output is:

矩阵 A:

tensor([[4., 2., 2.],

        [2., 5., 3.],

        [2., 3., 6.]], dtype=torch.float64)

右侧向量 B:

tensor([3., 4., 5.], dtype=torch.float64)

解 X:

tensor([-2.5000,  5.0000, -2.0000], dtype=torch.float64)

验证: A @ X =

tensor([3., 4., 5.], dtype=torch.float64)


PyTorch torch reference manual PyTorch torch reference manual