[英]Using an ellipsis for the middle dimensions of a PyTorch tensor
Suppose I have a torch.Tensor t
of shape (8, 3, 32, 32)
.假设我有一个torch.Tensor t
的形状(8, 3, 32, 32)
。 I want to index along the first and last 2 dimensions only.我只想索引第一个和最后两个维度。
In my usecase, t
is a batch of 8 images, of which I want to modify a patch.在我的用例中, t
是一批 8 张图像,我想修改其中的一个补丁。 Suppose the patch is given by indices idx_last = torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]])
.假设补丁由索引idx_last = torch.tensor([[0, 0], [0, 1], [1, 0], [1, 1]])
给出。 I also have idx1 = torch.arange(4)
: I want the patch for the first 4 images.我也有idx1 = torch.arange(4)
:我想要前 4 张图像的补丁。
The following does not work:以下不起作用:
t[idx1, ..., idx_last]
Is there any way to do this?有没有办法做到这一点?
I found one workaround, although it may not be the most efficient.我找到了一种解决方法,尽管它可能不是最有效的。 In the case where idx1
is 1-dimensional (datapoint selection), and idx_last
is multidimensional, the following gets the wanted result:在idx1
是一维(数据点选择)并且idx_last
是多维的情况下,以下得到想要的结果:
t[(idx1, ...) + tuple(idx_last.T)]
Better solutions are definitely welcome.更好的解决方案绝对受欢迎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.