题目:给定H * W的一维数组,指定index,求reshape成二维矩阵后,对应的二维矩阵index(x, y)。
>>> import torch
>>> x = torch.tensor([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
>>> x
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
>>> x.reshape([8,2])
tensor([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12],
[13, 14],
[15, 16]])
求的方法。
h = index // W
w = index % W
例如:index = 11时,h = 11 // 8 =1 , w = 11%8=3,故索引为(1,3)
>>> x.reshape(2,8)[1,3]
tensor(12)