tensorflow合并并压缩两个张量

问题描述 投票:3回答:1

我有两个张量如下:

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]]
]
python matrix tensorflow tensor
1个回答
6
投票

在最后一个轴上使用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)
© www.soinside.com 2019 - 2024. All rights reserved.