Repository navigation
[ENH] Support probabilistic targets in CORNLoss (soft-CORN) - #160
Merged
Merged
Conversation
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
self-requested a review
October 19, 2026 06:15
Contributor
|
Thanks for the solid PR! To answer your notes:
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
approved these changes
Oct 19, 2026
Contributor
Author
|
Ah thanks! I have made the benchmark public :) |
Contributor
|
Thanks! |
Contributor
Author
|
Just for completeness, this work was done together with @DaniPulidoE :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CORNLossnow 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 indicator1[y > k]are replaced by their expectationsP(y >= k)andP(y > k)under the target distribution,P(y=j) = prod_{k<j} sigmoid(z_k) (1 - sigmoid(z_j)), so it is a proper scoring rule for the target distribution.y_trueof shape(N,)runs the same code as before. A 2-Dy_truetakes a new vectorised path,_soft_forward.P(y >= k)(not1 - cumsum) andlogsigmoid, 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.bibcite 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:{1, 2};ValueErrorfor wrong target or logit shapes.I also checked the new path against
coral_pytorch.losses.corn_losson 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,isortandblackas configured in.pre-commit-config.yamlare clean, and the Sphinx docs build renders the new math and the citation (the only docs warning is inGaussianUncertaintyLossWrapper.forward, which this PR does not touch).Notes
y_true.ndim) and I will change it.CHANGELOG.md/changelog_versions, which look like they are written at release time.🤖 Generated with Claude Code