[PR #1870] [CLOSED] fix(activations): numerically stable sigmoid + remove overflow warning suppressor #30970

Closed
opened 2026-06-21 20:36:28 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1870
Author: @Shashank-Tripathi-07
Created: 6/16/2026
Status: Closed

Base: mainHead: fix/sigmoid-numerical-stability


📝 Commits (1)

  • 70d9988 fix 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:

result = 1.0 / (1.0 + np.exp(-x.data))

For very negative inputs (x << -87 in float32), np.exp(-x) overflows to inf. The result 1/(1+inf) = 0.0 is accidentally correct, but NumPy emits a RuntimeWarning. The unit test hid this with:

warnings.filterwarnings('ignore', category=RuntimeWarning)

This suppressor silences the whole RuntimeWarning category for any future code in that block -- meaning a real numerical regression would produce no signal.

Fix

Use the piece-wise stable sigmoid identity:

result = np.where(
    x_data >= 0,
    1.0 / (1.0 + np.exp(-x_data)),    # exp(-x) in [0,1], safe
    np.exp(x_data) / (1.0 + np.exp(x_data))  # exp(x) in [0,1), safe
)

Both branches compute the same mathematical function but stay in the safe exponent range. This is the same trick used by scipy.special.expit and PyTorch's torch.sigmoid.

With overflow eliminated: remove the warnings.filterwarnings calls and the now-unused import warnings.

Test plan

  • pytest tinytorch/tests/02_activations/ passes with no RuntimeWarning
  • sigmoid([-1000, 1000]) returns [~0, ~1] without any warning
  • sigmoid([0]) still returns 0.5

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1870 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 6/16/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/sigmoid-numerical-stability` --- ### 📝 Commits (1) - [`70d9988`](https://github.com/harvard-edge/cs249r_book/commit/70d9988cefba0590e4985e28c52081325e2052c8) fix sigmoid overflow and remove the warning suppressor that hid it ### 📊 Changes **1 file changed** (+10 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/src/02_activations/02_activations.py` (+10 -12) </details> ### 📄 Description ## What breaks `Sigmoid.forward()` uses the naive form: ```python result = 1.0 / (1.0 + np.exp(-x.data)) ``` For very negative inputs (`x << -87` in float32), `np.exp(-x)` overflows to `inf`. The result `1/(1+inf) = 0.0` is accidentally correct, but NumPy emits a `RuntimeWarning`. The unit test hid this with: ```python warnings.filterwarnings('ignore', category=RuntimeWarning) ``` This suppressor silences the whole `RuntimeWarning` category for any future code in that block -- meaning a real numerical regression would produce no signal. ## Fix Use the piece-wise stable sigmoid identity: ```python result = np.where( x_data >= 0, 1.0 / (1.0 + np.exp(-x_data)), # exp(-x) in [0,1], safe np.exp(x_data) / (1.0 + np.exp(x_data)) # exp(x) in [0,1), safe ) ``` Both branches compute the same mathematical function but stay in the safe exponent range. This is the same trick used by `scipy.special.expit` and PyTorch's `torch.sigmoid`. With overflow eliminated: remove the `warnings.filterwarnings` calls and the now-unused `import warnings`. ## Test plan - [ ] `pytest tinytorch/tests/02_activations/` passes with no RuntimeWarning - [ ] `sigmoid([-1000, 1000])` returns `[~0, ~1]` without any warning - [ ] `sigmoid([0])` still returns `0.5` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-06-21 20:36:28 -05:00
GiteaMirror changed title from [PR #1870] fix(activations): numerically stable sigmoid + remove overflow warning suppressor to [PR #1870] [CLOSED] fix(activations): numerically stable sigmoid + remove overflow warning suppressor 2026-07-12 08:49:33 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#30970