335 Star 1.5K Fork 861

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
unique.md 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
TingWang 提交于 2023-09-18 10:08 . update link logo

比较与torch.unique的差异

查看源文件

torch.unique

torch.unique(
    *args,
    **kwargs
)

更多内容详见torch.unique

mindspore.ops.unique

mindspore.ops.unique(input)

更多内容详见mindspore.ops.unique

差异对比

PyTorch:对Tensor中元素进行去重。可通过设置参数 sorted 确定输出是否按升序排列;设置参数 return_inverse 确定是否输出输入的Tensor的各元素在输出Tensor中的位置索引;设置参数 return_counts 确定是否输出各唯一值在输入的Tensor中的数量;设置参数 dim 指定unique的维度。MindSpore不支持这些功能。

MindSpore:对Tensor中元素进行去重,以及返回输入Tensor的各元素在输出Tensor中的位置索引。

分类 子类 PyTorch MindSpore 差异
参数 参数1 input input 一致
参数2 sorted - sorted为True时,输出Tensor按照升序排列;sorted为False时,按照原有顺序排列
参数3 return_inverse - return_inverse为True时,返回输入Tensor各元素在输出Tensor中的索引位置
参数4 return_counts - return_counts为True时,返回输出Tensor各元素在输入Tensor中的数量
参数5 dim - 指定unique的维度

代码示例

# In MindSpore
import mindspore

x = mindspore.Tensor([1, 3, 2, 3], mindspore.float32)
output, idx = mindspore.ops.unique(x)
print(output)
# [1. 3. 2.]
print(idx)
# [0 1 2 1]

# In PyTorch
import torch

output, inverse_indices, counts = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long), sorted=True, return_inverse=True, return_counts=True)
print(output)
# tensor([1, 2, 3])
print(inverse_indices)
# tensor([0, 2, 1, 2])
print(counts)
# tensor([1, 1, 2])

# Example of using unique with dim
output, inverse_indices = torch.unique(torch.tensor([[3, 1], [1, 2]], dtype=torch.long), sorted=True, return_inverse=True, dim=0)
print(output)
# tensor([[1, 2],
#         [3, 1]])
print(inverse_indices)
# tensor([1, 0])
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
master

搜索帮助