get_loss#
- mlquantify.losses.get_loss(loss='hellinger', normalize=True, **kwargs)[source]#
Instantiate a loss object from a string identifier or return a callable.
Provides a unified entry point for retrieving optimization loss objects used in distribution-matching quantifiers. If
lossis already callable it is returned unchanged.- Parameters:
- lossstr or callable, default=’hellinger’
Loss identifier. Accepted string values:
'hellinger','topsoe','probsymm','sqEuclidean','euclidean'—DistanceLoss.'least_squares','least-squares','least squares','ls','l2'—LeastSquaresLoss.'hellinger_surrogate','hd_surrogate'—HellingerSurrogateLoss.'nll','negative_log_likelihood'—NegativeLogLikelihoodLoss.'energy','energy_distance'—EnergyLoss.'mixture_nll','ml'—MixtureNegativeLogLikelihoodLoss.'regularized_mixture_nll','regularized_ml'—RegularizedMixtureNLLLoss.
A callable is returned as-is.
- normalizebool, default=True
Passed to distance and surrogate losses to control whether inputs are normalized to valid probability vectors before evaluation.
- **kwargs
Additional keyword arguments forwarded to the chosen loss class constructor.
- Returns:
- loss_fnBaseLoss or callable
The configured loss object.
- Raises:
- ValueError
If
lossis a string not matching any recognised identifier.
Examples
>>> from mlquantify.losses import get_loss >>> loss = get_loss("hellinger") >>> loss([0.4, 0.6], [0.5, 0.5]) 0.076... >>> loss = get_loss("least_squares") >>> loss([0.4, 0.6], [0.5, 0.5]) 0.02