#186Softmax with temperatureEasyNeural NetworksActivation Functions
Softmax with temperature
Background
Softmax turns a vector of logits into a probability distribution. A temperature rescales the logits first, controlling how sharp the distribution is:
Low sharpens toward the argmax (more confident); high flattens toward uniform. is plain softmax.
Problem statement
Implement softmax_temperature(z, T) returning the temperature-scaled softmax of the logit vector z.
Input
z— array-like of logits.T—float, the temperature.
Output
Returns an np.ndarray of probabilities (same length as z) that are non-negative and sum to 1.
Examples
Example 1 — equal logits give a uniform distribution
Input: z = [1, 1, 1], T = 1
Output: [0.33333333 0.33333333 0.33333333]
Example 2 — plain softmax (T=1)
Input: z = [2, 1, 0], T = 1
Output: [0.66524096 0.24472847 0.09003057]
Explanation: normalised.
Constraints
- Divide the logits by
Tbefore exponentiating. - The output must sum to 1.
- Return an
np.ndarray.
Notes
- As the output approaches uniform; as it approaches a one-hot at the largest logit. Temperature is the knob behind "sampling temperature" in language models.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Equal logits give a uniform distribution (example)
- •Plain softmax at T=1 (reference)
- •Probabilities sum to one on a sample vector