r"""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: .. math:: \left[\text{lower}, \text{upper}\right] Args: tensors: an iterable of Tensors or a single Tensor.
(
tensors: Union[Tensor, Iterable[Tensor]], lower: float, upper: float
)
| 50 | |
| 51 | |
| 52 | def clip_grad_value( |
| 53 | tensors: Union[Tensor, Iterable[Tensor]], lower: float, upper: float |
| 54 | ): |
| 55 | r"""Clips gradient of an iterable of parameters to a specified lower and |
| 56 | upper. Gradients are modified in-place. |
| 57 | |
| 58 | The gradients are clipped in the range: |
| 59 | |
| 60 | .. math:: \left[\text{lower}, \text{upper}\right] |
| 61 | |
| 62 | Args: |
| 63 | tensors: an iterable of Tensors or a single Tensor. |
| 64 | lower: minimum allowed value of the gradients. |
| 65 | upper: maximum allowed value of the gradients. |
| 66 | |
| 67 | Returns: |
| 68 | None. |
| 69 | |
| 70 | Examples: |
| 71 | >>> import megengine.optimizer as optim |
| 72 | >>> net = Net() # doctest: +SKIP |
| 73 | >>> optim.clip_grad_value(net.parameters(), lower=-2, upper=5) # doctest: +SKIP |
| 74 | """ |
| 75 | push_scope("clip_grad_value") |
| 76 | if isinstance(tensors, Tensor): |
| 77 | tensors = [tensors] |
| 78 | for tensor in tensors: |
| 79 | if tensor.grad is None: |
| 80 | continue |
| 81 | tensor.grad._reset(clip(tensor.grad, lower, upper)) |
| 82 | pop_scope("clip_grad_value") |