diff --git a/maths/sigmoid.py b/maths/sigmoid.py index cb45bde2702c..f8964544180e 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,42 @@ 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__": diff --git a/maths/softmax.py b/maths/softmax.py index ff872e2d17d0..2ebbc5eb8c46 100644 --- a/maths/softmax.py +++ b/maths/softmax.py @@ -33,15 +33,10 @@ def softmax(vector: np.ndarray, axis: int | None = -1) -> np.ndarray: probability distribution that sums to 1. Raises: - ValueError: If ``vector`` is empty or cannot be converted to a numeric - float array (for example a string or a dict). + ValueError: If ``vector`` is empty, cannot be converted to a numeric + float array, or contains non-finite values. numpy.exceptions.AxisError: If ``axis`` is out of bounds for the input. - Note: - If the input contains ``NaN`` or ``inf`` the result will contain - ``NaN`` along the affected axis; softmax is only meaningful for finite - real inputs. - The softmax vector adds up to one. We need to ceil to mitigate precision. >>> float(np.ceil(np.sum(softmax([1, 2, 3, 4])))) @@ -77,7 +72,7 @@ def softmax(vector: np.ndarray, axis: int | None = -1) -> np.ndarray: >>> float(np.round(np.sum(softmax(mat, axis=None)), 6)) 1.0 - Empty, non-numeric and out-of-bounds inputs raise clear errors: + Empty, non-numeric, non-finite and out-of-bounds inputs raise clear errors: >>> softmax([]) Traceback (most recent call last): @@ -91,6 +86,10 @@ def softmax(vector: np.ndarray, axis: int | None = -1) -> np.ndarray: Traceback (most recent call last): ... numpy.exceptions.AxisError: axis 3 is out of bounds for array of dimension 1 + >>> softmax([1.0, np.nan]) + Traceback (most recent call last): + ... + ValueError: softmax input must contain only finite values """ # Convert input to a float numpy array, turning numpy's terse conversion # errors into a clear message about the unsupported input type. @@ -100,9 +99,11 @@ def softmax(vector: np.ndarray, axis: int | None = -1) -> np.ndarray: error_message = f"softmax input must be numeric, got {type(vector).__name__}" raise ValueError(error_message) from exc - # Handle empty input + # Handle empty and non-finite input if vector.size == 0: raise ValueError("softmax input must be non-empty") + if not np.all(np.isfinite(vector)): + raise ValueError("softmax input must contain only finite values") # Validate axis (None means "treat the whole array as one distribution") if axis is not None: