Python Pytorch中作为索引的二维数组

Python Pytorch中作为索引的二维数组,python,numpy,matrix,indexing,pytorch,Python,Numpy,Matrix,Indexing,Pytorch,我想使用一组规则“增长”矩阵 规则示例: 0->[[1,1,1],[0,0,0],[2,2,2]], 1->[[2,2,2],[2,2,2],[2,2,2]], 2->[[0,0,0],[0,0,0],[0,0,0]] 增长矩阵的示例: [[0]]->[[1,1,1],[0,0,0],[2,2,2]]-> [[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2], [1,1,1,1,1,1,1,1,1

我想使用一组规则“增长”矩阵

规则示例:

0->[[1,1,1],[0,0,0],[2,2,2]],
1->[[2,2,2],[2,2,2],[2,2,2]],
2->[[0,0,0],[0,0,0],[0,0,0]]
增长矩阵的示例:

[[0]]->[[1,1,1],[0,0,0],[2,2,2]]->
[[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2],[2,2,2,2,2,2,2,2,2],
[1,1,1,1,1,1,1,1,1],[0,0,0,0,0,0,0,0,0],[2,2,2,2,2,2,2,2,2],
[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]
这就是我一直在Pytorch中尝试的代码

rules = np.random.randint(256,size=(10,256,3,3,3))
rules_tensor = torch.randint(256,size=(10,
            256, 3, 3, 3),
            dtype=torch.uint8, device = torch.device('cuda'))

rules = rules[0]
rules_tensor = rules_tensor[0]

seed = np.array([[128]])
seed_tensor = seed_tensor = torch.cuda.ByteTensor([[128]])

decode = np.empty((3**3, 3**3, 3))
decode_tensor = torch.empty((3**3,
                3**3, 3), dtype=torch.uint8,
                device = torch.device('cuda'))

for i in range(3):
    grow = seed
    grow_tensor = seed_tensor
    for j in range(1,4):
        grow = rules[grow,:,:,i].reshape(3**j,-1)
        grow_tensor = rules_tensor[grow_tensor,:,:,i].reshape(3**j,-1)

    decode[..., i] = grow
    decode_tensor[..., i] = grow_tensor
在这一行中,我似乎不能像Numpy那样选择索引:

grow = rules[grow,:,:,i].reshape(3**j,-1)

是否有办法在PyTrk中执行下列操作?< /P> < P>你可以考虑使用,在重构结果之前将索引张量变平:

代码:

Seed:
tensor([ 0])
Growth #0:
tensor([[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]])
Growth #1:
tensor([[[[ 2,  2,  2], [ 2,  2,  2], [ 2,  2,  2]],
         [[ 2,  2,  2], [ 2,  2,  2], [ 2,  2,  2]],
         [[ 2,  2,  2], [ 2,  2,  2], [ 2,  2,  2]]],

        [[[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]],
         [[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]],
         [[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]]],

        [[[ 0,  0,  0], [ 0,  0,  0], [ 0,  0,  0]],
         [[ 0,  0,  0], [ 0,  0,  0], [ 0,  0,  0]],
         [[ 0,  0,  0], [ 0,  0,  0], [ 0,  0,  0]]]])
导入火炬
将numpy作为np导入
规则\u np=np.array([
[[1,1,1],[0,0,0],[2,2,2]],#对于值0
[[2,2,2],[2,2,2],[2,2,2]],#对于值1
[[0,0,0],[0,0,0],[0,0,0]]]#表示值2,等等。
rules=torch.from_numpy(rules_np).long()
规则\u形状=规则[0]。形状
种子=火炬。零(1)。长()
num_增长=2
打印(“种子:”)
打印(种子)
生长=种子
对于范围内的i(数量增长):
grow=(torch.index_选择(规则,0,grow.view(-1))
.视图(grow.shape+规则形状)
.挤压()
打印(“增长{}:”.format(i))
打印(增长)
日志:

Seed:
tensor([ 0])
Growth #0:
tensor([[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]])
Growth #1:
tensor([[[[ 2,  2,  2], [ 2,  2,  2], [ 2,  2,  2]],
         [[ 2,  2,  2], [ 2,  2,  2], [ 2,  2,  2]],
         [[ 2,  2,  2], [ 2,  2,  2], [ 2,  2,  2]]],

        [[[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]],
         [[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]],
         [[ 1,  1,  1], [ 0,  0,  0], [ 2,  2,  2]]],

        [[[ 0,  0,  0], [ 0,  0,  0], [ 0,  0,  0]],
         [[ 0,  0,  0], [ 0,  0,  0], [ 0,  0,  0]],
         [[ 0,  0,  0], [ 0,  0,  0], [ 0,  0,  0]]]])