RegularizedMixtureNLLLoss#
- class mlquantify.losses.RegularizedMixtureNLLLoss(tau_0=0.0, tau_1=0.0, reduction='mean')[source]#
Mixture NLL with optional ordinal-smoothness regularization.
Extends
MixtureNegativeLogLikelihoodLosswith first- and second-order penalty terms that encourage a smooth prevalence vector when the classes have an ordinal interpretation.The total loss is
\[\mathcal{L} = \text{NLL}(\hat{p}) + \frac{\tau_0}{2} \sum_{c} (\hat{p}_{c+1} - \hat{p}_c)^2 + \frac{\tau_1}{2} \sum_{c} (-\hat{p}_{c} + 2\hat{p}_{c+1} - \hat{p}_{c+2})^2\]- Parameters:
- tau_0float, default=0.0
Weight of the first-order smoothness penalty. Set to
0to disable.- tau_1float, default=0.0
Weight of the second-order smoothness penalty. Set to
0to disable.- reduction{‘mean’, ‘sum’}, default=’mean’
How to reduce the per-sample log-likelihood values.
- Attributes:
- tau_0float
First-order regularization weight.
- tau_1float
Second-order regularization weight.
- reductionstr
The configured reduction mode.
Examples
>>> from mlquantify.losses import get_loss >>> import numpy as np >>> loss = get_loss("regularized_mixture_nll", tau_0=0.1) >>> prev = np.array([0.3, 0.4, 0.3]) >>> lkl = np.array([[0.2, 0.5, 0.3], [0.6, 0.3, 0.1], [0.1, 0.2, 0.7]]) >>> loss(prev, lkl) 1.1...
- __call__(prevalences, class_likelihoods)[source]#
Compute the regularized mixture negative log-likelihood.
- Parameters:
- prevalencesarray-like of shape (n_classes,)
Estimated class prevalence vector.
- class_likelihoodsarray-like of shape (n_classes, n_samples) or (n_samples, n_classes)
Per-class likelihood for each test instance.
- Returns:
- lossfloat
Mixture NLL plus any active smoothness penalties.
Examples
>>> from mlquantify.losses import get_loss >>> import numpy as np >>> loss = get_loss("regularized_ml", tau_0=0.0) >>> prev = np.array([0.5, 0.5]) >>> lkl = np.array([[0.2, 0.8], [0.7, 0.3]]) >>> round(loss(prev, lkl), 4) 0.6931