Shortcuts

torch.lstsq

torch.lstsq(input, A, *, out=None)

Computes the solution to the least squares and least norm problems for a full rank matrix AA of size (m×n)(m \times n) and a matrix BB of size (m×k)(m \times k).

If mnm \geq n, lstsq() solves the least-squares problem:

minXAXB2.\begin{array}{ll} \min_X & \|AX-B\|_2. \end{array}

If m<nm < n, lstsq() solves the least-norm problem:

minXX2subject toAX=B.\begin{array}{llll} \min_X & \|X\|_2 & \text{subject to} & AX = B. \end{array}

Returned tensor XX has shape (max(m,n)×k)(\max(m, n) \times k). The first nn rows of XX contains the solution. If mnm \geq n, the residual sum of squares for the solution in each column is given by the sum of squares of elements in the remaining mnm - n rows of that column.

Warning

torch.lstsq() is deprecated in favor of torch.linalg.lstsq() and will be removed in a future PyTorch release. torch.linalg.lstsq() has reversed arguments and does not return the QR decomposition in the returned tuple, (it returns other information about the problem). The returned solution in torch.lstsq() stores the residuals of the solution in the last m - n columns in the case m > n. In torch.linalg.lstsq(), the residuals are in the field ‘residuals’ of the returned named tuple.

Unpacking the solution as X = torch.lstsq(B, A).solution[:A.size(1)] should be replaced with

X = torch.linalg.lstsq(A, B).solution

Note

The case when m<nm < n is not supported on the GPU.

Parameters
  • input (Tensor) – the matrix BB

  • A (Tensor) – the mm by nn matrix AA

Keyword Arguments

out (tuple, optional) – the optional destination tensor

Returns

A namedtuple (solution, QR) containing:

  • solution (Tensor): the least squares solution

  • QR (Tensor): the details of the QR factorization

Return type

(Tensor, Tensor)

Note

The returned matrices will always be transposed, irrespective of the strides of the input matrices. That is, they will have stride (1, m) instead of (m, 1).

Example:

>>> A = torch.tensor([[1., 1, 1],
...                   [2, 3, 4],
...                   [3, 5, 2],
...                   [4, 2, 5],
...                   [5, 4, 3]])
>>> B = torch.tensor([[-10., -3],
...                   [ 12, 14],
...                   [ 14, 12],
...                   [ 16, 16],
...                   [ 18, 16]])
>>> X, _ = torch.lstsq(B, A)
>>> X
tensor([[  2.0000,   1.0000],
        [  1.0000,   1.0000],
        [  1.0000,   2.0000],
        [ 10.9635,   4.8501],
        [  8.9332,   5.2418]])

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources