kirad.losses¶
kirad.losses
¶
CORAL ordinal-regression loss (stage: loss).
Turns one score per sample into K ordered "is y > k?" binary problems with learned biases b_k. Instantiated by OrdinalRegressionPLModule in kirad/base_models.py.
CORALLoss
¶
Bases: LightningModule
CORAL loss: score + learned per-cutoff biases, BCE against the thermometer code of y.
Implements the CORAL loss function for ordinal regression introduced in https://arxiv.org/abs/1901.07884.
Source code in kirad/losses.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
weights = weights or [1.0 for _ in range(num_classes - 1)]
instance-attribute
¶
biases = torch.nn.Parameter(torch.zeros(len(self.weights), dtype=torch.float))
instance-attribute
¶
loss = torch.nn.BCEWithLogitsLoss(reduction='none')
instance-attribute
¶
__init__(num_classes, weights=None, *args, **kwargs)
¶
One learned bias per cutoff (num_classes - 1, e.g. 27 for PHQ-9) and a 0/1 weight each.
weights come from loss_target_cutoffs (all 1.0 for "exact28"/"exact22"); default all 1.0.
Source code in kirad/losses.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
forward(x, y)
¶
Weighted sum of BCE over cutoffs: sigmoid(s + b_k) vs [y > k] for each cutoff k.
Shapes: x [N, 1] score, y [N] integer label -> [N] loss per sample (not averaged). Note: sum over cutoffs, not mean; the caller averages over samples.
Source code in kirad/losses.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |