Rate this Page

PoissonNLLLoss#

classtorch.nn.PoissonNLLLoss(log_input=True,full=False,size_average=None,eps=1e-08,reduce=None,reduction='mean')[source]#

Negative log likelihood loss with Poisson distribution of target.

The loss can be described as:

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!})

The last term can be omitted or approximated with Stirling formula. Theapproximation is used for target values more than 1. For targets less orequal to 1 zeros are added to the loss.

Parameters
  • log_input (bool,optional) – ifTrue the loss is computed asexp(input)targetinput\exp(\text{input}) - \text{target}*\text{input}, ifFalse the loss isinputtargetlog(input+eps)\text{input} - \text{target}*\log(\text{input}+\text{eps}).

  • full (bool,optional) –

    whether to compute full loss, i. e. to add theStirling approximation term

    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) – Deprecated (seereduction). By default,the losses are averaged over each loss element in the batch. Note that forsome losses, there are multiple elements per sample. If the fieldsize_averageis set toFalse, the losses are instead summed for each minibatch. Ignoredwhenreduce isFalse. Default:True

  • eps (float,optional) – Small value to avoid evaluation oflog(0)\log(0) whenlog_input=False. Default: 1e-8

  • reduce (bool,optional) – Deprecated (seereduction). By default, thelosses are averaged or summed over observations for each minibatch dependingonsize_average. Whenreduce isFalse, returns a loss perbatch element instead and ignoressize_average. Default:True

  • reduction (str,optional) – Specifies the reduction to apply to the output:'none' |'mean' |'sum'.'none': no reduction will be applied,'mean': the sum of the output will be divided by the number ofelements in the output,'sum': the output will be summed. Note:size_averageandreduce are in the process of being deprecated, and in the meantime,specifying either of those two args will overridereduction. Default:'mean'

Examples

>>>loss=nn.PoissonNLLLoss()>>>log_input=torch.randn(5,2,requires_grad=True)>>>target=torch.randn(5,2)>>>output=loss(log_input,target)>>>output.backward()
Shape:
  • Input:()(*), where* means any number of dimensions.

  • Target:()(*), same shape as the input.

  • Output: scalar by default. Ifreduction is'none', then()(*),the same shape as the input.

forward(log_input,target)[source]#

Runs the forward pass.

Return type

Tensor