gpt4 book ai didi

numpy - 如何在 PyTorch 中选择二维索引?

转载 作者:行者123 更新时间:2023-12-01 11:17:25 26 4
gpt4 key购买 nike

给定 a = torch.randn(3, 2, 4, 5) ,我如何选择像 (2, :, 0, :), (1, :, 1, :), (2, :, 2, :), (0, :, 3, :) 这样的子张量(大小为 (2, 4, 5)(4, 2, 5) 的结果张量?

虽然 a[2, :, 0, :]

 0.5580 -0.0337  1.0048 -0.5044  0.6784
-1.6117 1.0084 1.1886 0.1278 0.3739
[torch.FloatTensor of size 2x5]

然而, a[[2, 1, 2, 0], :, [0, 1, 2, 3], :]

TypeError: Performing basic indexing on a tensor and encountered an error indexing dim 0 with an object of type list. The only supported types are integers, slices, numpy scalars, or if indexing with a torch.LongTensor or torch.ByteTensor only a single Tensor may be passed.



虽然 numpy返回 (4, 2, 5)张量成功。

最佳答案

对你起作用吗?

import torch

a = torch.randn(3, 2, 4, 5)
print(a.size())

b = [a[2, :, 0, :], a[1, :, 1, :], a[2, :, 2, :], a[0, :, 3, :]]
b = torch.stack(b, 0)

print(b.size()) # torch.Size([4, 2, 5])

关于numpy - 如何在 PyTorch 中选择二维索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47374172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com