Problems
Loading...
1 / 1

Implement Softmax Function

Activation Functions
Medium

Implement the Softmax function, which converts raw scores (logits) into probabilities that sum to 1.

For a vector x = [x₁, x₂, …, xₙ]:

softmax(xi)=exijexj\text{softmax}(x_i) = \frac{e^{x_i}}{\sum_{j} e^{x_j}}

In practice, to avoid numerical overflow, the stable version subtracts the maximum value:

softmax(xi)=eximax(x)jexjmax(x)\text{softmax}(x_i) = \frac{e^{x_i - \max(x)}}{\sum_{j} e^{x_j - \max(x)}}
Loading visualization...

Examples

Input: np.array([1, 2, 3])

Output: [0.09003057, 0.24472847, 0.66524096]

Input: np.array([[1, 2, 3], [0, 0, 0]])

Output: [[0.0900, 0.2447, 0.6652], [0.3333, 0.3333, 0.3333]]

Hint 1

Use broadcasting to subtract np.max(x, axis=...) before exponentiation for numerical stability.

Hint 2

For 2D arrays, use axis=1, keepdims=True to maintain proper broadcasting dimensions.

Requirements

  • Must handle both 1D and 2D NumPy arrays
  • Should be vectorized (no loops)
  • Must be numerically stable (subtract max before exponentiation)
  • Output probabilities must sum to 1 per vector (or per row for 2D)

Constraints

  • Input size ≤ 10⁶ elements
  • Only NumPy allowed
Try Similar Problems