Skip to content

Commit 93e436d

Browse files
Validate finite inputs in softmax
1 parent 30507bb commit 93e436d

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

‎maths/softmax.py‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,10 @@ def softmax(vector: np.ndarray, axis: int | None = -1) -> np.ndarray:
3333
probability distribution that sums to 1.
3434
3535
Raises:
36-
ValueError: If ``vector`` is empty or cannot be converted to a numeric
37-
float array (for example a string or a dict).
36+
ValueError: If ``vector`` is empty, cannot be converted to a numeric
37+
float array, or contains non-finite values.
3838
numpy.exceptions.AxisError: If ``axis`` is out of bounds for the input.
3939
40-
Note:
41-
If the input contains ``NaN`` or ``inf`` the result will contain
42-
``NaN`` along the affected axis; softmax is only meaningful for finite
43-
real inputs.
44-
4540
The softmax vector adds up to one. We need to ceil to mitigate precision.
4641
4742
>>> 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:
7772
>>> float(np.round(np.sum(softmax(mat, axis=None)), 6))
7873
1.0
7974
80-
Empty, non-numeric and out-of-bounds inputs raise clear errors:
75+
Empty, non-numeric, non-finite and out-of-bounds inputs raise clear errors:
8176
8277
>>> softmax([])
8378
Traceback (most recent call last):
@@ -91,6 +86,10 @@ def softmax(vector: np.ndarray, axis: int | None = -1) -> np.ndarray:
9186
Traceback (most recent call last):
9287
...
9388
numpy.exceptions.AxisError: axis 3 is out of bounds for array of dimension 1
89+
>>> softmax([1.0, np.nan])
90+
Traceback (most recent call last):
91+
...
92+
ValueError: softmax input must contain only finite values
9493
"""
9594
# Convert input to a float numpy array, turning numpy's terse conversion
9695
# 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:
10099
error_message = f"softmax input must be numeric, got {type(vector).__name__}"
101100
raise ValueError(error_message) from exc
102101

103-
# Handle empty input
102+
# Handle empty and non-finite input
104103
if vector.size == 0:
105104
raise ValueError("softmax input must be non-empty")
105+
if not np.all(np.isfinite(vector)):
106+
raise ValueError("softmax input must contain only finite values")
106107

107108
# Validate axis (None means "treat the whole array as one distribution")
108109
if axis is not None:

0 commit comments

Comments
 (0)