PAUCAtBudgetLoss¶
Differentiable partial-AUC-over-an-FPR-band loss with an optional memory queue. Optimizes the normalized partial AUC over a false-positive-rate band [alpha, beta] that brackets a target operating point (e.g. FPR ≈ 0.005 / 50 bps), rather than the full AUC or a single-threshold recall. Useful when you care about recall at a fixed, low false-alarm budget (fraud, screening, alerting).
imbalanced_losses.pauc_loss.PAUCAtBudgetLoss
¶
Bases: _QueuedRankingLoss
Differentiable partial-AUC-over-an-FPR-band loss with an optional memory queue.
For each class, FPR-band edges t_alpha/t_beta are estimated from the
pooled iid negatives (stop-gradient) as score quantiles, a scale-aware
sigmoid temperature tau_eff is derived from a detached robust dispersion
of those negatives, and the normalized partial AUC over [alpha, beta] is
optimized via a trapezoid (default) or band-restricted pairwise surrogate.
Loss is 1 - pauc.
Multi-class: one-vs-rest per class using logits[:, c], then reduce. Binary: logits[:, 0] with targets in {0, 1}.
Inherits queue management, DDP gather, ignore-index filtering, subsampling,
and reduction logic from _QueuedRankingLoss.
Degenerate-dispersion guard: if the robust dispersion of iid-negative
scores for a class is at or below _SCALE_EPS (all-equal scores, a
collapsed band with tau_scale='band', or too few iid negatives to
resolve the tail quantile), that class is marked INVALID and excluded from
reduction rather than silently computing with a near-zero temperature. A
one-time UserWarning is emitted on the first such occurrence. To avoid
degenerate classes, increase queue_size or ensure iid negatives cover a
meaningful score range.
The band-edge approximation of population FPR is reliable when the pooled
iid-negative count substantially exceeds 1/alpha. Monitor
band_neg_count in diagnostics and set queue_size accordingly.
The recommended band convention is alpha ≈ 0, beta ≈ budget (where
budget is the target FPR, e.g. beta=0.005 for a 50 bps operating
point). This sets the upper threshold t_alpha = quantile(neg, 1.0) =
max(neg) and the lower threshold t_beta = quantile(neg, 1 - budget),
so the band covers every false-positive that falls above the budget
threshold, i.e. all negatives scoring at or above the operating point.
The older convention [budget/2, 1.5·budget] — e.g. [0.0025, 0.0075]
for a 50 bps point — excludes the highest-scoring (worst) negatives via its
lower edge alpha = budget/2 and extends below the operating threshold via
its upper edge beta = 1.5·budget. A band sweep (8 seeds, synthetic
contested-top extreme-imbalance data, 50 bps budget) found coverage@budget
to be monotone in both edges: smaller alpha and smaller beta are
better in every cell, and the old convention sits in the poorly-performing
high-alpha region (the worst cell being alpha=budget/2, beta=2.5*budget).
The recommended alpha=0, beta=budget band fixes both defects by
contrasting positives against all false-positives at the budget.
Caveats: the sweep evidence is synthetic, at a single budget (50 bps),
and in the contested-top regime. The improvement is concentrated at
pos_rate ≪ budget; once pos_rate ≥ budget, coverage@budget is
mechanically capped at budget/pos_rate and band choice is irrelevant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes
|
int
|
Number of output classes. Use 1 for binary mode. |
required |
alpha
|
float
|
Lower FPR band edge. Must satisfy |
0.0
|
beta
|
float
|
Upper FPR band edge. Must satisfy |
0.005
|
surrogate
|
('trapezoid', 'pairwise')
|
pAUC estimator. |
'trapezoid'
|
n_knots
|
int
|
Number of equally-spaced FPR knots in
The default of 2 (trapezoid rule) is accurate for narrow bands where
TPR(FPR) is approximately linear over |
2
|
tau_scale
|
('iqr', 'band')
|
Robust dispersion used to make the temperature scale-aware.
|
'iqr'
|
pos_numerator
|
('pool', 'live')
|
Which positives form the soft-TPR numerator (and the pairwise positive
set). |
'pool'
|
queue_size
|
int
|
Circular buffer size (rows). Larger queues stabilise the quantile-based band edges -- at low FPR you need many negatives for a meaningful tail quantile. Set to 0 to disable. Default: 1024. DDP note: when |
1024
|
temperature
|
float
|
Dimensionless multiplier on |
0.1
|
reduction
|
('mean', 'sum', 'none')
|
How to aggregate per-class losses. - 'mean': scalar average over valid classes. - 'sum': scalar sum over valid classes. - 'none': tensor of shape [C]; invalid classes are nan. Default: 'mean'. |
'mean'
|
ignore_index
|
int
|
Target value marking padded positions. Excluded from threshold estimation and the positive set. Default: -100. |
-100
|
update_queue_in_eval
|
bool
|
If False (default), the queue is frozen during eval mode. Default: False. |
False
|
gather_distributed
|
bool or None
|
Whether to all-gather logits, targets, and the iid mask across DDP
workers before computing the loss. |
None
|
quantile_interpolation
|
str
|
Interpolation method passed to torch.quantile for the band edges. 'higher' is the conservative default. One of ('linear', 'lower', 'higher', 'nearest', 'midpoint'). Default: 'higher'. |
'higher'
|
max_pool_size
|
int or None
|
Maximum number of rows in the ranking pool (live batch + queue after
ignore_index filtering). When exceeded, minimum-quota subsampling caps
it. See |
None
|
Examples:
>>> loss_fn = PAUCAtBudgetLoss(num_classes=4, alpha=0.0, beta=0.005)
>>> logits = torch.randn(256, 4)
>>> targets = torch.randint(0, 4, (256,))
>>> loss = loss_fn(logits, targets)
>>> loss.backward()
Notes
The iid-negative band edges depend only on rows flagged
iid_mask=True; appending non-iid negatives (caller-side densification)
does not shift t_alpha/t_beta. iid_mask=None treats all rows as
iid (the common case when negatives are never densified by class).
Trapezoid cost is O(|P| x n_knots); pairwise cost is O(|P| x |band|). No O(M^2) path.
References
This loss is an original design, not a published method, but the partial-AUC-over-a-band objective and its estimators build on prior work:
.. [1] D. K. McClish (1989). "Analyzing a Portion of the ROC Curve." Medical Decision Making 9(3), 190-195. -- partial AUC over an ROC region. .. [2] L. E. Dodd and M. S. Pepe (2003). "Partial AUC Estimation and Regression." Biometrics 59(3), 614-623. -- nonparametric pAUC estimator (the consistent plug-in the trapezoid surrogate relates to). .. [3] H. Narasimhan and S. Agarwal (2013). "A Structural SVM Based Approach for Optimizing Partial AUC." ICML 2013. -- optimizing pAUC over an FPR band [alpha, beta] as a learning objective (the KDD 2013 "tight" variant gives the boundary-corrected estimator). .. [4] D. Zhu, G. Li, B. Wang, X. Wu, and T. Yang (2022). "When AUC meets DRO: Optimizing Partial AUC for Deep Learning with Non-Convex Convergence Guarantee." ICML 2022. -- deep one-/two-way pAUC surrogate optimization.
Source code in src/imbalanced_losses/pauc_loss.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
forward(logits, targets, iid_mask=None, return_per_class=False, return_diagnostics=False)
¶
Compute the pAUC loss, optionally returning per-class diagnostics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
(Tensor, shape[N, C])
|
Raw (un-normalised) class scores. |
required |
targets
|
(Tensor, shape[N])
|
Integer class labels. Positions equal to |
required |
iid_mask
|
torch.Tensor, shape [N], dtype=bool
|
Per-row iid-eligibility flag. |
None
|
return_per_class
|
bool
|
If True, also return per-class losses and a validity mask. |
False
|
return_diagnostics
|
bool
|
If True, also return a Keys:
When |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
loss |
Tensor
|
Scalar or shape |
per_class_loss |
(Tensor, shape[C])
|
Only when |
valid_classes |
torch.Tensor, shape [C], dtype=bool
|
Only when |
stats |
dict[str, Tensor[C]]
|
Only when |
Notes
self._last_diag is transient per-call internal state; it is
reset at the top of every forward call. Statefulness here is a
deliberate tradeoff to avoid changing the shared _QueuedRankingLoss
base-class contract (which cannot accept extra return values from
_compute_per_class).
Source code in src/imbalanced_losses/pauc_loss.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
Quick examples¶
Optimize pAUC around a 50 bps operating point¶
from imbalanced_losses import PAUCAtBudgetLoss
import torch
# Recommended band: alpha=0, beta=budget.
# t_alpha = max(neg_iid), t_beta = quantile at the budget threshold.
# Contrasts positives against every false-positive above the operating cutoff.
loss_fn = PAUCAtBudgetLoss(num_classes=4, alpha=0.0, beta=0.005, queue_size=1024)
logits = torch.randn(256, 4)
targets = torch.randint(0, 4, (256,))
loss = loss_fn(logits, targets)
loss.backward()
Binary classification¶
loss_fn = PAUCAtBudgetLoss(num_classes=1, alpha=0.0, beta=0.005, queue_size=1024)
logits = torch.randn(256, 1)
targets = torch.randint(0, 2, (256,))
loss = loss_fn(logits, targets)
Per-class logging¶
loss, per_class, valid = loss_fn(logits, targets, return_per_class=True)
loss.backward()
for c in valid.nonzero(as_tuple=True)[0].tolist():
print(f"Class {c} pAUC-loss: {per_class[c].item():.4f}")
Diagnostics — detect band starvation¶
loss, stats = loss_fn(logits, targets, return_diagnostics=True)
# stats: per-class [C] tensors
print(stats["band_neg_count"]) # iid negatives landing in the band
print(stats["grad_pos_count"]) # live positives carrying gradient (rank-local)
print(stats["t_alpha"], stats["t_beta"], stats["tau_eff"], stats["pauc_var"])
If grad_pos_count sits near 1 and pauc_var wanders, the band is starved of gradient signal — increase the effective batch (DDP all-gather) or densify positives upstream.
Marking densified negatives (advanced)¶
If a caller densifies negatives by class (e.g. hard-negative mining), pass iid_mask so the FPR band edges are still estimated from an iid sample and beta keeps meaning population FPR:
# iid_mask[i] = True for rows drawn iid; False for injected/densified rows.
loss = loss_fn(logits, targets, iid_mask=iid_mask)
When iid_mask=None (the default) every negative is treated as iid — correct for any pipeline that never densifies negatives by class.
Parameter guidance¶
| Parameter | Default | Notes |
|---|---|---|
num_classes |
required | Use 1 for binary |
alpha |
0.0 |
Lower FPR band edge; 0 <= alpha < beta <= 1. alpha=0 sets t_alpha=max(neg_iid), including all top negatives. |
beta |
0.005 |
Upper FPR band edge; set to your target operating-point FPR (e.g. 0.005 for 50 bps). |
surrogate |
"trapezoid" |
"trapezoid" integrates soft-TPR over the band (gradient through positives only); "pairwise" compares positives vs band negatives (band negatives carry gradient) — for wide/volatile bands |
n_knots |
2 |
Trapezoid FPR knots; 2 is accurate for narrow bands, >= 3 for wide bands |
tau_scale |
"iqr" |
Scale used for the scale-aware temperature: "iqr" (stable bulk statistic; pair with small temperature) or "band" (sized to the operating region; pair with temperature near 1.0) |
pos_numerator |
"pool" |
Positives in the soft-TPR numerator (and the pairwise positive set): "pool" (all pooled positives) or "live" (live-batch only). "live" gives an undiluted gradient when the queue swamps the few live positives at extreme imbalance — most beneficial for "trapezoid"; "pairwise" usually prefers "pool" to keep the positive×band-negative contrast populated |
temperature |
0.1 |
Dimensionless multiplier on tau_eff = temperature * scale — not raw logits. Larger = smoother/biased-to-0.5; smaller = sharper but risks saturation |
queue_size |
1024 |
Larger queues stabilise the tail quantile; at low FPR you need many pooled negatives |
reduction |
"mean" |
"none" returns [C]; invalid classes are nan |
ignore_index |
-100 |
Excluded from threshold estimation and the positive set |
quantile_interpolation |
"higher" |
Conservative default for the band edges |
max_pool_size |
None |
Minimum-quota subsampling cap for very large pools (seq2seq) |
Band selection guidance¶
Recommended convention: alpha ≈ 0, beta ≈ budget. For a 50 bps (FPR = 0.005) operating point, use alpha=0.0, beta=0.005. This sets t_alpha = max(neg_iid) and t_beta = quantile(neg_iid, 1 - budget), so the band covers every false-positive that scores above the operating threshold.
The older convention [budget/2, 1.5·budget] (e.g. [0.0025, 0.0075] for 50 bps) has two defects: its lower edge alpha = budget/2 excludes the highest-scoring (worst) negatives from the contrast, and its upper edge beta = 1.5·budget extends below the budget threshold into negatives that don't matter for coverage. A band sweep on synthetic contested-top extreme-imbalance data found coverage@budget to be monotone in both edges (smaller is better), with the old convention in the poorly-performing high-alpha region (the single worst cell is alpha=budget/2, beta=2.5·budget), well below the alpha≈0, beta≈budget optimum.
Caveat: this evidence is synthetic, at a single budget (50 bps), in the contested-top regime. The improvement is concentrated at pos_rate ≪ budget. Once pos_rate ≥ budget, coverage@budget is mechanically capped at budget/pos_rate and band choice is irrelevant.
The band edges are estimated as score quantiles of the iid negatives and approximate population FPR only when the pooled iid-negative count is adequate. With alpha=0, the upper threshold is always the maximum negative score (no tail-quantile bias for t_alpha); only t_beta requires enough negatives to resolve quantile(neg, 1 - beta). Use queue_size to accumulate enough negatives, and the band_neg_count diagnostic as the empirical check. A class whose iid-negative score dispersion is degenerate (≈ 0) is skipped (marked invalid) with a one-time warning.
Choosing among the ranking losses¶
PAUCAtBudgetLoss sits between the two existing ranking losses on the ROC:
SmoothAPLossoptimizes the whole precision–recall curve (Average Precision).PAUCAtBudgetLossoptimizes a band of the ROC around your operating point.RecallAtQuantileLossoptimizes recall at a single score threshold.
Reach for PAUCAtBudgetLoss when your business constraint is a fixed false-alarm budget (a region, not the whole curve and not one hard point).