Softmax Activation

~15 mincode completion

Implement using the numerically stable formula.

Examples

3 logits

Input
softmax([1, 2, 3])
Output
[0.09003, 0.24473, 0.66524]

Equal logits: uniform distribution

Input
softmax([0, 0, 0])
Output
[0.33333, 0.33333, 0.33333]

Hints

Hint 1

Subtract the row max before exponentiating to keep the result stable.

Hint 2

Do not forget to subtract the max for stability. That step is easy to skip.

Requirements

  • z: 1D NumPy array of logits

  • Return 1D array of probabilities summing to 1.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

8 employers weight this skill

4 frontier labs, 2 big tech firms, 1 autonomy company, 1 enterprise vendor. Top match scores 92.

Python
import numpy as np

def softmax(z):
    """
    Compute the numerically stable softmax of z.

    Args:
        z: 1D NumPy array of logits

    Returns:
        1D array of probabilities summing to 1.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.