Оба Torch.linalg.lstsq и Np.linalg.lstsq используют SVD, и
(.env)boris@boris-All-Series:~/TORCHLSTSQ$ cat torchLstsq.py
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 = torch.linalg.lstsq(b, a).solution
x1, res, r1, s = np.linalg.lstsq(b1, a1,rcond = -1)
print(f'torch_x: {x}')
print(f'torch_r1: {r1}\n')
print(f'np_x: {x1}')
print(f'np_res: {res}')
print(f'np_r1(rank): {r1}')
print(f'np_s: {s}')
(.env) boris@boris-All-Series:~/TORCHLSTSQ$ python3 torchLstsq.py
torch_x: tensor([[-0.1145, -0.1047, -0.2863],
[ 0.3591, 0.3372, 0.5407]])
torch_r1: 2
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]
References
https://pytorch.org/docs/stable/generated/torch.linalg.lstsq.html
No comments:
Post a Comment