pytorch 如何在Mac中打开exe文件

2izufjch  于 5个月前  发布在  Mac
关注(0)|答案(2)|浏览(95)

我试图在我创建的Tensor中反转行的顺序。我已经尝试过tensorflow和pytorch。我发现的唯一方法是torch.flip()方法。这不起作用,因为它不仅反转行的顺序,还反转每行中的所有元素。我希望元素保持不变。有没有数组操作来索引整数?例如:

tensor_a = [1, 2, 3]
       [4, 5, 6]
       [7, 8, 9]

I want it to be returned as:
       [7, 8, 9]
       [4, 5, 6]
       [1, 2, 3]

 however, torch.flip(tensor_a) = 
       [9, 8, 7]
       [6, 5, 4]
       [3, 2, 1]

字符串
有人有什么建议吗?

mu0hgdu0

mu0hgdu01#

根据documentationtorch.flip有一个参数dims,它控制要翻转的轴。在这种情况下,torch.flip(tensor_a, dims=(0,))将返回预期的结果。此外,torch.flip(tensor_a)将反转所有Tensor,而torch.flip(tensor_a, dims=(1,))将反转每行,就像[1, 2, 3] --> [3, 2, 1]一样。

b4lqfgs4

b4lqfgs42#

我不确定我的解决方案的性能,但你可以做一些事情,如下所示:

import torch
y = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Uncomment the next two lines so u can see it works on GPU as well
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# y.to(device)

y = y[[2, 1, 0], :]
# y = y[::-1, :] # this works in numpy but not in pytorch :(
print(y)

字符串
你可以查看有关切片和高级索引的numpy文档中类似的例子。希望这对你有帮助。

相关问题