[docs]defclip_grad_norm_(parameters:_tensor_or_tensors,max_norm:float,norm_type:float=2.0,error_if_nonfinite:bool=False)->torch.Tensor: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. Gradients are modified in-place. Args: parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a single Tensor that will have gradients normalized max_norm (float or int): max norm of the gradients norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for infinity norm. error_if_nonfinite (bool): if True, an error is thrown if the total norm of the gradients from :attr:``parameters`` is ``nan``, ``inf``, or ``-inf``. Default: False (will switch to True in the future) Returns: Total norm of the parameters (viewed as a single vector). """ifisinstance(parameters,torch.Tensor):parameters=[parameters]parameters=[pforpinparametersifp.gradisnotNone]max_norm=float(max_norm)norm_type=float(norm_type)iflen(parameters)==0:returntorch.tensor(0.)device=parameters[0].grad.deviceifnorm_type==inf:norms=[p.grad.detach().abs().max().to(device)forpinparameters]total_norm=norms[0]iflen(norms)==1elsetorch.max(torch.stack(norms))else:total_norm=torch.norm(torch.stack([torch.norm(p.grad.detach(),norm_type).to(device)forpinparameters]),norm_type)iferror_if_nonfiniteandtorch.logical_or(total_norm.isnan(),total_norm.isinf()):raiseRuntimeError(f'The total norm of order {norm_type} for gradients from ''`parameters` is non-finite, so it cannot be clipped. To disable ''this error and scale the gradients by the non-finite norm anyway, ''set `error_if_nonfinite=False`')clip_coef=max_norm/(total_norm+1e-6)# Note: multiplying by the clamped coef is redundant when the coef is clamped to 1, but doing so# avoids a `if clip_coef < 1:` conditional which can require a CPU <=> device synchronization# when the gradients do not reside in CPU memory.clip_coef_clamped=torch.clamp(clip_coef,max=1.0)forpinparameters:p.grad.detach().mul_(clip_coef_clamped.to(p.grad.device))returntotal_norm
defclip_grad_norm(parameters:_tensor_or_tensors,max_norm:float,norm_type:float=2.,error_if_nonfinite:bool=False)->torch.Tensor:r"""Clips gradient norm of an iterable of parameters. .. warning:: This method is now deprecated in favor of :func:`torch.nn.utils.clip_grad_norm_`. """warnings.warn("torch.nn.utils.clip_grad_norm is now deprecated in favor ""of torch.nn.utils.clip_grad_norm_.",stacklevel=2)returnclip_grad_norm_(parameters,max_norm,norm_type,error_if_nonfinite)
[docs]defclip_grad_value_(parameters:_tensor_or_tensors,clip_value:float)->None:r"""Clips gradient of an iterable of parameters at specified value. Gradients are modified in-place. Args: parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a single Tensor that will have gradients normalized clip_value (float or int): maximum allowed value of the gradients. The gradients are clipped in the range :math:`\left[\text{-clip\_value}, \text{clip\_value}\right]` """ifisinstance(parameters,torch.Tensor):parameters=[parameters]clip_value=float(clip_value)forpinfilter(lambdap:p.gradisnotNone,parameters):p.grad.data.clamp_(min=-clip_value,max=clip_value)
Docs
Access comprehensive developer documentation for PyTorch
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.