megengine.optimizer.clip_grad_value

clip_grad_value(tensors, lower, upper)[source]

Clips gradient of an iterable of parameters to a specified lower and upper. Gradients are modified in-place.

The gradients are clipped in the range:

\[\left[\text{lower}, \text{upper}\right]\]
Parameters
  • tensors (Union[Tensor, Iterable[Tensor]]) – an iterable of Tensors or a single Tensor.

  • lower (float) – minimum allowed value of the gradients.

  • upper (float) – maximum allowed value of the gradients.

Returns

None.

Examples

>>> import megengine.optimizer as optim
>>> net = Net()                                                 
>>> optim.clip_grad_value(net.parameters(), lower=-2, upper=5)