【问题标题】:Reshaping batch variable in tensorflow在张量流中重塑批处理变量
【发布时间】:2017-10-19 15:08:00
【问题描述】:

在 Tensorflow 中,我想重塑一个批处理变量 A 的形式 [100, 32, 32] 转换为 [10, 10, 32, 32] 形式的 B。但是天真地使用 reshape 会丢失订单信息。

例如,A[10,:,:]B[1,0,:,:] 不同,其中 1 表示 10 秒的小批量的下一行。

我希望32*32 的订单因为它是一个图像而保持不变,而我想以以下方式重塑其中的 100 个图像:

1 -> 2 -> ... -> 10

11 -> 12 -> ... -> 20

... ...

91 -> 92 -> ... -> 100

如何在张量流中做到这一点?

【问题讨论】:

    标签: tensorflow reshape transpose


    【解决方案1】:

    tf.reshape 做你想做的事,如果我正确理解你想要什么的话。例如:

    data = np.array(range(100*32*32)).reshape((100,32,32))
    A = tf.constant(data)
    B = tf.reshape(A, [10, 10, 32, 32])
    
    with tf.Session() as sess:
        print("A")
        print(sess.run(A)[10, :, :])
        print()
        print("B")
        print(sess.run(B)[1, 0, :, :])
    

    输出:

    A
     [[10240 10241 10242 ..., 10269 10270 10271]
      [10272 10273 10274 ..., 10301 10302 10303]
      [10304 10305 10306 ..., 10333 10334 10335]
      ...,
      [11168 11169 11170 ..., 11197 11198 11199]
      [11200 11201 11202 ..., 11229 11230 11231]
      [11232 11233 11234 ..., 11261 11262 11263]]
    
    B
     [[10240 10241 10242 ..., 10269 10270 10271]
      [10272 10273 10274 ..., 10301 10302 10303]
      [10304 10305 10306 ..., 10333 10334 10335]
      ...,
      [11168 11169 11170 ..., 11197 11198 11199]
      [11200 11201 11202 ..., 11229 11230 11231]
      [11232 11233 11234 ..., 11261 11262 11263]]
    

    【讨论】:

      【解决方案2】:

      您可以使用tf.unstack,然后使用tf.stack

      例如:

      A = tf.random_uniform([100, 32, 32])
      temp = tf.unstack(t)
      B = tf.stack([temp[pos:pos+10] for pos in range(0, len(temp), 10)]
      

      或者更透明和灵活一点:

      def chunker(seq, size):
          return [seq[pos:pos+size] for pos in range(0, len(seq), size)]
      
      B = tf.stack(chunker(temp,10))
      

      【讨论】:

        猜你喜欢
        • 2020-10-13
        • 2020-07-21
        • 1970-01-01
        • 1970-01-01
        • 2019-11-03
        • 2020-10-14
        • 2016-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多