在每个元素之前或之后对1-D张量进行零填充。 (TensorFlow)

时间:2017-09-29 09:19:09

标签: python tensorflow tensor

我有一个带有 N 元素的1-D张量,它是通过将2个1-D矢量与 N / 2 元素交织而产生的。我怎么能用TensorFlow做到这一点?

例如,我想从[0,2,4,6]和[1,3,5,7]生成[0,1,2,3,4,5,6,7]。

我希望有一个单行解决方案。

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以将ab列为列,然后将其重新整理为1d:

tf.reshape(tf.stack([a, b], axis=-1), [-1])
a = tf.constant([0, 2, 4, 6])
b = tf.constant([1, 3, 5, 7])

sess = tf.InteractiveSession()
interlace = tf.reshape(tf.stack([a, b], axis=-1), [-1])

print(sess.run(interlace))
# [0 1 2 3 4 5 6 7]