pytorch梯度裁剪(Clipping Gradient):torch.nn.utils.clip_grad_norm


torch.nn.utils.clip_grad_norm(parameters, max_norm, norm_type=2)

1、梯度裁剪原理(http://blog.csdn.net/qq_29340857/article/details/70574528)

 既然在BP過程中會產生梯度消失/爆炸(就是偏導無限接近0,導致長時記憶無法更新),那么最簡單粗暴的方法,設定閾值,當梯度小於/大於閾值時,更新的梯度為閾值,如下圖所示:
這里寫圖片描述

優點:簡單粗暴
缺點:很難找到滿意的閾值

2、nn.utils.clip_grad_norm(parameters, max_norm, norm_type=2)

這個函數是根據參數的范數來衡量的

Parameters:

    • parameters (Iterable[Variable]) – 一個基於變量的迭代器,會進行歸一化(原文:an iterable of Variables that will have gradients normalized)
    • max_norm (float or int) – 梯度的最大范數(原文:max norm of the gradients)
    • norm_type(float or int) – 規定范數的類型,默認為L2(原文:type of the used p-norm. Can be'inf'for infinity norm)

Returns:參數的總體范數(作為單個向量來看)(原文:Total norm of the parameters (viewed as a single vector).)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM