Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 46 additions & 9 deletions maths/sigmoid.py
Original file line number Diff line number Diff line change
@@ -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
"""

Expand All @@ -13,24 +14,60 @@

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]))
array([0.26894142, 0.73105858, 0.88079708])

>>> 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__":
Expand Down
Loading