Python TensorFlow-3D张量,从2D张量和步长1中收集每N个张量
假设我在Python TensorFlow-3D张量,从2D张量和步长1中收集每N个张量,python,tensorflow,slice,reshape,Python,Tensorflow,Slice,Reshape,假设我在[M,1]中有一个2D张量T T = tf.expand_dims([A1, B1, C1, A2, B2, C2], 1) 我想把它改造成这样: T_reshp = [[[A1], [A2]] [[B1], [B2]] [[C1], [C2]]] 我事先知道M和N(每组张量的数量)。此外,在我尝试使用的tf.reshape T_reshp = tf.reshape(T, [P, N, 1]
[M,1]
中有一个2D张量
T
T = tf.expand_dims([A1,
B1,
C1,
A2,
B2,
C2], 1)
我想把它改造成这样:
T_reshp = [[[A1], [A2]]
[[B1], [B2]]
[[C1], [C2]]]
我事先知道M
和N
(每组张量的数量)。此外,在我尝试使用的tf.reshape
T_reshp = tf.reshape(T, [P, N, 1])
然而,我最终得出以下结论:
T_reshp = [[[A1], [B1]]
[[C1], [A2]]
[[B2], [C2]]]
我可以使用一些切片或整形操作来进行此操作吗?您可以先将其整形为尺寸
[N,p,1]
,然后转置第一和第二轴:
tf.transpose(tf.reshape(T, [N, P, 1]), [1,0,2])
# ^^^^ switch the two dimensions here and then transpose
例如:
您可以先将其重塑为尺寸[N,p,1]
,然后转置第一和第二轴:
tf.transpose(tf.reshape(T, [N, P, 1]), [1,0,2])
# ^^^^ switch the two dimensions here and then transpose
例如:
优雅的解决方案!优雅的解决方案!