tensorflow 合并并压缩两个张量
tensorflow merge and zip two tensors
我有两个张量如下:
x1 = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
x2 = tf.constant([[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]])
我应该如何合并和转换 x1 和 x2,以便我可以得到如下张量:
[[[1.0, 7.0]
[2.0, 8.0]
[3.0, 9.0]]
[[4.0, 10.0]
[5.0, 11.0]
[6.0, 12.0]]
]
在最后一个轴上使用tf.stack
:
tf.InteractiveSession()
tf.stack([x1, x2], axis=-1).eval()
#array([[[ 1., 7.],
# [ 2., 8.],
# [ 3., 9.]],
# [[ 4., 10.],
# [ 5., 11.],
# [ 6., 12.]]], dtype=float32)
我有两个张量如下:
x1 = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
x2 = tf.constant([[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]])
我应该如何合并和转换 x1 和 x2,以便我可以得到如下张量:
[[[1.0, 7.0]
[2.0, 8.0]
[3.0, 9.0]]
[[4.0, 10.0]
[5.0, 11.0]
[6.0, 12.0]]
]
在最后一个轴上使用tf.stack
:
tf.InteractiveSession()
tf.stack([x1, x2], axis=-1).eval()
#array([[[ 1., 7.],
# [ 2., 8.],
# [ 3., 9.]],
# [[ 4., 10.],
# [ 5., 11.],
# [ 6., 12.]]], dtype=float32)