Skip to content

feat: add TripletMarginLoss#5175

Open
Bellman281 wants to merge 4 commits into
tracel-ai:mainfrom
Bellman281:loss/triplet-margin
Open

feat: add TripletMarginLoss#5175
Bellman281 wants to merge 4 commits into
tracel-ai:mainfrom
Bellman281:loss/triplet-margin

Conversation

@Bellman281

Copy link
Copy Markdown
Contributor

Checklist

  • Confirmed that cargo run-checks command has been executed.
  • Made sure the book is up to date with changes in this PR.

Related Issues/PRs

Part of the PyTorch loss-parity effort, following SoftMarginLoss (#5149),
MarginRankingLoss (#5151), and HingeEmbeddingLoss.

Changes

Adds TripletMarginLoss / TripletMarginLossConfig, matching torch.nn.TripletMarginLoss:
for each triplet, max(0, ||a − p||_p − ||a − n||_p + margin) (defaults margin = 1.0,
p = 2.0). Distances use burn::tensor::linalg::lp_norm, and the loss composes existing
ops so autodiff is free. Follows the MarginRankingLoss config/module pattern; added to the
book's loss table.

Testing

Known-answer test over forward_no_reduction, Mean, and Sum against PyTorch reference
values (3 triplets exercising a normal, a partial, and a clamped-to-zero case), plus a display
test. cargo run-checks passes.

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.71795% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 66.11%. Comparing base (12cba54) to head (400f6b5).
⚠️ Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
crates/burn-nn/src/loss/triplet_margin.rs 98.71% 1 Missing ⚠️

❌ Your project check has failed because the head coverage (66.11%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5175      +/-   ##
==========================================
+ Coverage   66.03%   66.11%   +0.07%     
==========================================
  Files        1206     1217      +11     
  Lines      183117   183581     +464     
==========================================
+ Hits       120927   121371     +444     
- Misses      62190    62210      +20     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants