r"""Clips gradient norm of an iterable of parameters. The norm is computed over all gradients together, as if they were concatenated into a single vector. You can set the max_norm and norm_type. For more details, you can refer to the documentation of each optimizer(like A
(self, error_if_nonfinite: bool = False)
| 404 | raise NotImplementedError() |
| 405 | |
| 406 | def clip_grad(self, error_if_nonfinite: bool = False): |
| 407 | r"""Clips gradient norm of an iterable of parameters. |
| 408 | The norm is computed over all gradients together, as if they were concatenated into a single vector. |
| 409 | |
| 410 | You can set the max_norm and norm_type. |
| 411 | |
| 412 | For more details, you can refer to the documentation of each optimizer(like Adam, SGD and so on). |
| 413 | |
| 414 | You can also refer the code in :func:`oneflow.nn.utils.clip_grad_norm_` |
| 415 | |
| 416 | Args: |
| 417 | error_if_nonfinite (bool): if True, an error is thrown if the total |
| 418 | norm of the gradients from :attr:``parameters`` is ``nan``, |
| 419 | ``inf``, or ``-inf``. Default: False (will switch to True in the future) |
| 420 | |
| 421 | """ |
| 422 | for param_group in self.param_groups: |
| 423 | if param_group._enable_clip_grad: |
| 424 | clip_grad_norm_( |
| 425 | param_group.parameters, |
| 426 | param_group["clip_grad_max_norm"], |
| 427 | param_group["clip_grad_norm_type"], |
| 428 | error_if_nonfinite, |
| 429 | param_group.get("fused", False), |
| 430 | ) |
| 431 | else: |
| 432 | warnings.warn( |
| 433 | "To enable clip_grad, passing the `clip_grad_max_norm` and `clip_grad_norm_type` parameters when instantializing the Optimizer." |
| 434 | ) |
| 435 | |
| 436 | def zero_grad(self, set_to_none: bool = False): |
| 437 | """Sets the gradients of all optimized :class:`oneflow.Tensor` s to zero. |