class

PoissonNLLLoss

extendsModule
PoissonNLLLoss(log_input: bool = True, full: bool = False, eps: float = 1e-08, reduction: str = 'mean')
source

Poisson negative log-likelihood loss for count data.

Models the negative log-likelihood under a Poisson observation model. Two modes are supported, controlled by log_input:

log_input=True — the input x is the natural logarithm of the Poisson rate λ\lambda (i.e. x=logλx = \log\lambda):

(x,y)=exyx\ell(x, y) = e^{x} - y \cdot x

log_input=False — the input x is the Poisson rate λ\lambda directly:

(x,y)=xylog(x+ε)\ell(x, y) = x - y \cdot \log(x + \varepsilon)

where ε\varepsilon is added for numerical stability.

When full=True, the Stirling approximation term ylogyy+0.5log(2πy)y\log y - y + 0.5\log(2\pi y) is added to approximate the full log-likelihood including the factorial term.

Parameters

log_inputbool= True
If True, the input is logλ\log\lambda. Default True.
fullbool= False
If True, adds the Stirling approximation term. Default False.
epsfloat= 1e-08
Small constant for numerical stability when log_input=False. Default 1e-8.
reductionstr= 'mean'
'none' | 'mean' (default) | 'sum'.

Attributes

log_inputbool
Whether the input is in log-space.
fullbool
Whether the full approximation is applied.
epsfloat
Numerical stability constant.
reductionstr
The reduction mode.

Notes

  • Input x : ()(*).
  • Target y : ()(*) — non-negative counts.
  • Output : scalar for 'mean' / 'sum'; ()(*) for 'none'.
  • Commonly used for count-valued outputs such as word counts in language modelling or photon counts in imaging.
  • The log_input=True variant is the standard form for neural network outputs because it guarantees λ>0\lambda > 0 and simplifies the gradient.

Examples

Log-input mode (standard neural network output):
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.PoissonNLLLoss(log_input=True)
>>> log_rates = lucid.tensor([1.5, 0.3, -0.5, 2.0])
>>> counts    = lucid.tensor([4.0, 1.0,  0.0, 7.0])
>>> loss = criterion(log_rates, counts)
Direct rate mode with full approximation:
>>> import lucid
>>> import lucid.nn as nn
>>> criterion = nn.PoissonNLLLoss(log_input=False, full=True)
>>> rates  = lucid.tensor([4.5, 1.0, 0.5, 7.5])
>>> counts = lucid.tensor([4.0, 1.0, 0.0, 7.0])
>>> loss = criterion(rates, counts)

Methods (3)

dunder

__init__

None
__init__(log_input: bool = True, full: bool = False, eps: float = 1e-08, reduction: str = 'mean')
source

Initialise the PoissonNLLLoss module. See the class docstring for parameter semantics.

fn

forward

Tensor
forward(x: Tensor, target: Tensor)
source

Compute the loss between predictions and targets.

Parameters

xTensor
Input tensor.
targetTensor
Input tensor.

Returns

Tensor

Scalar loss (or unreduced tensor depending on reduction).

fn

extra_repr

str
extra_repr()
source

Return a string representation of the layer's configuration.