【问题标题】:Tensorflow: Repeat(tile) elements of a TensorTensorflow:张量的重复(平铺)元素
【发布时间】:2017-05-14 15:07:29
【问题描述】:

我有一个输入张量如下:

a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

和“多重”张量:

mul= tf.constant([1, 3, 2])

是否可以用a的第一个元素出现一次,第二个出现3次,最后一个元素出现两次来平铺3D张量?

result = [
             [[1, 2, 3]],
             [[4, 5, 6],[4, 5, 6],[4, 5, 6]],
             [[7, 8, 9], [7, 8, 9]]
                                   ]

张量流 0.12

非常感谢。

【问题讨论】:

    标签: python list tensorflow tensor


    【解决方案1】:

    不,这是不可能的。阅读tensors and shapes from the docs

    要理解为什么不可能想象在每行中具有不同数量元素的矩阵。它显然不是矩阵。

    【讨论】:

      【解决方案2】:

      你可以使用 numpy

      import numpy as np
      import tensorflow as tf
      a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
      mul = np.array([1,3,2])
      result = []
      for i in range(len(mul)):
        result.append(np.tile(a[i], (mul[i], 1)))
      result = np.array(result)
      

      【讨论】:

      • 嗨,克里斯,感谢您的帮助。但是,我认为不可能在 Tensorflow 的图表中使用 numpy。我编辑了我的问题以澄清输入。
      • tensorflow 也有 tf.tile。非常类似于 numpy 的 tile。
      • 是的。但是,我想不出一种方法来输出我想要的张量。我认为不可能在 Tensorflow 中获得该输出。
      【解决方案3】:

      我确信在 tensorflow 中不能有非矩形张量。这就是造成问题的原因。否则我只是扩展了@Kris 的代码以完全在 tensorflow 上运行。

      import tensorflow as tf
      
      sess = tf.InteractiveSession()
      
      a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
      mul = tf.constant([1,3,2])
      result = []
      for i in range(3):
        result.append(tf.tile(a[i],[mul[i]]))
      print([r.eval() for r in result])
      #r_tensor = tf.stack(0,[r for r in result]) # Not possible
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-12
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-08
        • 1970-01-01
        相关资源
        最近更新 更多