From 1ddc39669b20cd83c6d60b5edd4c795fe4420785 Mon Sep 17 00:00:00 2001 From: MEHULI KHANRA Date: Tue, 17 Nov 2026 21:15:50 +0530 Subject: [PATCH 1/4] Improve numerical stability of sigmoid function --- maths/sigmoid.py | 57 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index cb45bde2702c..2a154ed316ff 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -1,10 +1,11 @@ """ This script demonstrates the implementation of the Sigmoid function. -The function takes a vector of K real numbers as input and then 1 / (1 + exp(-x)). -After through Sigmoid, the element of the vector mostly 0 between 1. or 1 between -1. +The sigmoid function maps real-valued inputs to the interval (0, 1). +Its implementation uses separate formulas for positive and negative inputs +to avoid overflow when evaluating large-magnitude values. -Script inspired from its corresponding Wikipedia article +Script inspired from its corresponding Wikipedia article: https://en.wikipedia.org/wiki/Sigmoid_function """ @@ -13,15 +14,17 @@ def sigmoid(vector: np.ndarray) -> np.ndarray: """ - Implements the sigmoid function + Implement the sigmoid function in a numerically stable way. Parameters: - vector (np.array): A numpy array of shape (1,n) - consisting of real values + vector (np.ndarray): An array of real-valued inputs. Returns: - sigmoid_vec (np.array): The input numpy array, after applying - sigmoid. + np.ndarray: The input array after applying the sigmoid function. + + Raises: + ValueError: If the input cannot be converted to a float array or + contains non-finite values. Examples: >>> sigmoid(np.array([-1.0, 1.0, 2.0])) @@ -29,8 +32,44 @@ def sigmoid(vector: np.ndarray) -> np.ndarray: >>> sigmoid(np.array([0.0])) array([0.5]) + + Large positive and negative values do not overflow: + >>> sigmoid(np.array([-1000.0, 1000.0])) + array([0., 1.]) + + Scalar arrays are supported: + >>> sigmoid(np.array(0.0)) + array(0.5) + + Non-numeric and non-finite inputs are rejected: + >>> sigmoid(np.array(["invalid"])) + Traceback (most recent call last): + ... + ValueError: sigmoid input must be numeric + >>> sigmoid(np.array([np.inf])) + Traceback (most recent call last): + ... + ValueError: sigmoid input must contain only finite values """ - 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)): + raise ValueError("sigmoid input must contain only finite values") + + result = np.empty_like(vector) + positive_mask = vector >= 0 + + result[positive_mask] = 1 / (1 + np.exp(-vector[positive_mask])) + + negative_exponentials = np.exp(vector[~positive_mask]) + result[~positive_mask] = negative_exponentials / ( + 1 + negative_exponentials + ) + + return result if __name__ == "__main__": From 87b71501eaca2847e6b2180bbd8300436f025dd6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:51:19 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/sigmoid.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index 2a154ed316ff..f8964544180e 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -65,9 +65,7 @@ def sigmoid(vector: np.ndarray) -> np.ndarray: result[positive_mask] = 1 / (1 + np.exp(-vector[positive_mask])) negative_exponentials = np.exp(vector[~positive_mask]) - result[~positive_mask] = negative_exponentials / ( - 1 + negative_exponentials - ) + result[~positive_mask] = negative_exponentials / (1 + negative_exponentials) return result From 86c5b648ebdba2350779475d8a3c5e2f926fef5c Mon Sep 17 00:00:00 2001 From: MEHULI KHANRA Date: Wed, 18 Nov 2026 18:23:01 +0530 Subject: [PATCH 3/4] Clarify sigmoid overflow warning behavior --- maths/sigmoid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index f8964544180e..15fdb3ee3ff0 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -33,7 +33,7 @@ def sigmoid(vector: np.ndarray) -> np.ndarray: >>> sigmoid(np.array([0.0])) array([0.5]) - Large positive and negative values do not overflow: + Large positive and negative values are handled without overflow warnings: >>> sigmoid(np.array([-1000.0, 1000.0])) array([0., 1.]) From 532e701ff0b1a2a85203c2ded13a04d1852a765c Mon Sep 17 00:00:00 2001 From: MEHULI KHANRA Date: Wed, 18 Nov 2026 18:26:44 +0530 Subject: [PATCH 4/4] Preserve sigmoid behavior for infinite inputs --- maths/sigmoid.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index 15fdb3ee3ff0..5511a0303442 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -3,7 +3,7 @@ The sigmoid function maps real-valued inputs to the interval (0, 1). Its implementation uses separate formulas for positive and negative inputs -to avoid overflow when evaluating large-magnitude values. +to avoid overflow warnings when evaluating large-magnitude values. Script inspired from its corresponding Wikipedia article: https://en.wikipedia.org/wiki/Sigmoid_function @@ -24,7 +24,7 @@ def sigmoid(vector: np.ndarray) -> np.ndarray: Raises: ValueError: If the input cannot be converted to a float array or - contains non-finite values. + contains NaN values. Examples: >>> sigmoid(np.array([-1.0, 1.0, 2.0])) @@ -37,27 +37,31 @@ def sigmoid(vector: np.ndarray) -> np.ndarray: >>> sigmoid(np.array([-1000.0, 1000.0])) array([0., 1.]) + Infinite values are supported: + >>> sigmoid(np.array([np.inf, -np.inf])) + array([1., 0.]) + Scalar arrays are supported: >>> sigmoid(np.array(0.0)) array(0.5) - Non-numeric and non-finite inputs are rejected: + Non-numeric and NaN inputs are rejected: >>> sigmoid(np.array(["invalid"])) Traceback (most recent call last): ... ValueError: sigmoid input must be numeric - >>> sigmoid(np.array([np.inf])) + >>> sigmoid(np.array([np.nan])) Traceback (most recent call last): ... - ValueError: sigmoid input must contain only finite values + ValueError: sigmoid input must not contain NaN values """ 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)): - raise ValueError("sigmoid input must contain only finite values") + if np.any(np.isnan(vector)): + raise ValueError("sigmoid input must not contain NaN values") result = np.empty_like(vector) positive_mask = vector >= 0