評價此頁

PoissonNLLLoss#

class torch.nn.modules.loss.PoissonNLLLoss(log_input=True, full=False, size_average=None, eps=1e-08, reduce=None, reduction='mean')[原始碼]#

目標為泊松分佈時的負對數似然損失。

損失函式可以描述為:

targetPoisson(input)loss(input,target)=inputtargetlog(input)+log(target!)\text{target} \sim \mathrm{Poisson}(\text{input}) \text{loss}(\text{input}, \text{target}) = \text{input} - \text{target} * \log(\text{input}) + \log(\text{target!})

最後一項可以省略或使用斯特林公式進行近似。當 target 值大於 1 時使用近似。對於 target 小於或等於 1 的情況,會向損失值中新增零。

引數
  • log_input (bool, optional) – 如果為 True,則損失計算為 exp(input)targetinput\exp(\text{input}) - \text{target}*\text{input};如果為 False,則損失為 inputtargetlog(input+eps)\text{input} - \text{target}*\log(\text{input}+\text{eps})

  • full (bool, optional) –

    是否計算完整的損失,即加上斯特林近似項

    targetlog(target)target+0.5log(2πtarget).\text{target}*\log(\text{target}) - \text{target} + 0.5 * \log(2\pi\text{target}).

  • size_average (bool, optional) – 已棄用 (參見 reduction)。預設情況下,損失值在批次中的每個損失元素上取平均值。請注意,對於某些損失,每個樣本有多個元素。如果欄位 size_average 設定為 False,則損失值在每個小批次中而是求和。當 reduceFalse 時忽略。預設值:True

  • eps (float, optional) – 當 log_input = False 時,用於避免計算 log(0)\log(0) 的小值。預設值:1e-8

  • reduce (bool, optional) – 已棄用 (參見 reduction)。預設情況下,損失值在每個小批次中根據 size_average 對觀測值進行平均或求和。當 reduceFalse 時,返回每個批次元素的損失值,並忽略 size_average。預設值:True

  • reduction (str, optional) – 指定應用於輸出的歸約方式:'none' | 'mean' | 'sum''none':不進行歸約;'mean':輸出的總和除以輸出中的元素數量;'sum':對輸出進行求和。注意:size_averagereduce 正在被棄用,在此期間,指定這兩個引數中的任何一個都會覆蓋 reduction。預設值:'mean'

示例

>>> loss = nn.PoissonNLLLoss()
>>> log_input = torch.randn(5, 2, requires_grad=True)
>>> target = torch.randn(5, 2)
>>> output = loss(log_input, target)
>>> output.backward()
形狀
  • 輸入: ()(*),其中 * 表示任意數量的維度。

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

  • 輸出:預設情況下為標量。如果 reduction'none',則輸出形狀與輸入相同。

forward(log_input, target)[原始碼]#

執行前向傳播。

返回型別

張量