我最近在学习pytorch。但是这个问题太奇怪了。。
x=np.arrage(24)
ft=torch.FloatTensor(x)
print(floatT.view([@1])[@2])
答案=张量([[13.,16.],[19.,22.]](
是否存在满足答案的索引方法@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.]])
通过view
将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.]])
将元素(13
、19
(和(16
、22
(放置在彼此之上。现在您只需要从右侧的行/列中提取它们:
.view(-1, 6)[2:, (1, 4)]
Out[]:
tensor([[13., 16.],
[19., 22.]])