mirror of
https://github.com/harvard-edge/cs249r_book.git
synced 2026-07-16 06:07:17 -05:00
[PR #1870] fix(activations): numerically stable sigmoid + remove overflow warning suppressor #29148
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/harvard-edge/cs249r_book/pull/1870
Author: @Shashank-Tripathi-07
Created: 6/16/2026
Status: 🔄 Open
Base:
main← Head:fix/sigmoid-numerical-stability📝 Commits (1)
70d9988fix sigmoid overflow and remove the warning suppressor that hid it📊 Changes
1 file changed (+10 additions, -12 deletions)
View changed files
📝
tinytorch/src/02_activations/02_activations.py(+10 -12)📄 Description
What breaks
Sigmoid.forward()uses the naive form:For very negative inputs (
x << -87in float32),np.exp(-x)overflows toinf. The result1/(1+inf) = 0.0is accidentally correct, but NumPy emits aRuntimeWarning. The unit test hid this with:This suppressor silences the whole
RuntimeWarningcategory for any future code in that block -- meaning a real numerical regression would produce no signal.Fix
Use the piece-wise stable sigmoid identity:
Both branches compute the same mathematical function but stay in the safe exponent range. This is the same trick used by
scipy.special.expitand PyTorch'storch.sigmoid.With overflow eliminated: remove the
warnings.filterwarningscalls and the now-unusedimport warnings.Test plan
pytest tinytorch/tests/02_activations/passes with no RuntimeWarningsigmoid([-1000, 1000])returns[~0, ~1]without any warningsigmoid([0])still returns0.5🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.