Fix log_cosh_loss docstring to document the element-wise loss#3846
Open
winklemad wants to merge 1 commit into
Open
Fix log_cosh_loss docstring to document the element-wise loss#3846winklemad wants to merge 1 commit into
winklemad wants to merge 1 commit into
Conversation
Pablosinyores
approved these changes
Jul 16, 2026
Pablosinyores
left a comment
Contributor
There was a problem hiding this comment.
Confirmed against the source: loss = mx.logaddexp(errors, -errors) - math.log(2) is log(cosh(errors)) computed element-wise, and reduction defaults to none. The old (1/n) sum form baked in a mean that the default reduction doesn't apply, so it contradicted the actual output. The element-wise form now matches the code and is consistent with how huber_loss above documents its formula (reduction left to the Args). LGTM.
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.
Proposed changes
log_cosh_loss's docstring documents the loss as a mean-reduced scalar:but the function defaults to
reduction="none"and returns the per-elementlog(cosh(y_pred - y_true)). The(1/n) Σonly corresponds toreduction="mean", so at the default the documented formula doesn't match the output:It's also the only loss in
losses.pywhose math block bakes in the reduction —huber_loss,smooth_l1_loss,hinge_loss, and the others all document the element-wise loss and delegate aggregation to thereductionargument. This drops the(1/n) Σso the formula reads the element-wiselog(cosh(y_pred - y_true)), matching both the code and the rest of the file.Docs-only; no behavior change.
Checklist
Put an
xin the boxes that apply.pre-commit(black + isort) on the changed file