-
-
Notifications
You must be signed in to change notification settings - Fork 50k
refactor: add type hints to maths/softmax.py #14153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,25 +13,22 @@ | |
| import numpy as np | ||
|
|
||
|
|
||
| def softmax(vector): | ||
| def softmax(vector: np.ndarray | list | tuple) -> np.ndarray: | ||
| """ | ||
| 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 | ||
| vector (np.array | list | tuple): A numpy array of shape (1,n) | ||
| consisting of real values or a similar list, tuple | ||
| Returns: | ||
| softmax_vec (np.array): The input numpy array after applying | ||
| softmax. | ||
| np.array: The input numpy array after applying softmax. | ||
|
Comment on lines
+21
to
+25
|
||
| 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 for 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]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new annotation
vector: np.ndarray | list | tupleis very broad and loses element-type information. Consider using a typed protocol/ABC (e.g.,Sequence[float]fromcollections.abc) for list/tuple inputs and/or specifying element types (e.g.,list[float] | tuple[float, ...]) to make the type hint more informative and consistent with other modules (e.g.,maths/polynomial_evaluation.py).