Torch.lstsq returns wrong results

Hi, I was trying to run torch.lstsq to get the solution vector, but I am getting wrong results. My code and results are given below:

import torch
A = torch.tensor([[1.0,1.0,1.0],[1.0,1.0,1.0]])
B = torch.tensor([[5.0],[5.0]])
lambda_id = torch.lstsq(A,B).solution
print("A = ",A.shape)
print("B = ",B.shape)
print("lambda_id shape = ",lambda_id.shape)
print("lambda_id = ",lambda_id)

Output:-
A = torch.Size([2, 3])
B = torch.Size([2, 1])
lambda_id shape = torch.Size([2, 3])
lambda_id = tensor([[ 2.0000e-01, 2.0000e-01, 2.0000e-01],
[-5.2870e-08, -5.2870e-08, -5.2870e-08]])

Whereas when I run the same code with scipy.linalg.lstsq, I am getting correct results.

from scipy.linalg import lstsq
A = torch.tensor([[1.0,1.0,1.0],[1.0,1.0,1.0]])
B = torch.tensor([[5.0],[5.0]])
lambda_id = lstsq(A,B)
print("A = ",A.shape)
print("B = ",B.shape)
print("lambda_id shape = ",lambda_id[0].shape)
print("lambda_id = ",lambda_id[0])

Output:-
A = torch.Size([2, 3])
B = torch.Size([2, 1])
lambda_id shape = (3, 1)
lambda_id = [[1.6666663]
[1.6666663]
[1.6666663]]

Please help me to figure it out.

Hi Kiran!

The short story: Use torch.linalg.lstsq().

torch.lstsq() has been deprecated for some time now (and was
removed, I believe, in pytorch version 1.13).

Quoting from the 1.12 documentation:

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

As an aside, your tensor A has two identical rows so it is rank-deficient.
The old torch.lstsq() was known not to support such cases, so even
if you swap your arguments around, torch.lstsq (B, A).solution,
torch.lstsq() probably still won’t work.

Best.

K. Frank

1 Like

Thanks for such a detailed explanation!!