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

PyTorch torch.lu_solve function

PyTorch torch.lu_solve function


PyTorch torch reference manual PyTorch torch reference manual

torch.lu_solve is a function in PyTorch used to solve systems of linear equations using LU decomposition. It uses an already computed LU decomposition to solve AX = B efficiently.

Function definition

torch.lu_solve(B, LU, pivots, out=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create a matrix and a right-hand-side vector

A

=

torch.

tensor

(

[

[

1.0

,

2.0

,

3.0

]

,

                 

[

4.0

,

5.0

,

6.0

]

,

                 

[

7.0

,

8.0

,

9.0

]

]

,

dtype

=

torch.

float64

)

B

=

torch.

tensor

(

[

14.0

,

32.0

,

50.0

]

,

dtype

=

torch.

float64

)

LU decomposition

LU

,

pivots

=

torch.

lu

(

A

)

Solve using LU decomposition

X

=

torch.

lu_solve

(

B

,

LU

,

pivots

)

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([[1., 2., 3.],

        [4., 5., 6.],

        [7., 8., 9.]], dtype=torch.float64)

右侧向量 B:

tensor([14., 32., 50.], dtype=torch.float64)

解 X:

tensor([1., 2., 3.], dtype=torch.float64)

验证: A @ X =

tensor([14., 32., 50.], dtype=torch.float64)


PyTorch torch reference manual PyTorch torch reference manual