Skip to content

Fix Swish layer - #425

Open
andrewdalpino wants to merge 3 commits into
masterfrom
fix-swish
Open

Fix Swish layer#425
andrewdalpino wants to merge 3 commits into
masterfrom
fix-swish

Conversation

@andrewdalpino

Copy link
Copy Markdown
Member

Swish layer: missing β in input derivative, wrong dBeta, 0/0 NaN — src/NeuralNet/Layers/Swish.php:176, 260-271. dBeta = Σ dOut·x is not Σ dOut·x²·σ(βx)(1−σ(βx)).

@andrewdalpino
andrewdalpino requested review from a team and a lite review from Copilot August 16, 2026 02:36
@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 fixes the Swish activation layer’s backprop math by correcting the β gradient and the input derivative, eliminating a potential 0/0 path that could produce NaNs. It also updates tests and the changelog to reflect/cover the corrected behavior.

Changes:

  • Correct Swish backprop: compute dβ as Σ(dOut · x² · σ(βx)(1−σ(βx))) and fix dInput to include β while avoiding 0/0.
  • Extend/adjust Swish unit tests (including coverage for β > 1) and update expected numeric results.
  • Update MLP regressor test to include a Swish layer, and document the fix in the changelog.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/NeuralNet/Layers/Swish.php Fixes Swish derivatives/gradients and removes the 0/0 derivative formulation by using σ(βx) directly.
tests/NeuralNet/Layers/SwishTest.php Updates expected values and adds a β>1 regression test for forward/back/infer correctness.
tests/Regressors/MLPRegressorTest.php Swaps one SiLU activation for a Swish layer to exercise the layer in an end-to-end model.
CHANGELOG.md Notes the Swish beta-parameter gradient fix.
Suppressed comments (2)

tests/NeuralNet/Layers/SwishTest.php:159

  • This floating-point gradient assertion is using strict equality. Consider switching to assertEqualsWithDelta (as used in many other neural-net tests) to avoid flaky failures from small rounding differences.
        $this->assertInstanceOf(Matrix::class, $gradient);
        $this->assertEquals($expected, $gradient->asArray());

tests/NeuralNet/Layers/SwishTest.php:170

  • The infer output is asserted with strict float equality; using assertEqualsWithDelta here would make the test less sensitive to minute numeric differences.
        $this->assertInstanceOf(Matrix::class, $infer);
        $this->assertEquals($expected, $infer->asArray());

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/NeuralNet/Layers/SwishTest.php Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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