【问题标题】:Iteratively build tensor in Tensorflow在 Tensorflow 中迭代构建张量
【发布时间】:2022-01-17 18:43:51
【问题描述】:

假设我有一个函数,它在输入(给定维度)中接受一个张量,并在输出中返回另一个张量。我想在一批输入上使用该函数,并希望它返回一批输出。所以输入和输出都会多一维。

我可以写一个 tf.while_loop 来对批处理中的所有输入执行我的函数,但我不确定如何在批处理中存储单个元素的输出。 我有一个关于如何做到这一点的想法,它也应该澄清我正在尝试做的事情,但我不确定它是否是最佳的。

batch = tf.random.uniform([4,3,2]) #batch of size 4 of (3,2) shaped tensors
output = tf.zeros([0,5]) #let's say that the output should be a batch of 4 (4,5) shaped     tensors.
#I will concatenate the single outputs to this tensor and then reshape it
for i in tf.range(len(batch)):
 output = tf.concat((output,MyVeryNiceFunction(batch[i])),0) #MyVeryNiceFunction     returns a (4,5) shaped tensor
output = tf.reshape(output,(4,4,5)) #(batch_size,(shape of tensor))
return output 

这段代码肯定给出了我想要的输出,但它允许并行循环的每次执行吗? 有一个更好的方法吗?是否有适当的数据结构可以让我存储每个循环执行的输出,然后从中有效地构建输出张量?

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    一般来说,迭代一个维度很可能是错误的方法。在 TF(以及 Matlab 和 Numpy)中,目标是矢量化 - 以一种可以同时触及批次的所有元素的方式描述您的操作。

    例如,假设我的数据集由长度为 2 的向量组成,我有一批 4 个。

    data = tf.convert_to_tensor([[1,2], [3,4], [5,6], [7,8]], tf.float32)
    >>> data
    <tf.Tensor: shape=(4, 2), dtype=float32, numpy=
    array([[1., 2.],
           [3., 4.],
           [5., 6.],
           [7., 8.]], dtype=float32)>
    
    

    如果您想以向量化的方式向每个向量添加一个元素,添加某种统计分析(例如方差),您可以这样做。请注意您是如何不断思考张量的形状和尺寸以及如何连接/附加张量的。经常记录张量形状甚至断言它们是很常见的。欢迎来到 TF 编程。

    vars = tf.math.reduce_variance(data, axis=1, keepdims=True)
    tf.debugging.assert_equal(tf.shape(vars), [4, 1])
    tf.concat(values=[data, vars], axis=1)
    
    
    <tf.Tensor: shape=(4, 3), dtype=float32, numpy=
    array([[1.  , 2.  , 0.25],
           [3.  , 4.  , 0.25],
           [5.  , 6.  , 0.25],
           [7.  , 8.  , 0.25]], dtype=float32)>
    

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 2018-11-11
      • 1970-01-01
      • 2017-03-20
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多