在每个元素之前或之后零填充一维张量。 (张量流)

Zero-padding a 1-D tensor before or after each element. (TensorFlow)

我有一个包含 N 个元素的一维张量,它是通过将 2 个一维向量与 N/2[=20= 交错生成的] 元素。我如何使用 TensorFlow 做到这一点?

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

希望能有1行解决方案

谢谢!!

您可以将 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]