Softmax Activation Function

EasyML Coding

Problem statement

Write a Python function softmax(scores) that computes the softmax activation for a list of scores and returns the resulting probabilities as a list of floats:

$$\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_{j} e^{z_j}}$$

Your implementation must be numerically stable: it should not overflow when the scores are large (for example, scores in the thousands).

Example

Input:

print([round(x, 4) for x in softmax([1, 2, 3])])

Output:

[0.09, 0.2447, 0.6652]

Reasoning: each output is proportional to the exponential of its score, divided by the sum of the exponentials of all scores, so the outputs form a probability distribution that sums to 1.

Show hint

What property of the softmax lets you add or subtract the same constant from every score without changing the output?

Relevant roles: ML Engineer, Deep Learning Engineer, LLM Engineer and AI Engineer.

Related free problems