Skip to content
fn

poisson_nll_loss

→Tensor
poisson_nll_loss(input: Tensor, target: Tensor, log_input: bool = True, full: bool = False, size_average: bool | None = None, eps: float = 1e-08, reduce: bool | None = None, reduction: Reduction = 'mean')

Poisson negative log-likelihood loss for count regression.

The maximum-likelihood objective when targets are non-negative integer counts modelled as y∼Poisson(λ)y \sim \mathrm{Poisson}(\lambda). Standard for forecasting tasks (web clicks, event counts, biological cell counts) where the variance scales with the mean. Unlike mse_loss, this loss respects the heteroscedasticity inherent in count data.

Parameters

inputTensor
Predicted Poisson rate: log⁡λ\log \lambda by default (log_input=True), the rate λ\lambda itself with log_input=False. A bool or integer input is computed in the default floating dtype, which a float16 target does not narrow.
targetTensor
Observed counts that broadcast with input; a shape that does not raises ValueError. A bool target is RuntimeError beside a bool input and under full; a bool input is, with log_input=False.
log_inputbool= True
Whether input is log⁡λ\log \lambda (default) or λ\lambda. The log-form avoids exponentiating an unbounded prediction in inner loops.
fullbool= False
Include the Stirling approximation term log⁡(y!)≈ylog⁡y−y+12log⁡(2πy)\log(y!) \approx y\log y - y + \tfrac{1}{2}\log(2\pi y) in the loss, where target > 1 (at 0 and 1 the true log⁡(y!)\log(y!) is 0). Has no effect on gradients (constant in input) but yields the correct log-likelihood value.
epsfloat= 1e-08
Small constant added before log⁡\log when log_input=False (default 1e-8).
reductionstr= 'mean'
"mean" (default), "sum", or "none".
size_averagestr= 'mean'
"mean" (default), "sum", or "none".
reducestr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or full-shape per reduction.

Notes

Per-element loss (constant-in-xx terms dropped):

Li={exi−yi xilog_input = Truexi−yilog⁡(xi+ε)log_input = FalseL_i = \begin{cases} e^{x_i} - y_i\,x_i & \text{log\_input = True} \\ x_i - y_i \log(x_i + \varepsilon) & \text{log\_input = False} \end{cases}

Gradient w.r.t. xx is ex−ye^x - y (log-input form) or 1−y/(x+ε)1 - y/(x + \varepsilon) (rate form). Both push λ\lambda toward yy in expectation.

Examples

>>> import lucid
>>> from lucid.nn.functional import poisson_nll_loss
>>> log_lam = lucid.tensor([0.0, 1.0, 2.0])
>>> y = lucid.tensor([1.0, 2.0, 5.0])
>>> poisson_nll_loss(log_lam, y, log_input=True)
tensor(-0.2976)

Used by 2