Munichong
Munichong

Reputation: 4031

tensorflow merge and zip two tensors

I have two tensors as below:

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]])

How should I merge and transform x1 and x2, so that I can have a tensor like below:

[[[1.0, 7.0]
  [2.0, 8.0]
  [3.0, 9.0]]

 [[4.0, 10.0]
  [5.0, 11.0]
  [6.0, 12.0]]
]

Upvotes: 3

Views: 4550

Answers (1)

akuiper
akuiper

Reputation: 214927

Use tf.stack on the last axis:

tf.InteractiveSession()

tf.stack([x1, x2], axis=-1).eval()

#array([[[  1.,   7.],
#        [  2.,   8.],
#        [  3.,   9.]],

#       [[  4.,  10.],
#        [  5.,  11.],
#        [  6.,  12.]]], dtype=float32)

Upvotes: 10

Related Questions