Implement Softmax Function
Implement Softmax Function
Implement the Softmax function, which converts raw scores (logits) into probabilities that sum to 1.
For a vector x = [x₁, x₂, …, xₙ]:
softmax(xi)=∑jexjexiIn practice, to avoid numerical overflow, the stable version subtracts the maximum value:
softmax(xi)=∑jexj−max(x)exi−max(x)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
Log in to take notes on this problem
Accepts: array
Implement Softmax Function
Implement Softmax Function
Implement the Softmax function, which converts raw scores (logits) into probabilities that sum to 1.
For a vector x = [x₁, x₂, …, xₙ]:
softmax(xi)=∑jexjexiIn practice, to avoid numerical overflow, the stable version subtracts the maximum value:
softmax(xi)=∑jexj−max(x)exi−max(x)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
Log in to take notes on this problem
Accepts: array