捷徑

SmoothL1Loss

class torch.nn.SmoothL1Loss(size_average=None, reduce=None, reduction='mean', beta=1.0)[原始碼][原始碼]

建立一個 criterion,如果 element-wise 的絕對誤差小於 beta,則使用平方項;否則使用 L1 項。 它對於離群值(outlier)的敏感度低於 torch.nn.MSELoss,並且在某些情況下可以防止梯度爆炸(例如,請參閱 Ross Girshick 的論文 Fast R-CNN)。

對於大小為 NN 的批次(batch),未簡化的損失可以描述為

(x,y)=L={l1,...,lN}T\ell(x, y) = L = \{l_1, ..., l_N\}^T

其中

ln={0.5(xnyn)2/beta,if xnyn<betaxnyn0.5beta,otherwise l_n = \begin{cases} 0.5 (x_n - y_n)^2 / beta, & \text{if } |x_n - y_n| < beta \\ |x_n - y_n| - 0.5 * beta, & \text{otherwise } \end{cases}

如果 reduction 不是 none,則:

(x,y)={mean(L),if reduction=‘mean’;sum(L),if reduction=‘sum’.\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases}

注意

Smooth L1 loss 可以被看作與 L1Loss 完全相同,但將 xy<beta|x - y| < beta 的部分替換為一個二次函數,使其在 xy=beta|x - y| = beta 的斜率為 1。 二次部分使 L1 loss 在 xy=0|x - y| = 0 附近變得平滑。

注意

Smooth L1 loss 與 HuberLoss 密切相關,相當於 huber(x,y)/betahuber(x, y) / beta (請注意,Smooth L1 的 beta 超參數也稱為 Huber 的 delta)。 這導致以下差異

  • 當 beta -> 0 時,Smooth L1 loss 會收斂到 L1Loss,而 HuberLoss 會收斂到一個常數 0 的 loss。當 beta 為 0 時,Smooth L1 loss 等同於 L1 loss。

  • 當 beta -> ++\infty 時,Smooth L1 loss 會收斂到一個常數 0 的 loss,而 HuberLoss 會收斂到 MSELoss

  • 對於 Smooth L1 loss,當 beta 改變時,loss 的 L1 部分具有恆定的斜率 1。對於 HuberLoss,L1 部分的斜率是 beta。

參數
  • size_average (bool, optional) – 已棄用 (請參閱 reduction)。 預設情況下,losses 會在 batch 中針對每個 loss 元素進行平均。 請注意,對於某些 losses,每個 sample 有多個元素。 如果欄位 size_average 設定為 False,則 losses 會針對每個 minibatch 進行加總。 當 reduceFalse 時會被忽略。 預設值:True

  • reduce (bool, optional) – 已棄用 (請參閱 reduction)。 預設情況下,losses 會根據 size_average 在每個 minibatch 的 observations 上進行平均或加總。 當 reduceFalse 時,會改為傳回每個 batch 元素的 loss,並忽略 size_average。 預設值:True

  • reduction (str, optional) – 指定要套用到輸出的 reduction:'none' | 'mean' | 'sum''none':不會套用任何 reduction,'mean':輸出的總和將除以輸出中的元素數量,'sum':輸出將被加總。 注意:size_averagereduce 正在棄用中,在此期間,指定這兩個參數中的任何一個都將覆寫 reduction。 預設值:'mean'

  • beta (float, optional) – 指定在 L1 和 L2 loss 之間切換的閾值。 該值必須為非負數。 預設值:1.0

形狀
  • 輸入:()(*),其中 * 表示任何數量的維度。

  • 目標:()(*),與輸入相同的形狀。

  • 輸出:純量。 如果 reduction'none',則 ()(*),與輸入相同的形狀。

文件

取得 PyTorch 的完整開發者文件

檢視文件

教學

取得初學者和進階開發者的深度教學

檢視教學

資源

尋找開發資源並獲得您的問題解答

檢視資源