fn

poisson_nll_loss

Tensor
poisson_nll_loss(x: Tensor, target: Tensor, log_input: bool = True, full: bool = False, eps: float = 1e-08, reduction: str = 'mean')
source

Poisson negative log-likelihood loss for count regression.

The maximum-likelihood objective when targets are non-negative integer counts modelled as yPoisson(λ)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

xTensor
Predicted Poisson rate. By default (log_input=True) treated as logλ\log \lambda for numerical stability; set log_input=False to pass the rate λ\lambda directly.
targetTensor
Observed counts, broadcast-compatible with x.
log_inputbool= True
Whether x 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!)ylogyy+12log(2πy)\log(y!) \approx y\log y - y + \tfrac{1}{2}\log(2\pi y) in the loss. Has no effect on gradients (constant in x) but yields the correct log-likelihood value. Not currently added — kept for API parity.
epsfloat= 1e-08
Small constant added before log\log when log_input=False (default 1e-8).
reductionstr= 'mean'
"mean" (default), "sum", or "none".

Returns

Tensor

Scalar or full-shape per reduction.

Notes

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

Li={exiyixilog_input = Truexiyilog(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 exye^x - y (log-input form) or 1y/(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.7299...)