Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multilabel binary cross entropy #1571

Merged
merged 2 commits into from
Apr 3, 2024
Merged

Support multilabel binary cross entropy #1571

merged 2 commits into from
Apr 3, 2024

Conversation

laggui
Copy link
Member

@laggui laggui commented Apr 2, 2024

The current implementation of BinaryCrossEntropyLoss only supports the binary case for 1-D inputs and targets. This PR extends the current definition to support Tensor<B, D>.

For multi-label classification the typical shape is [batch_size, num_classes] and targets are provided as multi-hot encoded targets.

Checklist

  • Confirmed that run-checks all script has been executed.

Changes

Added support for multilabel BCE loss for input Tensor<B, D>

  • Changed implementation to use log_sigmoid(x) instead of log(sigmoid(x)) with logits (numerical stability)
  • Update label smoothing for multiclass (N > 2)
  • Update loss class weighting to support multilabel case and Tensor<B, D>
  • Fixed unit tests to use pre-computed values to compare against instead of comparing the function results with random values

⚠️ Breaking
Changed the default config to use BinaryCrossEntropyLoss without logits (expects probabilities, not raw predictions/logits). I think this makes more sense as a default and allows to use the config to build the loss with something more natural:

BinaryCrossEntropyLossConfig::new().with_logits(true)

This is a bit of an opiniated change, could be reverted if really desired.

Testing

Added unit tests for new multi-label case and updated the binary 1-D tests.

Copy link

codecov bot commented Apr 2, 2024

Codecov Report

Attention: Patch coverage is 99.50739% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 86.53%. Comparing base (b0c5986) to head (b498d6c).

Files Patch % Lines
...ates/burn-core/src/nn/loss/binary_cross_entropy.rs 99.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1571      +/-   ##
==========================================
+ Coverage   86.48%   86.53%   +0.05%     
==========================================
  Files         684      684              
  Lines       78093    78248     +155     
==========================================
+ Hits        67535    67713     +178     
+ Misses      10558    10535      -23     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@louisfd louisfd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@antimora antimora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@laggui laggui merged commit 0978c8a into main Apr 3, 2024
15 checks passed
@laggui laggui deleted the feat/bce-multilabel branch April 3, 2024 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants