Skip to content

Fix cross entropy loss compute() - #424

Closed
andrewdalpino wants to merge 2 commits into
masterfrom
fix-cross-entropy-loss
Closed

Fix cross entropy loss compute()#424
andrewdalpino wants to merge 2 commits into
masterfrom
fix-cross-entropy-loss

Conversation

@andrewdalpino

Copy link
Copy Markdown
Member

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.

@andrewdalpino
andrewdalpino requested review from a team and a lite review from Copilot August 16, 2026 02:19
@andrewdalpino andrewdalpino added the bug Something isn't working label Aug 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 CrossEntropyTest to 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();
@andrewdalpino

Copy link
Copy Markdown
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants