【问题标题】:reshaping tensor and keep the original relative position of the elements unchanged in numpy (or tensorflow) tensor重塑张量并在numpy(或张量流)张量中保持元素的原始相对位置不变
【发布时间】:2021-03-05 15:59:39
【问题描述】:

我有一个这样的张量:

A=array([[[[ 1.4033688 , -0.95642966,  1.0958625 , -0.64104766],
         [-1.2625898 , -0.59444463,  0.72382635, -0.5195144 ],
         [ 0.15248759,  2.4054656 ,  1.0738292 ,  1.0531213 ],
         [ 1.2878437 , -1.1945801 , -1.0729346 , -1.6739473 ]],

        [[ 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.        ,  0.        ],
         [ 0.        , -0.        , -0.        , -0.        ]],

        [[ 1.4033688 , -0.95642966,  1.0958625 , -0.64104766],
         [-1.2625898 , -0.59444463,  0.72382635, -0.5195144 ],
         [ 0.15248759,  2.4054656 ,  1.0738292 ,  1.0531213 ],
         [ 1.2878437 , -1.1945801 , -1.0729346 , -1.6739473 ]]]],
      dtype=float32)

特别是,

A[0][0]=[[ 1.4033688 , -0.95642966,  1.0958625 , -0.64104766],
             [-1.2625898 , -0.59444463,  0.72382635, -0.5195144 ],
             [ 0.15248759,  2.4054656 ,  1.0738292 ,  1.0531213 ],
             [ 1.2878437 , -1.1945801 , -1.0729346 , -1.6739473 ]]
A[1][0]=[[[ 0.        , -0.        ,  0.        , -0.        ],
             [-0.        , -0.        ,  0.        , -0.        ],
             [ 0.        ,  0.        ,  0.        ,  0.        ],
             [ 0.        , -0.        , -0.        , -0.        ]]
A[0][1]=[[[ 0.        , -0.        ,  0.        , -0.        ],
             [-0.        , -0.        ,  0.        , -0.        ],
             [ 0.        ,  0.        ,  0.        ,  0.        ],
             [ 0.        , -0.        , -0.        , -0.        ]]
A[1][1]=[[ 1.4033688 , -0.95642966,  1.0958625 , -0.64104766],
             [-1.2625898 , -0.59444463,  0.72382635, -0.5195144 ],
             [ 0.15248759,  2.4054656 ,  1.0738292 ,  1.0531213 ],
             [ 1.2878437 , -1.1945801 , -1.0729346 , -1.6739473 ]]

我想将 A 重塑为 (8 , 8) 数组,这样我就可以保持元素的位置,就好像数组刚刚删除了中间括号一样。也就是说,将A重新整形为新数组后,我们称这个新的整形数组A_reshaped,那么我希望A_reshaped等于下面的A

A_reshaped[:4,:4]=A[0][0]
A_reshaped[4:8,0:4]=A[1][0]
A_reshaped[:4,4:8]=A[0][1]   
A_reshaped[4:8,4:8]=A[1][1]

简单的命令:

np.reshape(A,(8,8))[:4,:4]

不起作用,它会产生以下结果:

array([[ 1.4033688 , -0.95642966,  1.0958625 , -0.64104766],
       [ 0.15248759,  2.4054656 ,  1.0738292 ,  1.0531213 ],
       [ 0.        , -0.        ,  0.        , -0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ]],
      dtype=float32)

感谢任何提示。

【问题讨论】:

  • 原来的形状是什么? (2,2,4,4)?您可以将其重塑为 (4,4,4)。要获得 (8,8) 你需要先转置所以形状是 (2,4,2,4)

标签: numpy tensorflow reshape tensor


【解决方案1】:

使用gather_nd():

import tensorflow as tf
input1 = tf.constant([[0, 1], [2, 3]])
input2 = tf.constant([[4, 5], [6, 7]])
input3 = tf.constant([[8, 9], [10, 11]])
input4 = tf.constant([[12, 13], [14, 15]])

input = tf.stack([input1, input2, input3, input4])

# prepare indices for gather_nd
my_shape = tf.shape(input)[1:]
my_range = my_shape[0]*my_shape[1]
m = tf.range(0, my_range)
def get_inds(t, last_dim):
  return tf.convert_to_tensor([t // last_dim, t % last_dim])
inds = tf.map_fn(fn=lambda t: get_inds(t, my_shape[-1]), elems=m)
sh = tf.concat([my_shape, [2]], -1)
inds = tf.reshape(inds, sh)
mat0 = tf.zeros(my_shape, dtype=tf.int32)
mat0 = mat0[..., tf.newaxis]
mat1 = mat0 + 1
mat2 = mat0 + 2
mat3 = mat0 + 3
mat0 = tf.concat([mat0, inds], -1)
mat1 = tf.concat([mat1, inds], -1)
mat2 = tf.concat([mat2, inds], -1)
mat3 = tf.concat([mat3, inds], -1)
mat0 = tf.concat([mat0, mat1], 1)
mat1 = tf.concat([mat2, mat3], 1)
inds = tf.concat([mat0, mat1], 0)

res = tf.gather_nd(input, inds)

准备索引矩阵很复杂。我没有设法简化它

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2020-08-25
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多