From d23ea4ef8ebc637534c5abafca995be257f83751 Mon Sep 17 00:00:00 2001 From: fengjiayi Date: Fri, 12 Jan 2018 16:47:57 +0800 Subject: [PATCH] add gradient clip by norm --- python/paddle/v2/fluid/clip.py | 12 ++++++++++++ python/paddle/v2/fluid/layers/ops.py | 1 + 2 files changed, 13 insertions(+) diff --git a/python/paddle/v2/fluid/clip.py b/python/paddle/v2/fluid/clip.py index b1fd1c2b65f..eb75018d779 100644 --- a/python/paddle/v2/fluid/clip.py +++ b/python/paddle/v2/fluid/clip.py @@ -77,6 +77,18 @@ class GradientClipByValue(BaseGradientClipAttr): return param, new_grad +class GradientClipByNorm(BaseGradientClipAttr): + def __init__(self, clip_norm): + self.clip_norm = clip_norm + + def process_context(self, context, p_g): + pass + + def create_operators(self, param, grad): + new_grad = layers.clip_by_norm(x=grad, max_norm=self.clip_norm) + return param, new_grad + + def append_gradient_clip_ops(param_grad): context = dict() create_op_callbacks = [] diff --git a/python/paddle/v2/fluid/layers/ops.py b/python/paddle/v2/fluid/layers/ops.py index d3a5b707859..884e84011d9 100644 --- a/python/paddle/v2/fluid/layers/ops.py +++ b/python/paddle/v2/fluid/layers/ops.py @@ -16,6 +16,7 @@ __all__ = [ 'elementwise_sub', 'elementwise_mul', 'clip', + 'clip_by_norm', 'sequence_softmax', ] + __activations__ -- GitLab