Fix cross entropy loss compute() - #424
Closed
andrewdalpino wants to merge 2 commits into
Closed
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to correct the CrossEntropy::compute() loss calculation to include the missing negative-class term so that loss values properly reflect errors for target=0 samples.
Changes:
- Update
CrossEntropy::compute()to use a clipped output and include the(1−t)·log(1−p)term. - Update expected loss values in
CrossEntropyTestto match the new computation. - Document the fix in the changelog.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/NeuralNet/CostFunctions/CrossEntropy.php | Changes the cross-entropy computation formula and adds clipping for numerical stability. |
| tests/NeuralNet/CostFunctions/CrossEntropyTest.php | Updates expected loss outputs for the revised compute() behavior. |
| CHANGELOG.md | Notes the cross-entropy compute fix in the 2.5.5 section. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
89
to
101
| @@ -97,7 +97,7 @@ public function computeProvider() : Generator | |||
| [0.0, 1.0, 0.0], | |||
| [0.0, 0.0, 1.0], | |||
| ]), | |||
| 0.10809567592917217, | |||
| 0.20764012617655228, | |||
| ]; | |||
Comment on lines
+36
to
+43
| $output = $output->clip(EPSILON, 1.0 - EPSILON); | ||
|
|
||
| return $target->negate()->multiply($entropy)->mean()->mean(); | ||
| $ones = Matrix::ones(...$output->shape()); | ||
|
|
||
| $entropy = $output->log()->multiply($target) | ||
| ->add($ones->subtract($output)->log()->multiply($ones->subtract($target))); | ||
|
|
||
| return $entropy->negate()->mean()->mean(); |
apphp
approved these changes
Aug 16, 2026
Member
Author
|
Going to close this one @apphp because Co Pilot is correct, this will effect how cross entropy is calculated for multiclass (non-binary) classification problems as well. Perhaps we should consider implementing both Binary and Multiclass cross entropy loss functions in 3.0. Thoughts? |
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.
CrossEntropy compute() missing the (1−t)log(1−p) term — src/NeuralNet/CostFunctions/CrossEntropy.php:34-39. Loss is always 0 for target=0 samples (gradient differentiate is correct BCE, so they disagree). Verified.