有没有办法在张量流中剪切中间爆炸梯度

use*_*268 6 adam gradient clipping deep-learning tensorflow

问题:一个很长的RNN网

N1 -- N2 -- ... --- N100
Run Code Online (Sandbox Code Playgroud)

对于类似的优化器AdamOptimizer,compute_gradient()将为所有训练变量提供渐变.

但是,它可能会在某个步骤中爆炸.

类似于如何有效地应用梯度剪切张量流的方法 可以剪切大的最终梯度.

但是如何剪辑那些中级的呢?

一种方法可能是从"N100 - > N99"手动执行backprop,剪辑渐变,然后是"N99 - > N98"等等,但这太复杂了.

所以我的问题是:是否有更简单的方法来剪辑中间渐变?(当然,严格来说,它们不再是数学意义上的渐变)

Ale*_*sos 0

您可以使用custom_gradient装饰器制作一个tf.identity剪辑中间分解渐变的版本。

``` 来自tensorflow.contrib.eager.python import tfe

@tfe.custom_gradient defgradient_clipping_identity(张量,max_norm):结果= tf.identity(张量)

def grad(dresult): 返回 tf.clip_by_norm(dresult, max_norm), 无

返回结果,等级```

然后gradient_clipping_identity像通常使用恒等一样使用,并且您的渐变将在向后传递中被剪裁。