Clip by global norm. The global norm is the sum of norm for **all** gradients. See :func:`tf.clip_by_global_norm` for more information.
| 81 | |
| 82 | |
| 83 | class GlobalNormClip(GradientProcessor): |
| 84 | """ Clip by global norm. |
| 85 | The global norm is the sum of norm for **all** gradients. |
| 86 | |
| 87 | See :func:`tf.clip_by_global_norm` for more information. |
| 88 | """ |
| 89 | |
| 90 | def __init__(self, global_norm): |
| 91 | """ |
| 92 | Args: |
| 93 | global_norm(float): the threshold to clip with. |
| 94 | """ |
| 95 | super(GlobalNormClip, self).__init__() |
| 96 | self._norm = float(global_norm) |
| 97 | |
| 98 | def _process(self, grads): |
| 99 | g = [k[0] for k in grads] |
| 100 | v = [k[1] for k in grads] |
| 101 | g, _ = tf.clip_by_global_norm(g, self._norm, name='clip_by_global_norm') |
| 102 | return list(zip(g, v)) |
| 103 | |
| 104 | |
| 105 | class MapGradient(GradientProcessor): |