lstsq

paddle.linalg. lstsq ( x, y, rcond=None, driver=None, name=None ) [source]

Computes a solution to the least squares problem of a system of linear equations.

Parameters
  • x (Tensor) – A tensor with shape (*, M, N) , the data type of the input Tensor x should be one of float32, float64.

  • y (Tensor) – A tensor with shape (*, M, K) , the data type of the input Tensor y should be one of float32, float64.

  • rcond (float, optional) – The default value is None. A float pointing number used to determine the effective rank of x. If rcond is None, it will be set to max(M, N) times the machine precision of x_dtype.

  • driver (str, optional) – The default value is None. The name of LAPACK method to be used. For CPU inputs the valid values are ‘gels’, ‘gelsy’, ‘gelsd, ‘gelss’. For CUDA input, the only valid driver is ‘gels’. If driver is None, ‘gelsy’ is used for CPU inputs and ‘gels’ for CUDA inputs.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

A tuple of 4 Tensors which is (solution, residuals, rank, singular_values). solution is a tensor with shape (*, N, K), meaning the least squares solution. residuals is a tensor with shape (*, K), meaning the squared residuals of the solutions, which is computed when M > N and every matrix in x is full-rank, otherwise return an empty tensor. rank is a tensor with shape (*), meaning the ranks of the matrices in x, which is computed when driver in (‘gelsy’, ‘gelsd’, ‘gelss’), otherwise return an empty tensor. singular_values is a tensor with shape (*, min(M, N)), meaning singular values of the matrices in x, which is computed when driver in (‘gelsd’, ‘gelss’), otherwise return an empty tensor.

Return type

Tuple

Examples

>>> import paddle

>>> x = paddle.to_tensor([[1, 3], [3, 2], [5, 6.]])
>>> y = paddle.to_tensor([[3, 4, 6], [5, 3, 4], [1, 2, 1.]])
>>> results = paddle.linalg.lstsq(x, y, driver="gelsd")
>>> print(results[0])
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.78350395, -0.22165027, -0.62371236],
 [-0.11340097,  0.78866047,  1.14948535]])
>>> print(results[1])
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[19.81443405, 10.43814468, 30.56185532])
>>> print(results[2])
Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
2)
>>> print(results[3])
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[9.03455734, 1.54167950])

>>> x = paddle.to_tensor([[10, 2, 3], [3, 10, 5], [5, 6, 12.]])
>>> y = paddle.to_tensor([[4, 2, 9], [2, 0, 3], [2, 5, 3.]])
>>> results = paddle.linalg.lstsq(x, y, driver="gels")
>>> print(results[0])
Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[ 0.39386186,  0.10230169,  0.93606132],
 [ 0.10741688, -0.29028130,  0.11892584],
 [-0.05115093,  0.51918161, -0.19948851]])
>>> print(results[1])
Tensor(shape=[0], dtype=float32, place=Place(cpu), stop_gradient=True,
[])