pytorch中是否有任何索引方法?
pytorch 203
原文标题 :Is there any indexing method in pytorch?
我最近在研究pytorch。但是这个问题太奇怪了..
x=np.arrage(24)
ft=torch.FloatTensor(x)
print(floatT.view([@1])[@2])
答案 = 张量([[13., 16.], [19., 22.]])
是否有满足答案的索引方法@1 和@2?
回复
我来回复-
lucidbrot 评论
如果您首先获取您关心的值,然后才使用
view
将其解释为矩阵,则更容易考虑:# setting up >>> import torch >>> import numpy as np >>> x=np.arange(24) + 3 # just to visualize the difference between indices and values >>> x array([ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]) # taking the values you want and viewing as matrix >>> ft = torch.FloatTensor(x) >>> ft[[13, 16, 19, 22]] tensor([16., 19., 22., 25.]) >>> ft[[13, 16, 19, 22]].view(2,2) tensor([[16., 19.], [22., 25.]])
2年前 -
Shai 评论
该回答已被采纳!
by
view
ingft
作为一个有 6 列的张量:ft.view(-1, 6) Out[]: tensor([[ 0., 1., 2., 3., 4., 5.], [ 6., 7., 8., 9., 10., 11.], [12., 13., 14., 15., 16., 17.], [18., 19., 20., 21., 22., 23.]])
您将元素 (
13
,19
) 和 (16
,22
) 放在彼此的顶部。现在您只需从正确的行/列中拾取它们:.view(-1, 6)[2:, (1, 4)] Out[]: tensor([[13., 16.], [19., 22.]])
2年前