tf.stop_gradient()是否对内存有帮助?

问题描述 投票:2回答:1

在内存上是否有帮助?tf.stop_gradient 实际上有助于节省GPU内存。我问这个问题是因为stop_gradient后面的一些层的中间输出可能不需要被存储(否则梯度计算就会需要)。

python tensorflow deep-learning neural-network gradient
1个回答
0
投票

是的,它可以节省GPU内存。从文档中可以看到 tf.stop_gradient它指出 当构建运算法则来计算梯度时,这个运算法则可以防止其输入的贡献被考虑在内。

再来解释一下 tf.stop_gradient(): 它是一项行动,作为 identity functionforward direction但停止了累积的 gradient 流程中流经该运营商的 backward direction. 它并不妨碍 backpropagation 完全,而是防止单个张量对其贡献 gradients 计算出的表达式的权重。

这意味着,传递给 tf.stop_gradient 在反向传播过程中不更新。

通常情况下,在反向传播过程中,使用公式更新权重。

W(i+1) = W(i) - Learning Rate * Gradient

当我们使用 tf.stop_gradient在此操作中,作为输入的权重被作为常量保存。由于计算过程中的 Back Propagation 减少。内存消耗也会减少.

更多关于 tf.stop_gradient请参考 Tensorflow文档, Abhishek的Stack Overflow回答, mrry的Stack Overflow回答本文.

希望对大家有所帮助。学习愉快!

© www.soinside.com 2019 - 2024. All rights reserved.