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
17 changes: 7 additions & 10 deletions maths/softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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])

Expand Down