megengine.optimizer.clip_grad_norm

clip_grad_norm(tensors, max_norm, ord=2.0)[源代码]

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. Gradients are modified in-place.

参数
  • tensors (Union[Tensor, Iterable[Tensor]]) – an iterable of Tensors or a single Tensor.

  • max_norm (float) – max norm of the gradients.

  • ord (float) – type of the used p-norm. Can be 'inf' for infinity norm.

返回

total norm of the parameters (viewed as a single vector).