什么时候使用变量而不是图节点在 TensorFlow 中存储中间结果更好?

When is it better to use a variable rather than a graph node to store intermediate results in TensorFlow?

将张量存储在变量中而不是依赖图中的节点是否有任何优势,除了变量在图的多次运行中持续存在并且可以共享?与图形节点相比,变量是否可能更好地支持资源管理?

(来自@YaroslavBulatov:)

如果您需要在图形的开头存储张量以便稍后重用:

        x
        |            
      conv1   ------        
        |           | 
      conv2         |  
        |           |
      conv3         |  
        |          loooong dependency
       ...          | 
        |           |
     deconv1        |
        |           |
        +   --------
        |
       res

在这里,您在开始时计算 conv1,稍后在计算 res 时在图中重用它。


最好的方法实际上是什么也不做,让 TensorFlow 处理长期依赖。

调用 sess.run(res, feed_dict={x: ...}) 时,TensorFlow 将计算不同的层 conv1conv2、...,并存储计算 res 后一层所需的层(这里 conv1).

我相信这与反向传播的机制相同,TensorFlow 需要在内存中保存激活以稍后计算梯度。