From b00d7ff818389a78dc0a4a6a3ff05c7777b62dc5 Mon Sep 17 00:00:00 2001 From: MEHULI KHANRA Date: Tue, 17 Nov 2026 21:07:15 +0530 Subject: [PATCH 1/2] Add numerically stable sigmoid activation function --- machine_learning/sigmoid.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 machine_learning/sigmoid.py diff --git a/machine_learning/sigmoid.py b/machine_learning/sigmoid.py new file mode 100644 index 000000000000..dc912208c203 --- /dev/null +++ b/machine_learning/sigmoid.py @@ -0,0 +1,31 @@ +"""Sigmoid activation function for binary classification models. + +The sigmoid function maps a real-valued input to the interval (0, 1). + +>>> sigmoid(0) +0.5 +>>> round(sigmoid(2), 4) +0.8808 +>>> round(sigmoid(-2), 4) +0.1192 +>>> sigmoid(1000) +1.0 +>>> sigmoid(-1000) +0.0 +""" + +import math + + +def sigmoid(value: float) -> float: + """Return the numerically stable sigmoid of *value*.""" + if value >= 0: + return 1 / (1 + math.exp(-value)) + exp_value = math.exp(value) + return exp_value / (1 + exp_value) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 1a557e16f7dab36202e1a3f84052cc63d9f901d3 Mon Sep 17 00:00:00 2001 From: MEHULI KHANRA Date: Tue, 17 Nov 2026 21:07:25 +0530 Subject: [PATCH 2/2] Add numerically stable softmax activation function --- machine_learning/softmax.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 machine_learning/softmax.py diff --git a/machine_learning/softmax.py b/machine_learning/softmax.py new file mode 100644 index 000000000000..132ffccd3601 --- /dev/null +++ b/machine_learning/softmax.py @@ -0,0 +1,37 @@ +"""Softmax activation function for multiclass classification. + +Softmax converts a sequence of scores into probabilities that sum to one. + +>>> probabilities = softmax([2.0, 1.0, 0.1]) +>>> [round(value, 4) for value in probabilities] +[0.659, 0.2424, 0.0986] +>>> sum(softmax([1.0, 2.0, 3.0])) +1.0 +>>> softmax([]) +Traceback (most recent call last): + ... +ValueError: scores must not be empty +""" + +import math + + +def softmax(scores: list[float]) -> list[float]: + """Return softmax probabilities for a non-empty list of scores. + + Subtracting the maximum score before exponentiation improves numerical + stability and prevents overflow for large input values. + """ + if not scores: + raise ValueError("scores must not be empty") + + max_score = max(scores) + exponentials = [math.exp(score - max_score) for score in scores] + total = sum(exponentials) + return [value / total for value in exponentials] + + +if __name__ == "__main__": + import doctest + + doctest.testmod()