代码拉取完成,页面将自动刷新
torch.Tensor.take(indices)
更多内容详见torch.Tensor.take。
mindspore.Tensor.take(indices, axis=None, mode='clip')
更多内容详见mindspore.Tensor.take。
MindSpore此API功能与PyTorch基本一致。
PyTorch:获取Tensor中的元素。不可指定维度,使用展开的输入数组。若索引超出范围,则抛出异常。
MindSpore:在指定维度上获取Tensor中的元素。可以指定维度,默认使用展开的输入数组。若索引超出范围:mode为'raise'时,则抛出异常;mode为'wrap'时,绕接;mode为'raise'时,裁剪到范围。
分类 | 子类 | PyTorch | MindSpore | 差异 |
---|---|---|---|---|
参数 | 参数 1 | indices | indices | 无 |
参数 2 | axis | 指定获取的索引,Pytorch不支持 | ||
参数 3 | mode | 若索引超出范围时的行为模式选择,Pytorch不支持 |
# PyTorch
import torch
input_x1 = torch.tensor([[4, 3, 5], [6, 7, 8]])
indices = torch.tensor([0, 2, 4])
output = input_x1.take(indices)
print(output)
# tensor([4, 5, 7])
# MindSpore
import mindspore as ms
input_x1 = ms.Tensor([[4, 3, 5], [6, 7, 8]])
indices = ms.Tensor([0, 2, 4])
output = input_x1.take(indices)
print(output)
# [4 5 7]
# PyTorch
import torch
input_x1 = torch.tensor([[4, 3, 5], [6, 7, 8]])
indices = torch.tensor([0, 2, 8])
output = input_x1.take(indices)
print(output)
# IndexError: out of range: tried to access index 8 on a tensor of 6 elements
# MindSpore
import mindspore as ms
input_x1 = ms.Tensor([[4, 3, 5], [6, 7, 8]])
indices = ms.Tensor([0, 2, 8])
output = input_x1.take(indices, mode='clip')
print(output)
# [4 5 8]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。