Upload alea/losses.py with huggingface_hub
Browse files- alea/losses.py +64 -0
alea/losses.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Proper scoring rules for decision heads.
|
| 2 |
+
|
| 3 |
+
All losses accept a target distribution q (soft, [B, K]) — for deterministic
|
| 4 |
+
labels q is one-hot. Proper scoring rules are minimized at p = q either way;
|
| 5 |
+
soft targets just buy lower variance.
|
| 6 |
+
"""
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn.functional as F
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def brier_loss(logits, q, pad_mask=None):
|
| 12 |
+
"""Mean over classes of (p - q)^2. logits [B,K], q [B,K], pad True=ignore."""
|
| 13 |
+
p = F.softmax(logits.float(), dim=-1)
|
| 14 |
+
loss = ((p - q.float()) ** 2).sum(-1)
|
| 15 |
+
return loss.mean()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def ce_loss(logits, q, pad_mask=None):
|
| 19 |
+
"""Cross-entropy against soft targets."""
|
| 20 |
+
logp = F.log_softmax(logits.float(), dim=-1)
|
| 21 |
+
return -(q.float() * logp).sum(-1).mean()
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def bernoulli_brier(logit, q):
|
| 25 |
+
"""logit [B] for P(yes); q [B] in [0,1] target probability."""
|
| 26 |
+
p = torch.sigmoid(logit.float())
|
| 27 |
+
return ((p - q.float()) ** 2).mean()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def crps_loss(level_logits, q, pad_mask=None):
|
| 31 |
+
"""CRPS over ordered levels: sum over thresholds of (F_p - F_q)^2.
|
| 32 |
+
|
| 33 |
+
level_logits [B, L] over L ordered levels; q [B, L] target distribution
|
| 34 |
+
(one-hot for a known level). Respects ordinality — mass far from the truth
|
| 35 |
+
costs more than mass near it.
|
| 36 |
+
"""
|
| 37 |
+
p = F.softmax(level_logits.float(), dim=-1)
|
| 38 |
+
Fp = p.cumsum(-1)
|
| 39 |
+
Fq = q.float().cumsum(-1)
|
| 40 |
+
return ((Fp - Fq) ** 2)[:, :-1].sum(-1).mean() # last CDF entry is always 1
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def choice_loss(logits, q, w_brier=1.0, w_ce=0.1):
|
| 44 |
+
return w_brier * brier_loss(logits, q) + w_ce * ce_loss(logits, q)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def _slog(x):
|
| 48 |
+
"""Signed log1p: monotone, ~identity near 0, log-scale for big |x|."""
|
| 49 |
+
return torch.sign(x) * torch.log1p(x.abs())
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def pinball_loss(pred_quantiles, target_quantiles, taus):
|
| 53 |
+
"""Pinball in signed-log space, normalized by mean|slog(target)|.
|
| 54 |
+
|
| 55 |
+
Quantiles commute with monotone transforms, so minimizing
|
| 56 |
+
pinball(slog(p), slog(t)) still gives p -> t in real space, but errors
|
| 57 |
+
on $1e5-scale targets get proportional gradients instead of being
|
| 58 |
+
normalized away (or dominating) the loss."""
|
| 59 |
+
t = torch.tensor(taus, device=pred_quantiles.device).float()
|
| 60 |
+
p = _slog(pred_quantiles.float())
|
| 61 |
+
y = _slog(target_quantiles.float())
|
| 62 |
+
diff = y - p
|
| 63 |
+
scale = y.abs().mean(dim=-1, keepdim=True).clamp_min(1e-3)
|
| 64 |
+
return (torch.maximum(t * diff, (t - 1) * diff) / scale).mean()
|