Skip to content

fix: stabilize Softmax for large finite inputs - #2031

Open
lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/stable-softmax
Open

lanqing431 wants to merge 1 commit into
TheAlgorithms:masterfrom
lanqing431:fix/stable-softmax

Conversation

@lanqing431

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Softmax([1000, 1000]) currently returns [NaN, NaN]: both exponentials overflow. Large negative finite inputs similarly underflow to zero, producing 0 / 0.

Subtract the maximum input before exponentiation, then reuse those exponentials for normalization. A common shift leaves the mathematical distribution unchanged. Regression tests check finite probabilities, normalization and invariance under shifts of +1000 and -1000.

Validation: the two new regression cases fail on unchanged master. npm test -- --maxWorkers=2 --minWorkers=1 passes all 11972 tests in 346 files. npm run check-style and git diff --check pass (Node.js 24.22.0).

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is my own contribution and does not copy another proposed fix.
  • This PR only changes one algorithm and its tests.
  • All files remain in their existing directories.

Prepared with OpenAI Codex assistance.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant