Skip to content

[ENH] Support probabilistic targets in CORNLoss (soft-CORN) - #160

Merged
victormvy merged 2 commits into
ayrna:mainfrom
M-Colley:enh/corn-probabilistic-targets
Oct 19, 2026
Merged

victormvy merged 2 commits into
ayrna:mainfrom
M-Colley:enh/corn-probabilistic-targets

Conversation

@M-Colley

Copy link
Copy Markdown
Contributor

Summary

CORNLoss now also accepts probabilistic targets: a (N, J) tensor with a distribution over the classes for every example, for instance a soft label, the uniform distribution over a set of acceptable classes, or the empirical distribution of several annotators. Until now its docstring said "CORN loss does not support probabilistic targets".

With such targets it is the soft-CORN loss: CORN's hard subset membership 1[y >= k] and hard indicator 1[y > k] are replaced by their expectations P(y >= k) and P(y > k) under the target distribution,

L = - sum_i sum_{k=0}^{J-2} [ P_i(y > k) log sigmoid(z_ik) + p_ik log(1 - sigmoid(z_ik)) ]
    / sum_i sum_{k=0}^{J-2} P_i(y >= k)
  • For one-hot targets it equals the existing loss exactly.
  • Up to the normalisation it is the cross-entropy between the target distribution and the class probabilities that the CORN logits define, P(y=j) = prod_{k<j} sigmoid(z_k) (1 - sigmoid(z_j)), so it is a proper scoring rule for the target distribution.
  • The hard-label path is not touched: y_true of shape (N,) runs the same code as before. A 2-D y_true takes a new vectorised path, _soft_forward.
  • It uses the suffix sums P(y >= k) (not 1 - cumsum) and logsigmoid, so it stays finite for extreme logits.

This is not a new loss (it is the continuation-ratio likelihood that CORN already optimises, applied to a soft target); the PR only adds support for probabilistic targets, which fits the soft labelling methods already in the library.

Benchmark

The docstring and docs/references.bib cite a benchmark of this loss against eleven others for learning from set-valued ordinal labels: https://github.com/M-Colley/soft-corn-benchmark (12 losses including soft cross-entropy, soft-CORAL / soft-OR, OLDL, partial-label likelihood and the hard-label CORN / CORAL / CE; five real datasets, synthetic families, fine-tuned backbones, and an in-vehicle study with natively set-valued answers). In short, on the frozen-feature benchmark soft-CORN is clearly better on likelihood than the hard-label losses, OLDL and partial-label likelihood, essentially tied with soft cross-entropy, and practically tied with soft-CORAL on likelihood while better on set-MAE and at larger training sizes; the report (REPORT.md) lists where it loses as well.

Tests

Added to dlordinal/losses/tests/test_corn_loss.py:

  • one-hot probabilistic targets give exactly the loss of the class indices;
  • the loss equals the CORN-pmf cross-entropy divided by the expected size of the conditional sets (independent reference);
  • a hand computation for a uniform-over-set target {1, 2};
  • zero loss for a point mass with saturated logits;
  • finite loss and gradients for extreme logits;
  • gradient descent on one example recovers the target distribution;
  • ValueError for wrong target or logit shapes.

I also checked the new path against coral_pytorch.losses.corn_loss on one-hot targets and against an independent implementation of soft-CORN on 60 random cases (maximum difference 5e-7 in float32).

pytest dlordinal/losses: 237 passed (the CUDA parametrisations are skipped on the CPU-only environment I used). ruff, isort and black as configured in .pre-commit-config.yaml are clean, and the Sphinx docs build renders the new math and the citation (the only docs warning is in GaussianUncertaintyLossWrapper.forward, which this PR does not touch).

Notes

  • The README asks contributors to contact the maintainers before doing any work; I went straight to a PR, so please say if you would rather see a different shape for this (a separate class or a flag instead of dispatching on y_true.ndim) and I will change it.
  • Probabilistic targets are not validated to be non-negative or to sum to one; the docstring states the requirement.
  • I did not touch CHANGELOG.md / changelog_versions, which look like they are written at release time.

🤖 Generated with Claude Code

CORNLoss now also accepts a (N, J) tensor of class probabilities as target, for example a soft
label, the uniform distribution over a set of acceptable classes, or the empirical distribution of
several annotators. The hard subset membership 1[y >= k] and indicator 1[y > k] of CORN's
conditional tasks are replaced by their expectations P(y >= k) and P(y > k) under the target
distribution (soft-CORN). It equals the hard CORN loss for one-hot targets, and up to the
normalisation it is the cross-entropy between the target distribution and the class probabilities
defined by the CORN logits. The hard-label path is unchanged.

Tests: one-hot equals hard targets, cross-entropy reference, hand computation for a uniform-over-set
target, saturated logits, finite gradients, recovery of the target distribution by gradient
descent, shape errors. The docstring cites a benchmark of the loss against other losses for
set-valued ordinal labels (https://github.com/M-Colley/soft-corn-benchmark), added to references.bib.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@victormvy

Copy link
Copy Markdown
Contributor

Thanks for the solid PR!

To answer your notes:

  • I think that dispatching on y_true.ndim is super clean and keeps the API straightforward.
  • Documenting the requirement in the docstring instead of adding runtime checks makes total sense for performance.
  • Regarding the changelog, no problem at all, we usually update that at release time anyway.

One quick thing: it looks like the benchmark repository linked in the docstring is currently set to private. Could you please make it public so users can access it?

I'll do a quick code review in the meantime, but overall this looks ready to go. Thanks again for the great contribution!

@victormvy victormvy added the enhancement New feature or request label Oct 19, 2026
@victormvy victormvy changed the title ENH: Support probabilistic targets in CORNLoss (soft-CORN) [ENH] Support probabilistic targets in CORNLoss (soft-CORN) Oct 19, 2026
@M-Colley

M-Colley commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Ah thanks! I have made the benchmark public :)

https://github.com/M-Colley/soft-corn-benchmark/

@victormvy
victormvy merged commit 4ecfae7 into ayrna:main Oct 19, 2026
39 checks passed
@victormvy

Copy link
Copy Markdown
Contributor

Thanks!

@M-Colley

Copy link
Copy Markdown
Contributor Author

Just for completeness, this work was done together with @DaniPulidoE :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants