dicee.losses.custom_losses
Alternative loss functions selectable via the loss_fn config option.
BaseKGE.__init__ (dicee/models/base_model.py) instantiates one of
these in place of the framework’s default loss based on
args["loss_fn"]; see that dispatch for the exact string values and
which additional config options (label_smoothing_rate,
label_relaxation_alpha) feed each one’s constructor.
Several of these declare a current_epoch parameter on forward so
they can vary their behavior over training; BaseKGE.loss_function
detects this once per model (self._loss_needs_epoch, set by inspecting
self.loss.forward’s parameters) and forwards the current epoch only to
losses that declare it.
Classes
Plain |
|
BCE weighted by each prediction's own confidence. |
|
Fixed-rate label-smoothing loss, selectable via |
|
KL-divergence label smoothing with a smoothing factor that self-adjusts |
|
Label relaxation loss, selectable via |
|
|
|
|
|
Switches from |
|
Switches from |
|
Fixed 0.4/0.6 weighted average of |
|
Adaptive and Conditional Label Smoothing-style calibration loss, |
Module Contents
- class dicee.losses.custom_losses.DefaultBCELoss[source]
Bases:
torch.nn.ModulePlain
BCEWithLogitsLoss, selectable vialoss_fn="BCELoss".Equivalent to the framework’s own default entity/relation-prediction loss (see
BaseKGE.__init__); provided as an explicitloss_fnoption for parity with the other choices in this module.
- class dicee.losses.custom_losses.WeightedBCELoss[source]
Bases:
torch.nn.ModuleBCE weighted by each prediction’s own confidence.
Down-weights low-confidence predictions (
sigmoid(pred)near 0.5) and up-weights confident ones, clamped to[0.5, 1.0]. Weights are detached, so they don’t receive gradients themselves.current_epochis accepted for interface consistency with the otherloss_fnoptions but is currently unused.
- class dicee.losses.custom_losses.LabelSmoothingLoss(smoothness_ratio=0.0)[source]
Bases:
torch.nn.ModuleFixed-rate label-smoothing loss, selectable via
loss_fn="LS"(smoothness_ratiocomes from thelabel_smoothing_rateconfig option).Converts
pred(logits) to log-probabilities and compares against a smoothed target distribution (masssmoothness_ratioredistributed uniformly over the non-target classes) viaKL(pred || smoothed_target)— the same fixed-rate variant ofAdaptiveLabelSmoothingLoss’s per-call smoothing, without the batch-to-batch adaptation.This is independent of the (separately, correctly implemented) dataset-level smoothing controlled by the same
label_smoothing_rateoption (dicee/dataset_classes/_label_based.py,_negative_sampling.py), which bakes smoothing into the training targets regardless ofloss_fn.- Parameters:
smoothness_ratio (float) – Fraction of probability mass redistributed from the target class(es) to the non-target classes;
0recovers a KL-divergence loss with no smoothing.
- smoothness_ratio = 0.0
- class dicee.losses.custom_losses.AdaptiveLabelSmoothingLoss(min_smoothing_factor=0.01, max_smoothing_factor=0.2, smoothing_factor_step=0.01, initial_smoothing_factor=0.1)[source]
Bases:
torch.nn.ModuleKL-divergence label smoothing with a smoothing factor that self-adjusts from batch to batch, selectable via
loss_fn="AdaptiveLabelSmoothingLoss".Converts
logitsto log-probabilities and compares against a smoothed target distribution (masssmoothing_factorredistributed uniformly over the non-target classes) viaKL(pred || smoothed_target). After each call,smoothing_factormoves onesmoothing_factor_steptowardmax_smoothing_factorif the loss increased since the previous call, or towardmin_smoothing_factorif it decreased — i.e. it backs off smoothing while the model is improving and leans into it when training stalls.current_epochis accepted for interface consistency with the otherloss_fnoptions but is currently unused.This module is stateful (
prev_loss,smoothing_factorpersist across calls), so a fresh instance should be used per training run.- Parameters:
min_smoothing_factor (float) – Lower bound the adaptive smoothing factor can decay to.
max_smoothing_factor (float) – Upper bound the adaptive smoothing factor can grow to.
smoothing_factor_step (float) – Adjustment applied to the smoothing factor after each call.
initial_smoothing_factor (float) – Starting value before any adjustment has occurred.
- min_smoothing_factor = 0.01
- max_smoothing_factor = 0.2
- smoothing_factor_step = 0.01
- smoothing_factor = 0.1
- prev_loss = None
- eps = 1e-14
- class dicee.losses.custom_losses.LabelRelaxationLoss(alpha=0.0)[source]
Bases:
torch.nn.ModuleLabel relaxation loss, selectable via
loss_fn="LRLoss".Based on “From Label Smoothing to Label Relaxation” (Lienen & Hullermeier, AAAI 2021): instead of smoothing toward one fixed soft target, it constructs a credal set of distributions that assign at least
1 - alphaprobability to the target class(es) and measures the KL divergence to the closest point in that set, rather than to a single fixed distribution. Predictions that are already confident enough (probability mass on the target above1 - alpha) incur zero loss.- Parameters:
alpha (float) – Relaxation strength in
[0, 1);0recovers ordinary cross-entropy-style behavior (no slack), larger values tolerate more probability mass elsewhere before penalizing.
- alpha = 0.0
- gz_threshold = 0.1
- eps = 1e-14
- class dicee.losses.custom_losses.AdaptiveLabelRelaxationLoss(min_alpha=0.01, max_alpha=0.2, alpha_step=0.01, initial_alpha=0.1)[source]
Bases:
torch.nn.ModuleLabelRelaxationLosswith analphathat self-adjusts from batch to batch, selectable vialoss_fn="AdaptiveLabelRelaxationLoss".Computes the label-relaxation loss once under
torch.no_grad()with the currentalphato decide the adjustment (grow towardmax_alphaif the loss increased since the previous call, shrink towardmin_alphaif it decreased), then recomputes the loss with the updatedalphafor the actual gradient-carrying return value. LikeAdaptiveLabelSmoothingLoss, this module is stateful (prev_loss,alphapersist across calls) - use a fresh instance per training run.- Parameters:
min_alpha (float) – Lower bound the adaptive
alphacan decay to.max_alpha (float) – Upper bound the adaptive
alphacan grow to.alpha_step (float) – Adjustment applied to
alphaafter each call.initial_alpha (float) – Starting value before any adjustment has occurred.
- min_alpha = 0.01
- max_alpha = 0.2
- alpha_step = 0.01
- alpha = 0.1
- prev_loss = None
- eps = 1e-14
- gz_threshold = 0.1
- class dicee.losses.custom_losses.ConfidenceBasedAdaptiveLabelRelaxationLoss(alpha=0.1)[source]
Bases:
torch.nn.ModuleLabelRelaxationLosswherealphashrinks as the model’s own average prediction confidence grows, selectable vialoss_fn="ConfidenceBasedAdaptiveLabelRelaxationLoss".Each call rescales
alphaby(1 - mean(pred))before computing the loss. UnlikeAdaptiveLabelRelaxationLoss, this adjustment is one-directional and unbounded below:alphaonly ever decays toward 0 as confidence rises, with no mechanism to grow back if confidence later drops.current_epochis accepted for interface consistency with the otherloss_fnoptions but is currently unused. Stateful (alphapersists and mutates across calls) - use a fresh instance per training run.- Parameters:
alpha (float) – Initial relaxation strength before any confidence-based decay.
- alpha = 0.1
- gz_threshold = 0.1
- eps = 1e-14
- class dicee.losses.custom_losses.CombinedLSandLR(smoothness_ratio=0.0, alpha=0.0)[source]
Bases:
torch.nn.ModuleSwitches from
LabelSmoothingLosstoLabelRelaxationLossafter epoch 20, selectable vialoss_fn="CombinedLSandLR".Intended to smooth early training then relax the target distribution later on.
- Parameters:
smoothness_ratio (float) – Forwarded to
LabelSmoothingLossfor the first 20 epochs.alpha (float) – Forwarded to
LabelRelaxationLossfrom epoch 20 onward.
- smoothness_ratio = 0.0
- alpha = 0.0
- class dicee.losses.custom_losses.CombinedAdaptiveLSandAdaptiveLR[source]
Bases:
torch.nn.ModuleSwitches from
AdaptiveLabelSmoothingLosstoAdaptiveLabelRelaxationLossafter epoch 100, selectable vialoss_fn="CombinedAdaptiveLSandAdaptiveLR".Both inner losses use their default hyperparameters; there is currently no way to configure them through this wrapper.
- adaptive_label_smoothing
- adaptive_label_relaxation
- criterion = ''
- class dicee.losses.custom_losses.AggregatedLSandLR(smoothness_ratio=0.1, alpha=0.1)[source]
Bases:
torch.nn.ModuleFixed 0.4/0.6 weighted average of
LabelSmoothingLossandLabelRelaxationLoss, selectable vialoss_fn="AggregatedLSandLR".Unlike
CombinedLSandLR, both terms are computed every call and blended (0.4 * smoothing + 0.6 * relaxation) rather than switched between by epoch.- Parameters:
smoothness_ratio (float) – Forwarded to the inner
LabelSmoothingLoss.alpha (float) – Forwarded to the inner
LabelRelaxationLoss.
- smoothness_ratio = 0.1
- alpha = 0.1
- class dicee.losses.custom_losses.ACLS(pos_lambda: float = 1.0, neg_lambda: float = 0.1, alpha: float = 0.1, margin: float = 10.0, num_classes: int = 200, ignore_index: int = -100)[source]
Bases:
torch.nn.ModuleAdaptive and Conditional Label Smoothing-style calibration loss, selectable via
loss_fn="ACLS".Adds a margin-based confidence regularizer to cross-entropy: for the predicted (argmax) class, logits above
marginare penalized quadratically (discourages over-confidence on the top class), and for every other class, logits withinmarginof the top logit are also penalized quadratically (discourages under-separation from the runner-up classes). The two penalties are weighted bypos_lambda/neg_lambdaand added toCrossEntropyLossscaled byalpha.Warning
nn.CrossEntropyLossexpectstargetsas class indices (or a proper probability distribution per row); KGE’sKvsAlllabels are multi-hot (possibly several true tails per row), which is not an equivalent input.num_classesandignore_indexare currently unused inforward/get_reg.- Parameters:
pos_lambda (float) – Weight of the over-confidence penalty on the predicted class.
neg_lambda (float) – Weight of the under-separation penalty on the other classes.
alpha (float) – Weight of the regularization term relative to cross-entropy.
margin (float) – Logit margin used by both penalty terms.
num_classes (int) – Currently unused.
ignore_index (int) – Currently unused.
- pos_lambda = 1.0
- neg_lambda = 0.1
- alpha = 0.1
- margin = 10.0
- num_classes = 200
- ignore_index = -100
- cross_entropy
- property names