Skip to content

Softmax

Functions

softmax(Z)

Compute the softmax activation function.

Parameters:

Name Type Description Default
Z ndarray

The input array.

required

Returns:

Type Description

numpy.ndarray: An array with the same shape as Z, containing the softmax activation values.

Example
Z = np.array([[1, 2], [3, 4]])
A = softmax(Z)
print(A)  # Output: [[0.119203 0.119203] [0.880797 0.880797]]
Source code in microkeras/activations/softmax.py
def softmax(Z):
    """
    Compute the softmax activation function.

    Args:
        Z (numpy.ndarray): The input array.

    Returns:
        numpy.ndarray: An array with the same shape as Z, containing the softmax activation values.

    Example:
        ```python
        Z = np.array([[1, 2], [3, 4]])
        A = softmax(Z)
        print(A)  # Output: [[0.119203 0.119203] [0.880797 0.880797]]
        ```
    """
    exp_shifted = np.exp(Z - np.max(Z))
    return exp_shifted / (exp_shifted.sum(axis=0) + 1e-8)