【发布时间】:2017-09-29 09:19:08
【问题描述】:
我有一个带有 N 元素的一维张量,它是通过将 2 个带有 N/2 元素的一维向量交错生成的。如何使用 TensorFlow 做到这一点?
例如,我想从 [0, 2, 4, 6] 和 [1, 3, 5, 7] 生成 [0, 1, 2, 3, 4, 5, 6, 7]。
我希望有一个单行解决方案。
谢谢!!
【问题讨论】:
标签: python tensorflow tensor
我有一个带有 N 元素的一维张量,它是通过将 2 个带有 N/2 元素的一维向量交错生成的。如何使用 TensorFlow 做到这一点?
例如,我想从 [0, 2, 4, 6] 和 [1, 3, 5, 7] 生成 [0, 1, 2, 3, 4, 5, 6, 7]。
我希望有一个单行解决方案。
谢谢!!
【问题讨论】:
标签: python tensorflow tensor
您可以将a 和b 堆叠为列,然后将其重新整形为 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]
【讨论】: