在numpy(或tensorflow)张量中重塑张量并保持元素的原始相对位置不变
我有一个像这样的张量:在numpy(或tensorflow)张量中重塑张量并保持元素的原始相对位置不变,numpy,tensorflow,reshape,tensor,Numpy,Tensorflow,Reshape,Tensor,我有一个像这样的张量: 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
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 ]]
我想把一个数组重塑成一个(8,8)数组,这样我就可以保持元素的位置,就好像数组刚刚去掉了中间的括号一样。换句话说,在将A重塑为新数组之后,让我们将这个新的重塑数组称为A_重塑的
,然后我希望A_重塑的
与下面的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)
任何提示都将不胜感激。使用gather\u nd()
:
编制指数矩阵是一项复杂的工作。我没有设法简化它原来的形状是什么?(2,2,4,4)? 可以将其重塑为(4,4,4)。要得到一个(8,8),你需要先转置,使形状为(2,4,2,4)
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)