代码拉取完成,页面将自动刷新
torch.unique(
*args,
**kwargs
)
更多内容详见torch.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])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。