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)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]]
Use broadcasting to subtract np.max(x, axis=...) before exponentiation for numerical stability.
For 2D arrays, use axis=1, keepdims=True to maintain proper broadcasting dimensions.
Sign in to take notes on this problem
Accepts: array
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)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]]
Use broadcasting to subtract np.max(x, axis=...) before exponentiation for numerical stability.
For 2D arrays, use axis=1, keepdims=True to maintain proper broadcasting dimensions.
Sign in to take notes on this problem
Accepts: array