clip_grad_value_

paddle.nn.utils. clip_grad_value_ ( parameters, clip_value ) [source]

Clips gradient of an iterable of parameters at specified value. The gradient will be modified in place. This API can only run in dynamic graph mode, not static graph mode.

Parameters
  • parameters (Iterable[paddle.Tensor]|paddle.Tensor) – Tensors or a single Tensor that will be normalized gradients

  • clip_value (float|int) – maximum allowed value of the gradients. The gradients are clipped in the range \(\left[\text{-clip\_value}, \text{clip\_value}\right]\)

Example

>>> import paddle
>>> x = paddle.uniform([10, 10], min=-10.0, max=10.0, dtype='float32')
>>> clip_value = float(5.0)
>>> linear = paddle.nn.Linear(in_features=10, out_features=10)
>>> out = linear(x)
>>> loss = paddle.mean(out)
>>> loss.backward()
>>> paddle.nn.utils.clip_grad_value_(linear.parameters(), clip_value)
>>> sdg = paddle.optimizer.SGD(learning_rate=0.1, parameters=linear.parameters())
>>> sdg.step()