The Physics: Power, Chance, and Temperature
that may exist in a number of totally different states. Every state has an related vitality. Some states are low cost, requiring little or no vitality, whereas others are costly. One elementary query statistical physics asks is deceptively easy:
Given all these doable states, how possible is the system to occupy every one?
At first, it may appear tempting to make use of the energies themselves as chances. However energies aren’t chances. A state can have vitality 5, or -3, or 127, none of which says something about how possible it’s. Chances obey guidelines that energies don’t: they’ll by no means be detrimental, and collectively they need to add as much as precisely one.
The answer is to cease enthusiastic about chances altogether. As a substitute, every state’s vitality is first transformed right into a weight:
The exponential doesn’t produce chances. It produces relative significance. States with low vitality obtain giant weights, whereas high-energy states obtain exponentially smaller ones. At this stage, nothing has been normalized; the numbers merely categorical how strongly every state competes with the others.
Turning these weights into chances is nearly trivial. Divide every state’s weight by the sum of all weights:
That’s your complete Boltzmann distribution.
Regardless of its intimidating look, the equation performs solely two operations. The exponential interprets vitality into relative desire. The denominator rescales these preferences so that they type a legitimate chance distribution. What initially seems like a sophisticated system is actually only a weighting step adopted by a normalization step.
After this image is obvious for the reader, lets flip within the temperature variable. Discover that temperature by no means adjustments the energies themselves. As a substitute, it adjustments how a lot these vitality variations matter.
Think about three doable states with energies 0, 1, and a pair of. At a really low temperature, say , even these modest vitality variations grow to be monumental contained in the exponential. The bottom-energy state captures virtually all of the chance, whereas the opposite two grow to be virtually unattainable. Enhance the temperature to , and the other occurs. The identical vitality variations are actually closely compressed, producing practically an identical weights and a distribution that’s near uniform.
Check out the 2 following tables to visualise the idea higher (assuming for simplicity that ok=1):
| Power | weight if T=0.1 | Chance (%) |
| 0 | 1 | 99.99 |
| 1 | 0.000045 | 0.0045 |
| 2 | 0.000000002 | ~0 |
| Power | weight if T=10 | Chance |
| 0 | 1 | 36.7 |
| 1 | 0.905 | 33.2 |
| 2 | 0.819 | 30.1 |
Nothing in regards to the states modified. Their energies remained 0, 1, and a pair of all through. Solely the significance of these variations modified.
That is the position of temperature within the Boltzmann distribution. It doesn’t change the energies or introduce any new phrases into the equation. It merely controls how aggressively the chance distribution favors low-energy states. Low temperatures amplify small vitality variations into close to certainty. Excessive temperatures flatten these variations till each state turns into virtually equally believable.
That concept could sound uniquely bodily, however it isn’t. Within the subsequent part, the identical mathematical mechanism will quietly reappear inside each neural community that produces chances.
The LLM Equivalency: Vocabulary as States
To see how this connects to Giant Language Fashions, we solely want to vary the labels on our variables.
In physics, our system could be a gasoline, and the “states” are the totally different vitality ranges its molecules can occupy. In an LLM, the system is the precise second the mannequin must predict the subsequent phrase. The “states” are your complete vocabulary of the mannequin that means tens of 1000’s of doable phrases (or tokens) competing to be chosen subsequent.
When an LLM processes your immediate, it doesn’t instantly output chances. Its remaining neural layer spits out uncooked, unnormalized numbers for each single phrase in its vocabulary. In Machine Studying, we name these uncooked scores logits (). One phrase may get a logit of 12.5, one other -3.2, and one other 0.8.
Right here is the essential conceptual bridge: Logits are the mannequin’s vitality, however inverted.
Nature is inherently lazy and it tends to favor lower-energy states. Neural networks, then again, are grasping; they often favor the predictions with the highest doable rating ().
Softmax is Boltzmann in Disguise
To show these uncooked, unbounded logits right into a clear chance distribution that sums to at least one, ML engineers use the Softmax perform. If we explicitly embody the temperature parameter (T), the Softmax equation seems like this:
Look acquainted? It’s the very same mathematical trick. We change the detrimental vitality with our constructive logits . We exponentiate to show scores into relative weights, and we divide by the sum of all weights to normalize them. Softmax is simply the Boltzmann distribution sporting a pc science trench coat.
How Temperature Controls the “Warmth” of Textual content
As a result of the mathematics is an identical, the temperature parameter behaves mathematically precisely because it does in thermodynamics. It doesn’t alter the mannequin’s underlying beliefs because the uncooked logits stay unchanged. As a substitute, it dictates how strictly the mannequin enforces these beliefs.
Let’s take a look at how adjusting the thermostat adjustments the habits of a LLM:
- Absolute Zero (T=0): The Grasping Deterministic State:
Because the temperature approaches zero, even microscopic variations in logits are magnified infinitely. If “apple” has a logit of 5.01 and “banana” has 5.00, dividing by a tiny temperature blows that 0.01 distinction out of proportion. “Apple” will seize 99.99% of the chance mass. The mannequin turns into a deterministic machine, all the time choosing the only highest-scoring phrase. That is ideally suited for duties requiring strict logic, like writing Python code, fixing math equations, or extracting JSON information. There is no such thing as a room for randomness. - Room Temperature (T = 1): The Baseline:
At T=1, the temperature successfully disappears from the equation. The mannequin outputs the precise chance distribution it realized throughout its coaching. The textual content flows naturally, balancing anticipated grammar with the pure variance of human language. - Excessive Warmth (T > 1): The Artistic Chaos:
Once we crank up the temperature, we compress the variations between the logits. The exponential perform flattens out. The very best-scoring phrase loses its absolute dominance, and lower-scoring phrases start to seize significant chance mass.
Suppose again to statistical physics: at excessive temperatures, gasoline molecules have a lot thermal vitality that they’ll simply entry greater, tougher vitality states. In an LLM, excessive temperature offers the algorithm the “vitality” to entry mathematically unbelievable phrases. The mannequin takes dangers. It turns into inventive, poetic, and unpredictable.
For those who push the temperature too excessive (e.g., ), the distribution turns into virtually utterly uniform. The mannequin loses its grip on context and begins producing incoherent, virtually random textual content, nearer to noise than language
A Small however Vital Element: T = 0 Isn’t Actually Zero
There’s a delicate wrinkle value realizing in the event you’ve ever set in an API name. Mathematically, needs to be undefined, you’d be dividing each logit by zero, which the system merely can not deal with. So what really occurs whenever you request it?
In follow, LLM inference libraries don’t actually compute . As a substitute, is handled as a particular instruction: skip the softmax system fully, and simply choose the only highest-scoring logit immediately. That is known as grasping decoding, all the time output , the token with the largest uncooked rating, no exponentials, no normalization, no chance distribution in any respect.
The result seems the identical as an especially chilly Boltzmann distribution, virtually all chance piled onto a single state. However there’s a distinction in how you get there. Mathematically, you’d attain that final result progressively, by cooling T down towards zero and watching the distribution sharpen step-by-step. In code, there’s no gradual cooling in any respect, only a shortcut that jumps straight to the reply..
Watching It Occur: Actual Logits from GPT-2
Every thing up to now has been confirmed with toy numbers, three summary states, a handful of made-up logits for “apple” and “banana.” Let’s change that with one thing actual. GPT-2, a small, freely accessible language mannequin, will hand us precise logits for an precise sentence, and we are able to watch softmax reshape them in entrance of us, at 4 totally different temperatures.
The setup is easy: feed the mannequin the immediate “I’m drained, I wll take a __,” and ask it what comes subsequent. Internally, it produces one uncooked logit for each phrase in its vocabulary, tens of 1000’s of numbers. For visualization, we’ll maintain solely the highest 5 candidate logits and renormalize them with softmax over simply these 5 at T = 0.1, 0.5, 1.0, and a pair of.0.
import torch
import numpy as np
import matplotlib.pyplot as plt
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load GPT-2
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
mannequin = GPT2LMHeadModel.from_pretrained("gpt2")
mannequin.eval()
# Get actual logits for the subsequent token
immediate = "I'm drained, I'll take a"
input_ids = tokenizer.encode(immediate, return_tensors="pt")
with torch.no_grad():
outputs = mannequin(input_ids)
logits = outputs.logits[0, -1, :]
# High 5 candidate tokens by uncooked logit
top5 = torch.topk(logits, 5)
top_ids = top5.indices
top_logits = top5.values
tokens = [tokenizer.decode([tid]) for tid in top_ids]
print(f'Immediate: "{immediate}"n')
print("High 5 candidate subsequent tokens (uncooked logits):")
for t, l in zip(tokens, top_logits):
print(f" {t!r:10s} logit = {l.merchandise():.2f}")
# Run Softmax for various temperatures
temps = [0.1, 0.5, 1.0, 2.0]
outcomes = {}
for T in temps:
probs = torch.softmax(top_logits / T, dim=0).numpy()
outcomes[T] = probs
print(f"nT = {T}")
for t, p in zip(tokens, probs):
print(f" {t!r:10s} P = {p*100:.2f}%")
# Plots
fig, axes = plt.subplots(2, 2, figsize=(10, 8), sharey=True)
axes = axes.flatten()
colours = plt.cm.coolwarm(np.linspace(0.15, 0.85, len(tokens)))
for ax, T in zip(axes, temps):
probs = outcomes[T]
ax.bar(tokens, probs * 100, colour=colours)
ax.set_title(f"T = {T}", fontsize=12)
ax.set_ylim(0, 105)
ax.tick_params(axis='x', rotation=45)
ax.set_ylabel("Chance (%)")
fig.suptitle(f'Subsequent-token distribution for: "{immediate} ___"', fontsize=14)
plt.tight_layout()
plt.savefig("temperature_sweep.png", dpi=150, bbox_inches="tight")
plt.present()

Learn the 4 panels so as, and also you’re watching temperature do precisely what it did to our three-state toy system earlier, simply now on phrases that an actual mannequin really predicted. At , “nap” takes primarily all of it, 100%, the opposite 4 candidates don’t even register a visual bar. At , “nap” nonetheless dominates at round 74%, however “break” has picked up an actual, seen share close to 18%; the remainder stay minor. At , the mannequin’s relative preferences amongst these 5 candidates seem with none temperature scaling: “nap” leads at about 49%, whereas “break,” “relaxation,” “bathtub,” and “bathe” all retain significant, visibly distinct chances. Push to , and the gaps compress additional nonetheless, “nap” edges the sphere at round 34%, with the opposite 4 bunched shut behind as an alternative of trailing far off.
Nothing in regards to the mannequin modified throughout these 4 panels. Not the weights, not the coaching, not even the logits themselves, these stayed frozen your complete time. The one factor that moved was how aggressively these mounted logits received transformed right into a chance distribution. That’s the entire lesson of this text, sitting inside a single sentence completion: temperature is just not about creativity or randomness as some separate ingredient. It’s the identical dial physics has been turning for over a century, now induced into the final layer of a neural community.
Closing the loop
Begin again the place this text did: a sealed container of gasoline, and a query about how possible a single molecule is to take a seat in a single vitality state versus one other. Nothing in that query talked about language, tokens, or neural networks. And but, by the point GPT-2 finishes predicting the phrase after “I’m drained, I’ll take a,” it’s operating the an identical calculation, weighing uncooked scores into chances, then letting a single parameter determine how sharply these chances ought to decide to a winner.
That parameter saved the identical identify for a motive. It isn’t a metaphor borrowed to make an equation sound extra fascinating. Temperature in an LLM does, mathematically, precisely what temperature does in a gasoline: it decides how a lot the variations between choices are allowed to matter. Chilly, and one possibility swallows every part. Scorching, and each possibility blurs towards the identical shrug of a chance.
So the subsequent time you nudge a temperature slider, earlier than a coding assistant, a chatbot, a narrative generator, you’re not adjusting some advert hoc “creativity knob” invented for the event. You’re turning a dial that physicists had been already turning a century earlier than the primary neural community existed, simply pointed, this time, at a sentence as an alternative of a gasoline.














