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.