捷徑

torch.nn.utils.clip_grad_norm_

torch.nn.utils.clip_grad_norm_(parameters, max_norm, norm_type=2.0, error_if_nonfinite=False, foreach=None)[source][source]

對參數的可迭代對象的梯度範數進行裁剪。

範數是針對所有參數的個別梯度的範數計算的,就像個別梯度的範數被連接成單個向量一樣。梯度會就地修改。

此函數等效於 torch.nn.utils.get_total_norm() 接著執行 torch.nn.utils.clip_grads_with_norm_(),並使用 get_total_norm 傳回的 total_norm

參數
  • parameters (Iterable[Tensor] or Tensor) – 將要正規化梯度的 Tensor 的可迭代對象或單個 Tensor

  • max_norm (float) – 梯度的最大範數

  • norm_type (float) – 使用的 p-norm 的類型。可以是 'inf' 表示無窮範數。

  • error_if_nonfinite (bool) – 如果為 True,當 parameters 中梯度的總範數為 naninf-inf 時,會擲回錯誤。預設值:False(未來會切換為 True)

  • foreach (bool) – 使用更快的基於 foreach 的實作。如果 None,則針對 CUDA 和 CPU 原生 Tensor 使用 foreach 實作,並針對其他裝置類型靜默地回退到較慢的實作。預設值:None

返回值

參數梯度的總範數(視為單個向量)。

返回類型

Tensor

文件

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources