335 Star 1.5K Fork 861

MindSpore / docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Unique.md 1.89 KB
一键复制 编辑 原始数据 按行查看 历史
俞涵 提交于 2023-01-29 16:21 . add mindquantum and modify url

比较与torch.unique的功能差异

torch.unique

torch.unique(
    input,
    sorted=True,
    return_inverse=False,
    return_counts=False,
    dim=None
)

更多内容详见torch.unique

mindspore.ops.Unique

class mindspore.ops.Unique(*args, **kwargs)(x)

更多内容详见mindspore.ops.Unique

使用方式

PyTorch:可通过设置参数来确定输出是否排序,是否输出输入的tensor的各元素在输出tensor中的位置索引,是否输出各唯一值在输入的tensor中的数量。

MindSpore:升序输出所有的唯一值,以及输入的tensor的各元素在输出tensor中的位置索引。

代码示例

import mindspore as ms
import mindspore.ops as ops
import torch
import numpy as np

# In MindSpore, the tensor containing unique elements in ascending order.
# As well as another tensor containing the corresponding indices will be directly returned.
x = ms.Tensor(np.array([1, 2, 5, 2]), ms.int32)
unique = ops.Unique()
output, indices = unique(x)
print(output)
print(indices)
# Out:
# [1 2 5]
# [0 1 2 1]

# In torch, parameters can be set to determine whether to output tensor containing unique elements in ascending order.
# As well as whether to output tensor containing corresponding indices.
x = torch.tensor([1, 2, 5, 2])
output, indices = torch.unique(x, sorted=True, return_inverse=True)
print(output)
print(indices)
# Out:
# tensor([1, 2, 5])
# tensor([0, 1, 2, 1])
1
https://gitee.com/mindspore/docs.git
git@gitee.com:mindspore/docs.git
mindspore
docs
docs
r2.0.0-alpha

搜索帮助