简体   繁体   中英

Is there any indexing method in pytorch?

I'm studying about pytorch recently. But this problem is so weird..

x=np.arrage(24)
ft=torch.FloatTensor(x)
print(floatT.view([@1])[@2])

Answer = tensor([[13., 16.], [19., 22.]])

Can there be indexing methods @1 and @2 that satisfy the Answer?

It is easier to think about if you first grab the values you care about and only then use view to interpret it as a matrix:

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

by view ing ft as a tensor with 6 columns:

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

you place the elements ( 13 , 19 ), and ( 16 , 22 ) on top of each other. Now you only need to pick them up from the right rows/columns:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM