pytorch中有索引方法吗?

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.]])

能否有满足Answer的索引方法@1和@2?

如果你抓取你关心的值,然后才使用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.]])

通过 viewing ft 作为具有 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.]])

您将元素 (1319) 和 (1622) 放在彼此之上。 现在你只需要从右边拿起来rows/columns:

.view(-1, 6)[2:, (1, 4)]
Out[]:
tensor([[13., 16.],
        [19., 22.]])