Repository navigation
Improve numerical stability of sigmoid function - #15769
Open
mehulikhanra904-prog wants to merge 4 commits into
Open
mehulikhanra904-prog wants to merge 4 commits into
mehulikhanra904-prog wants to merge 4 commits into
Conversation
for more information, see https://pre-commit.ci
Author
|
check |
jgohel9902
reviewed
Nov 18, 2026
Author
|
Ok
…On Wed, Nov 18, 2026, 06:19 Jenil Gohel ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In maths/sigmoid.py
<#15769 (comment)>
:
> """
- return 1 / (1 + np.exp(-vector))
+ try:
+ vector = np.asarray(vector, dtype=float)
+ except (TypeError, ValueError) as exc:
+ raise ValueError("sigmoid input must be numeric") from exc
+
+ if not np.all(np.isfinite(vector)):
The stable branches already handle infinities correctly: I checked that
sigmoid([inf, -inf]) gives [1., 0.], which is also what the old
implementation returned. Raising a ValueError for inf is therefore a
behaviour change that isn't needed for the stability fix, and could break
callers that pass inf. Could you drop the inf check (or only reject NaN,
which the formula would otherwise propagate)? If you keep it, please
mention it in the PR description and check other callers of sigmoid in the
repo.
------------------------------
In maths/sigmoid.py
<#15769 (comment)>
:
>
Examples:
>>> sigmoid(np.array([-1.0, 1.0, 2.0]))
array([0.26894142, 0.73105858, 0.88079708])
>>> sigmoid(np.array([0.0]))
array([0.5])
+
+ Large positive and negative values do not overflow:
Small clarification: the old code returned the correct value for -1000 and
1000 as well, but emitted a RuntimeWarning (overflow encountered in exp).
The real improvement is that this version returns the same values without
the warning. It might be worth saying that in the docstring or PR
description, since the current wording suggests the old result was wrong.
------------------------------
In maths/sigmoid.py
<#15769 (comment)>
:
>
-Script inspired from its corresponding Wikipedia article
+Script inspired from its corresponding Wikipedia article:
This line looks duplicated: once without the colon and once with the colon
plus the link. Could you keep only the second one?
—
Reply to this email directly, view it on GitHub
<#15769?email_source=notifications&email_token=BZMOKHAWLSQ4XDWYJ2PGC3D5ZTW2RA5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKOJUHEYDMOJYGY3KM4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#pullrequestreview-5949069866>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BZMOKHAKKT2KEU2PDTXUDZ35ZTW2RAVCNFSNUABEKJSXA33TNF2G64TZHM3DGNBXGYZTGNZ3JFZXG5LFHM3DINBYGQZDKMZVGCQXMAQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
mehulikhanra904-prog
left a comment
Author
There was a problem hiding this comment.
Thanks for pointing this out! I've removed the finite-value validation and now reject only NaN inputs. Infinite values are handled correctly by the stable branches, preserving the previous behavior. I've also added a doctest covering positive and negative infinity.
- Click Resolve comment.
This branch has not been deployed
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.
Describe your change
Changes
Checklist
No existing issue is being resolved.