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

PyTorch torch.linalg.lstsq function

PyTorch torch.linalg.lstsq function


PyTorch torch reference manual PyTorch torch reference manual

torch.linalg.lstsq is a function in PyTorch's linear algebra module used to solve linear least-squares problems. It solves argmin_x ||AX - B||_F.

Function definition

torch.linalg.lstsq(A, B, rcond=None, driver=None)

Parameters:

Return Value:


Usage examples

Example

import

torch

Create the coefficient matrix and the right-hand-side vector

A

=

torch.

tensor

(

[

[

1.0

,

1.0

]

,

[

1.0

,

2.0

]

,

[

1.0

,

3.0

]

]

,

dtype

=

torch.

float64

)

B

=

torch.

tensor

(

[

1.0

,

2.0

,

3.0

]

,

dtype

=

torch.

float64

)

Least-squares solution

X

=

torch.

linalg

.

lstsq

(

A

,

B

)

.

solution

print

(

"Coefficient matrix A:"

)

print

(

A

)

print

(

"nRight-hand-side vector B:"

)

print

(

B

)

print

(

"nLeast-squares solution X:"

)

print

(

X

)

The output is:

系数矩阵 A:

tensor([[1., 1.],

        [1., 2.],

        [1., 3.]], dtype=torch.float64)

右侧向量 B:

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

最小二乘解 X:

tensor([0.0000, 1.0000], dtype=float64)


PyTorch torch reference manual PyTorch torch reference manual