Softmax with Temperature

easy
Unit 1 · Tokenization0/2 to milestone

Every LLM turns raw logits into a probability distribution over the vocabulary with softmax. A temperature TT controls how sharp that distribution is:

pi=ezi/T∑jezj/Tp_i = \frac{e^{z_i / T}}{\sum_j e^{z_j / T}}

Implement softmax(logits, temperature=1.0) returning a NumPy array of probabilities.

Requirements

  • Must be numerically stable — logits like [1000, 1001] must not overflow.
  • Lower temperature → sharper (more confident) distribution; higher → flatter.

Example

softmax(np.array([1.0, 2.0, 3.0]))
# array([0.0900, 0.2447, 0.6652])
#llm#sampling#numerics3 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Softmax with Temperature · TechGradify