Differences with torch.svd
The following mapping relationships can be found in this file.
PyTorch APIs |
MindSpore APIs |
---|---|
torch.svd |
mindspore.ops.svd |
torch.Tensor.svd |
mindspore.Tensor.svd |
torch.svd
torch.svd(input, some=True, compute_uv=True, *, out=None)
For more information, see torch.svd.
mindspore.ops.svd
mindspore.ops.svd(input, full_matrices=False, compute_uv=True)
For more information, see mindspore.ops.svd.
Differences
API function of MindSpore is not consistent with that of PyTorch.
PyTorch:
If
some
is True, the method returns the reduced singular value decomposition.There are always three output values, and the order of the output values is u, s, v.
If
compute_uv
is False, the returned u and v will be zero-filled matrices.
MindSpore:
If
full_matrices
is False, the method returns the reduced singular value decomposition.If
compute_uv
is False, there is only one output value s.If
compute_uv
is True, there are three output values in the order s, u, v.
torch.svd()
has been deprecated in PyTorch 1.8.0 and later, and alternative apitorch.linalg.svd()
is recommended, which has the same parameterfull_matrices
asmindspore.ops.svd
.
Categories |
Subcategories |
PyTorch |
MindSpore |
Differences |
---|---|---|---|---|
Parameters |
Parameter 1 |
input |
input |
Consistent |
Parameter 2 |
some |
full_matrices |
To return the reduced singular value decomposition, MindSpore should set |
|
Parameter 3 |
compute_uv |
compute_uv |
If |
|
Parameter 4 |
out |
- |
For details, see General Difference Parameter Table |
Code Example 1
When
compute_uv
is False, PyTorch has three output values.
# PyTorch
import torch
input = torch.tensor([[1, 2], [-4, -5], [2, 1]], dtype=torch.float32)
u, s, v = torch.svd(input, some=False, compute_uv=False)
print(s)
# tensor([7.0653, 1.0401])
print(u)
# tensor([[0., 0., 0.],
# [0., 0., 0.],
# [0., 0., 0.]])
print(v)
# tensor([[0., 0.],
# [0., 0.]])
# MindSpore doesn't support this feature currently.
Code Example 2
When
compute_uv
is True, the order of output values is inconsistent. The output values of singular value decomposition are not unique.
# PyTorch
import torch
input = torch.tensor([[1, 2], [-4, -5], [2, 1]], dtype=torch.float32)
u, s, v = torch.svd(input, some=False, compute_uv=True)
print(s)
# tensor([7.0653, 1.0401])
print(u)
# tensor([[-0.3082, -0.4882, 0.8165],
# [ 0.9061, 0.1107, 0.4082],
# [-0.2897, 0.8657, 0.4082]])
print(v)
# tensor([[-0.6386, 0.7695],
# [-0.7695, -0.6386]])
# MindSpore
import mindspore as ms
input = ms.Tensor([[1, 2], [-4, -5], [2, 1]], ms.float32)
s, u, v = ms.ops.svd(input, full_matrices=True, compute_uv=True)
print(s)
# [7.0652843 1.040081 ]
print(u)
# [[ 0.30821905 -0.48819482 0.81649697]
# [-0.90613353 0.11070572 0.40824813]
# [ 0.2896955 0.8656849 0.4082479 ]]
print(v)
# [[ 0.63863593 0.769509 ]
# [ 0.769509 -0.63863593]]