如何根据索引张量对一个热张量进行排序
给定下面的张量:
tensor = torch.Tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
下面是包含索引的张量:
indices = torch.tensor([2, 6, 7, 5, 4, 0, 3, 1])
如何使用索引
内部的值对张量
进行排序?
尝试使用 sorted
会出现错误:
类型错误:“张量”对象不可调用。
而 numpy.sort 给出:
ValueError:当数组没有字段时无法指定顺序。`
Given the below tensor:
tensor = torch.Tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1.],
[1., 0., 0., 0., 0.],
[1., 0., 0., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])
and below is the tensor containing the indices:
indices = torch.tensor([2, 6, 7, 5, 4, 0, 3, 1])
How can I sort tensor
using the values inside of indices
?
Trying with sorted
gives the error:
TypeError: 'Tensor' object is not callable`.
While numpy.sort
gives:
ValueError: Cannot specify order when the array has no fields.`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您可以像这样使用索引:
You can use the indices like this: