为什么torch.lstsq的输出与np.linalg.lstsq完全不同?

问题描述

Pytorch提供了lstsq函数,但是返回的结果与numpy的版本完全不同。这是一个示例输入及其两个结果:

import numpy as np
import torch 

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]])

a1 = a.clone().numpy()
b1 = b.clone().numpy()

x,r = torch.lstsq(b,a)

x1,res,r1,s = np.linalg.lstsq(b1,a1)

print(f'torch_x: {x}')
print(f'torch_r: {r}\n')

print(f'np_x: {x1}')
print(f'np_res: {res}')
print(f'np_r1(rank): {r1}')
print(f'np_s: {s}')

输出:

torch_x: tensor([[ 2.0000,1.0000],[ 1.0000,2.0000],[10.9635,4.8501],[ 8.9332,5.2418]])
torch_r: tensor([[-7.4162,-6.7420,-6.7420],[ 0.2376,-3.0896,0.1471],[ 0.3565,0.5272,3.0861],[ 0.4753,-0.3952,-0.4312],[ 0.5941,-0.1411,0.2681]])

np_x: [[-0.11452514 -0.10474861 -0.28631285]
 [ 0.35913807  0.33719075  0.54070234]]
np_res: [ 5.4269753 10.197526   1.4185953]
np_r1(rank): 2
np_s: [43.057705  5.199417]

我在这里想念什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)