Fix Multiclass layer gradient - #480
Open
andrewdalpino wants to merge 4 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The updated Multiclass::gradient() reduces along the wrong axis for the Softmax Jacobian-vector product, and the new test data currently allows that incorrect implementation to pass.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR removes the standalone Softmax activation function from the NeuralNet API and moves Softmax behavior into the Multiclass output layer, aiming to fix backpropagation for non-cross-entropy classification losses (e.g., Relative Entropy/KL divergence).
Changes:
- Removed
Softmaxactivation function (code, tests, benchmarks, and docs navigation/page). - Implemented Softmax computation inside
Multiclassand updated its gradient logic; added a new test covering a non-cross-entropy loss. - Performed small formatting/doc touch-ups (initializers, docs, changelog) to reflect the removal.
File summaries
| File | Description |
|---|---|
| tests/NeuralNet/Layers/MulticlassTest.php | Adds a new gradient test intended to cover Softmax-Jacobian behavior with RelativeEntropy. |
| tests/NeuralNet/ActivationFunctions/SoftmaxTest.php | Removes unit tests for the deleted Softmax activation function. |
| src/NeuralNet/Layers/Multiclass.php | Removes dependency on Softmax activation class, adds internal Softmax, and updates gradient computation. |
| src/NeuralNet/Initializers/Xavier1.php | Updates doc text to remove Softmax mention; minor formatting. |
| src/NeuralNet/Initializers/Uniform.php | Minor formatting. |
| src/NeuralNet/Initializers/Normal.php | Minor formatting. |
| src/NeuralNet/Initializers/LeCun.php | Minor formatting. |
| src/NeuralNet/Initializers/He.php | Minor formatting. |
| src/NeuralNet/ActivationFunctions/Softmax.php | Deletes the Softmax activation function implementation. |
| mkdocs.yml | Removes Softmax activation function from docs nav. |
| docs/neural-network/initializers/xavier-1.md | Removes Softmax link/mention in Xavier 1 docs. |
| docs/neural-network/activation-functions/softmax.md | Removes the Softmax activation function docs page. |
| docs/classifiers/softmax-classifier.md | Removes the link to the deleted Softmax activation docs page. |
| CHANGELOG.md | Notes Softmax removal and Multiclass gradient fix. |
| benchmarks/NeuralNet/ActivationFunctions/SoftmaxBench.php | Removes benchmarks for the deleted Softmax activation function. |
Review details
- Files reviewed: 15/15 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Softmax activation function is incompatible with any non-Cross Entropy-based loss function. Remove Softmax activation function. Subsume functionality into Multiclass layer - correct the gradient computation for non-Cross Entropy-based loss function such as Relative Entropy.