class
PoissonNLLLoss
extends
ModulePoissonNLLLoss(log_input: bool = True, full: bool = False, eps: float = 1e-08, reduction: str = 'mean')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 (i.e. ):
log_input=False — the input x is the Poisson rate
directly:
where is added for numerical stability.
When full=True, the Stirling approximation term
is added to approximate the
full log-likelihood including the factorial term.
Parameters
log_inputbool= TrueIf
True, the input is . Default True.fullbool= FalseIf
True, adds the Stirling approximation term. Default
False.epsfloat= 1e-08Small constant for numerical stability when
log_input=False.
Default 1e-8.reductionstr= 'mean''none' | 'mean' (default) | 'sum'.Attributes
log_inputboolWhether the input is in log-space.
fullboolWhether the full approximation is applied.
epsfloatNumerical stability constant.
reductionstrThe 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=Truevariant is the standard form for neural network outputs because it guarantees 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')Initialise the PoissonNLLLoss module. See the class docstring for parameter semantics.
fn
forward
→Tensorforward(x: Tensor, target: Tensor)Compute the loss between predictions and targets.
Parameters
xTensorInput tensor.
targetTensorInput tensor.
Returns
TensorScalar loss (or unreduced tensor depending on reduction).
fn
extra_repr
→strextra_repr()Return a string representation of the layer's configuration.