From 01ae0fbccba75e9c69174b225ed655066ec30e62 Mon Sep 17 00:00:00 2001 From: Jashwanthallenki <141424170+Jashwanthallenki@users.noreply.github.com> Date: Fri, 10 Oct 2025 11:19:55 +0530 Subject: [PATCH 1/4] Update softmax.py --- maths/softmax.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/maths/softmax.py b/maths/softmax.py index 95c95e66f59e..c199e2a2166b 100644 --- a/maths/softmax.py +++ b/maths/softmax.py @@ -13,14 +13,14 @@ import numpy as np -def softmax(vector): +def softmax(vector, axis=-1): """ Implements the softmax function Parameters: vector (np.array,list,tuple): A numpy array of shape (1,n) consisting of real values or a similar list,tuple - + axis (int, optional): Axis along which to compute softmax. Default is -1. Returns: softmax_vec (np.array): The input numpy array after applying @@ -39,18 +39,40 @@ def softmax(vector): array([1.]) """ - # Calculate e^x for each x in your vector where e is Euler's - # number (approximately 2.718) - exponent_vector = np.exp(vector) + # Convert input to numpy array of floats + vector = np.asarray(vector, dtype=float) + + # Handle empty input + if vector.size == 0: + raise ValueError("softmax input must be non-empty") + + # Validate axis + if not (-vector.ndim <= axis < vector.ndim): + raise np.AxisError(f"axis {axis} is out of bounds for array of dimension {vector.ndim}") + + # Subtract max for numerical stability + vector_max = np.max(vector, axis=axis, keepdims=True) + exponent_vector = np.exp(vector - vector_max) - # Add up the all the exponentials - sum_of_exponents = np.sum(exponent_vector) + # Sum of exponentials along the axis + sum_of_exponents = np.sum(exponent_vector, axis=axis, keepdims=True) - # Divide every exponent by the sum of all exponents + # Divide each exponent by the sum along the axis softmax_vector = exponent_vector / sum_of_exponents return softmax_vector if __name__ == "__main__": + # Single value print(softmax((0,))) + + # Vector + print(softmax([1, 2, 3])) + + # Matrix along last axis + mat = np.array([[1, 2, 3], [4, 5, 6]]) + print("Softmax along last axis:\n", softmax(mat)) + + # Matrix along axis 0 + print("Softmax along axis 0:\n", softmax(mat, axis=0)) From 02fad70941f6c0638bee72f293b18a6ef959a8f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Oct 2025 06:04:18 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/softmax.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/softmax.py b/maths/softmax.py index c199e2a2166b..3b9c9afa393b 100644 --- a/maths/softmax.py +++ b/maths/softmax.py @@ -48,7 +48,9 @@ def softmax(vector, axis=-1): # Validate axis if not (-vector.ndim <= axis < vector.ndim): - raise np.AxisError(f"axis {axis} is out of bounds for array of dimension {vector.ndim}") + raise np.AxisError( + f"axis {axis} is out of bounds for array of dimension {vector.ndim}" + ) # Subtract max for numerical stability vector_max = np.max(vector, axis=axis, keepdims=True) From 35ad4024de1cbd293333a9bfbeab4f2a2811b557 Mon Sep 17 00:00:00 2001 From: Jashwanthallenki <141424170+Jashwanthallenki@users.noreply.github.com> Date: Fri, 10 Oct 2025 11:38:57 +0530 Subject: [PATCH 3/4] Update softmax.py --- maths/softmax.py | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/maths/softmax.py b/maths/softmax.py index 3b9c9afa393b..35890818f536 100644 --- a/maths/softmax.py +++ b/maths/softmax.py @@ -1,44 +1,44 @@ """ This script demonstrates the implementation of the Softmax function. -Its a function that takes as input a vector of K real numbers, and normalizes -it into a probability distribution consisting of K probabilities proportional -to the exponentials of the input numbers. After softmax, the elements of the -vector always sum up to 1. +It takes as input a vector of K real numbers and normalizes it into a +probability distribution consisting of K probabilities proportional +to the exponentials of the input numbers. After applying softmax, +the elements of the vector always sum up to 1. -Script inspired from its corresponding Wikipedia article +Script inspired by its corresponding Wikipedia article: https://en.wikipedia.org/wiki/Softmax_function """ import numpy as np +from numpy.exceptions import AxisError -def softmax(vector, axis=-1): +def softmax(vector: np.ndarray, axis: int = -1) -> np.ndarray: """ - Implements the softmax function + Implements the softmax function. Parameters: - vector (np.array,list,tuple): A numpy array of shape (1,n) - consisting of real values or a similar list,tuple - axis (int, optional): Axis along which to compute softmax. Default is -1. + vector (np.ndarray | list | tuple): A numpy array of shape (1, n) + consisting of real values or a similar list/tuple. + axis (int, optional): Axis along which to compute softmax. + Default is -1. Returns: - softmax_vec (np.array): The input numpy array after applying - softmax. + np.ndarray: The input numpy array after applying softmax. - The softmax vector adds up to one. We need to ceil to mitigate for - precision - >>> float(np.ceil(np.sum(softmax([1,2,3,4])))) + The softmax vector adds up to one. We need to ceil to mitigate precision. + + >>> float(np.ceil(np.sum(softmax([1, 2, 3, 4])))) 1.0 - >>> vec = np.array([5,5]) + >>> vec = np.array([5, 5]) >>> softmax(vec) array([0.5, 0.5]) >>> softmax([0]) array([1.]) """ - # Convert input to numpy array of floats vector = np.asarray(vector, dtype=float) @@ -47,11 +47,10 @@ def softmax(vector, axis=-1): raise ValueError("softmax input must be non-empty") # Validate axis - if not (-vector.ndim <= axis < vector.ndim): - raise np.AxisError( - f"axis {axis} is out of bounds for array of dimension {vector.ndim}" - ) - + ndim = vector.ndim + if axis >= ndim or axis < -ndim: + error_message = f"axis {axis} is out of bounds for array of dimension {ndim}" + raise AxisError(error_message) # Subtract max for numerical stability vector_max = np.max(vector, axis=axis, keepdims=True) exponent_vector = np.exp(vector - vector_max) @@ -61,20 +60,15 @@ def softmax(vector, axis=-1): # Divide each exponent by the sum along the axis softmax_vector = exponent_vector / sum_of_exponents - return softmax_vector - if __name__ == "__main__": # Single value print(softmax((0,))) - # Vector print(softmax([1, 2, 3])) - # Matrix along last axis mat = np.array([[1, 2, 3], [4, 5, 6]]) print("Softmax along last axis:\n", softmax(mat)) - # Matrix along axis 0 print("Softmax along axis 0:\n", softmax(mat, axis=0)) From 0d8407162e75c86447300bd2c96efed695c83aad Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 10 Oct 2025 06:09:17 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/softmax.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/softmax.py b/maths/softmax.py index 35890818f536..6fb0b63248e2 100644 --- a/maths/softmax.py +++ b/maths/softmax.py @@ -62,6 +62,7 @@ def softmax(vector: np.ndarray, axis: int = -1) -> np.ndarray: softmax_vector = exponent_vector / sum_of_exponents return softmax_vector + if __name__ == "__main__": # Single value print(softmax((0,)))