【问题标题】:Zero-padding a 1-D tensor before or after each element. (TensorFlow)在每个元素之前或之后对一维张量进行零填充。 (TensorFlow)
【发布时间】: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


    【解决方案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]
    

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 2016-03-12
      • 2020-01-20
      • 2018-02-28
      • 2019-11-03
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多