srinivas raghav blog's

Deep Learning Note 5: Score Spread, Temperature, and Gradients

We can now connect this to our earlier discussion of mean and variance. Suppose we have a collection of N compatibility scores,

s=(s1,s2,…,sN)

We can calculate their mean as

μ=1N∑j=1Nsj

The mean describes the common numerical location around which the scores are distributed. We can also calculate their variance.

σ2=1N∑j=1N(sj−μ)2

The standard deviation is the square root of this quantity.

σ=1N∑j=1N(sj−μ)2

Here, we are treating the scores in a particular attention row as a finite collection and calculating their population variance. We are not trying to estimate an unknown distribution using the sample variance formula. Consider two collections of scores:

A=(4,5,6)

and

B=(0,5,10)

Both collections have the same mean.

μA=μB=5

However, their spreads are different. In the first collection, the scores lie close together. In the second collection, their deviations from the mean are considerably larger. The first collection has variance

σA2=(4−5)2+(5−5)2+(6−5)23=1+0+13=23

Consequently,

σA=23≈0.8165

For the second collection,

σB2=(0−5)2+(5−5)2+(10−5)23=25+0+253=503

Therefore,

σB=503≈4.0825

The second collection has a standard deviation five times larger than the first, despite having exactly the same mean. This distinction becomes particularly important when we apply softmax. We can already anticipate that the second collection will produce a more concentrated attention distribution because its highest score has a much greater advantage over its competitors. But let us understand this more systematically.

Standardizing scores into location and spread

Every score sj can be written as its deviation from the mean plus the mean itself.

sj=μ+(sj−μ)

There is no assumption involved here. We have simply added and subtracted the same quantity. Now suppose the standard deviation σ is nonzero. We can divide each deviation by σ to obtain

uj=sj−μσ

This operation is called standardization. The quantity uj tells us how far the score sj lies from the mean, measured in units of standard deviation. For instance, if uj=2, the original score lies two standard deviations above the mean. If uj=−1, the score lies one standard deviation below the mean. If uj=0, the score equals the mean. The standardized values are often called z-scores in statistics, although we are using the letter u here to avoid confusion with the logits. Now multiply both sides of the standardization equation by σ.

σuj=sj−μ

Adding μ to both sides,

sj=μ+σuj

This is where our earlier equation comes from. It is simply a rearrangement of the definition of a standardized variable. However, this rearrangement gives us a particularly powerful interpretation. It expresses every score using three pieces of information: a common location μ, a common scale σ, and a dimensionless relative position uj. We can think of u as describing the shape or relative arrangement of the scores. The mean μ determines where this arrangement is located on the number line. The standard deviation σ determines how widely the arrangement is stretched. To understand this, suppose we begin with the scores

s=(2,4,6)

Their mean is

μ=2+4+63=4

Their variance is

σ2=(−2)2+02+223=83

Their standard deviation is

σ=83≈1.633

After standardization, the scores become

u1=2−41.633≈−1.225u2=4−41.633=0u3=6−41.633≈1.225

Thus,

u≈(−1.225,0,1.225)

We have removed the original location and scale of the scores. Only their relative arrangement remains. We can reconstruct the original scores by multiplying the standardized values by σ and adding the mean.

s=μ1+σu

The standardized collection has two useful mathematical properties. Its mean is zero. To prove this, consider

1N∑juj=1N∑jsj−μσ=1Nσ(∑jsj−Nμ)

By definition,

∑jsj=Nμ

Therefore,

1N∑juj=0

The standardized collection also has unit variance. Because its mean is zero, its variance is simply its mean squared value.

Var(U)=1N∑juj2=1N∑j(sj−μ)2σ2=1σ21N∑j(sj−μ)2

But the final average is precisely the original variance σ2. Consequently,

Var(U)=σ2σ2=1

Therefore,

Mean(U)=0,Var(U)=1

We have constructed a standardized representation of the original scores with zero mean and unit variance. Notice that zero mean does not mean every standardized score is zero. It means that the positive and negative deviations balance when averaged. Similarly, unit variance does not mean that every score equals either one or minus one. It means that the average squared deviation from the mean equals one. Some standardized values may be larger than one in magnitude, while others may be smaller. The collection (−3/2,0,3/2), for instance, has zero mean and unit variance, despite containing values whose magnitudes are not one. Now let us substitute the decomposition

sj=μ+σuj

into softmax.

pj=eμ+σuj∑keμ+σuk

Using the properties of exponentiation,

pj=eμeσujeμ∑keσuk

The common factor eμ cancels. Consequently,

pj=eσuj∑keσuk

Or, equivalently,

softmax(μ1+σu)=softmax(σu)

This equation tells us that the mean of the scores has no direct effect on the softmax distribution. The common location disappears. What remains is the relative arrangement u and its scale σ. We can understand the importance of σ by imagining the standardized scores as points on a number line attached to a stretchable piece of elastic. The mean determines where the centre of the elastic is placed. Moving the entire elastic to the left or right changes every score equally. Their separations remain unchanged, so softmax remains unchanged. The standard deviation determines how far the points are stretched away from the centre.

Stretching the elastic increases the distances between the points, changing their relative score differences and therefore changing their softmax probabilities. This is why scale matters even when location does not. Consider a simple arrangement of scores centred around zero.

u=(−32,0,32)

This collection has zero mean and unit variance. Now construct a family of score vectors by multiplying this arrangement by a positive number σ.

s(σ)=σu

By construction, the standard deviation of these score vectors is σ. If σ is small, the scores become close together. For example, when σ=0.1,

s≈(−0.1225,0,0.1225)

The resulting probabilities are approximately

p≈(0.293,0.332,0.375)

All three values participate to a reasonably similar extent. Now increase σ to one.

s≈(−1.225,0,1.225)

The resulting probabilities become approximately

p≈(0.062,0.213,0.725)

The third value becomes considerably more influential. Now increase σ to five.

s≈(−6.124,0,6.124)

The corresponding probabilities become approximately

p≈(0.000005,0.00219,0.99781)

Almost all the attention now belongs to the third value. We have not changed which score is largest. We have not changed the ordering of the scores. We have only increased the distances between them. Softmax converts these increased distances into a more concentrated distribution. This becomes especially clear when we examine the ratio between two probabilities.

pipj=esi−sj

Substituting si=μ+σui and sj=μ+σuj,

pipj=e(μ+σui)−(μ+σuj)=eσ(ui−uj)

Consequently,

pipj=eσ(ui−uj)

For a fixed arrangement of standardized scores, the standard deviation directly scales the exponent controlling the ratio between probabilities. This is why the spread of the logits influences the strength of their competition. If the standardized score difference ui−uj is positive, increasing σ makes the probability ratio grow exponentially. If the standardized score difference is negative, increasing σ makes the ratio shrink exponentially. However, we should be precise about what this does and does not imply. The variance of a collection of scores does not uniquely determine its softmax distribution. Two collections can have identical means and variances while possessing very different relative arrangements and extreme values. For example, consider

A=(−1,−1,1,1)

and

B=(−2,0,0,2)

Both have mean zero and variance one. However, the first collection has two equally largest scores, while the second collection has one uniquely largest score. Their softmax distributions are different. Therefore, we should not say that variance alone determines attention selectivity. The precise statement is that when the standardized arrangement remains fixed, increasing its scale makes the softmax distribution increasingly concentrated on the largest scores. This distinction will be important when we consider real attention logits, whose entire distribution may change during training. We can understand the behaviour at the two mathematical extremes. First, suppose that σ approaches zero while the standardized arrangement u remains fixed.

σ→0

Then every scaled score approaches zero.

σuj→0

Exponentiating,

eσuj→e0=1

The denominator becomes a sum of N ones.

∑k=1Neσuk→N

Consequently,

limσ→0+pj=1N

All the attention probabilities become equal. When the scores become indistinguishable, softmax has no mathematical reason to prefer one value over another. If all original scores are exactly identical, their variance is exactly zero. We cannot standardize that collection by dividing by zero, but we can directly see that its softmax distribution is uniform. At the other extreme, suppose we continuously increase the scale σ.

σ→∞

This does not mean that an actual model contains a standard deviation equal to infinity. It is a mathematical way of asking what happens as we consider a sequence of score vectors with progressively larger and larger spreads. Imagine increasing the standard deviation from one to ten, then to one hundred, then to one thousand, while preserving the same standardized arrangement. We want to understand the behaviour that softmax approaches in this process. Suppose um is the unique largest standardized score. The probability associated with this score is

pm=eσum∑jeσuj

We can divide the numerator and denominator by eσum.

pm=1∑jeσ(uj−um)

Separating the largest score's own contribution,

pm=11+∑j≠meσ(uj−um)

Because um is the unique maximum, every competing score satisfies

uj−um<0

As σ becomes increasingly large, the quantity σ(uj−um) becomes increasingly negative. Consequently,

eσ(uj−um)→0

Every competing exponential contribution disappears in the limit. The denominator approaches one. Therefore,

limσ→∞pm=1

At the same time,

limσ→∞pj=0,j≠m

When we write pmax→1, we mean that the probability associated with the highest-scoring alternative approaches 100%. The attention output consequently approaches the value vector associated with that alternative.

o=∑jpjvj→vm

Softmax gradually approaches hard selection. This is why the function is called softmax. It provides a smooth distribution over alternatives that can approach the behaviour of selecting the maximum. More precisely, if several scores are tied for the maximum, increasing the scale does not distinguish between those tied scores. If there are M equally largest scores, their probabilities approach 1/M, while the probabilities of the lower-scoring alternatives approach zero. This shows that even the limiting behaviour depends on the relative arrangement of the scores. We can now understand attention as a mechanism capable of moving continuously between two limiting behaviours.

At one extreme, it retrieves an approximately uniform average of the available values.

ouniform=1N∑jvj

At the other extreme, it retrieves almost exclusively the value associated with the largest compatibility score.

oselective≈vm

Between these extremes, it retrieves different mixtures of information. What controls where we lie between these behaviours? One important quantity is the spread of the compatibility scores. Another way of controlling that spread is through a parameter known as temperature. Temperature-scaled softmax is defined as

pj(T)=esj/T∑kesk/T

where T>0. Let us first understand why dividing by a number changes the spread of a collection. Suppose a random variable X has mean μ and variance σ2. We define another random variable

Y=aX

Its mean is

E[Y]=aE[X]=aμ

Its variance is

Var(Y)=E[(Y−E[Y])2]=E[(aX−aμ)2]=E[a2(X−μ)2]=a2Var(X)

Consequently,

Var(aX)=a2Var(X)

Taking the square root,

Std(aX)=|a|Std(X)

Dividing scores by temperature is equivalent to multiplying them by a=1/T. Therefore,

Var(XT)=σ2T2

and

Std(XT)=σT

Temperature is therefore directly related to the effective spread of the scores. When T<1, dividing by T increases the magnitude of the differences between scores. When T>1, dividing by T decreases those differences. Let us see how this works using the same three scores throughout.

s=(2,1,0)

At temperature T=1, the scores remain unchanged.

s1=(2,1,0)

The probabilities are approximately

p(1)=(0.6652,0.2447,0.0900)

Now suppose we increase the temperature to T=2.

s2=(1,0.5,0)

The differences between the scores have become smaller. The probabilities become

p(2)≈(0.5065,0.3072,0.1863)

The highest-scoring value now receives a smaller share of attention, while the lower-scoring values receive greater shares. The distribution becomes more uniform. Now decrease the temperature to T=0.5.

s0.5=(4,2,0)

The differences between the scores have doubled. The probabilities become

p(0.5)≈(0.8668,0.1173,0.0159)

The highest-scoring value now receives almost 87% of the attention. At a temperature of T=0.1, the effective scores become

(20,10,0)

and the probabilities become approximately

p(0.1)≈(0.999955,0.0000454,0.000000002)

We have moved from a mixture of several values to an almost exclusive selection of one value. Throughout this entire process, the ranking of the original scores has remained unchanged. Temperature has not decided which key is most relevant. It has decided how strongly the existing preferences should influence the retrieved information. This is a useful distinction. The compatibility scores express what the model prefers. The temperature controls how decisively those preferences are translated into weights. We can connect this directly to the standardized representation of the scores. Suppose

sj=μ+σuj

Then temperature-scaled softmax becomes

pj(T)=e(μ+σuj)/T∑ke(μ+σuk)/T

Once again, the common mean cancels.

pj(T)=e(σ/T)uj∑ke(σ/T)uk

Look carefully at the ratio σ/T. Increasing the standard deviation and decreasing the temperature have mathematically equivalent effects when the arrangement of the scores remains fixed. Both increase the effective scale entering softmax. Likewise, decreasing the standard deviation and increasing the temperature both reduce the effective scale.

Inverse temperature and the log-partition function

This suggests that softmax selectivity is closely related to a kind of effective inverse temperature.

βeff=σT

A large effective inverse temperature produces stronger competition. A small effective inverse temperature produces weaker competition. This also explains the entropy interpretation of temperature that we encountered earlier. When T is large, the entropy term in the optimization objective becomes more influential, encouraging a more spread-out distribution. When T is small, maximizing compatibility becomes more influential, encouraging concentrated selection. There is an even more precise mathematical connection between softmax, temperature, and variance. Let us define the inverse temperature β=1/T and introduce the log-partition function

F(β)=ln∑jeβsj

The quantity inside the logarithm is the total transformed score used for normalization. Differentiating with respect to β gives

F′(β)=∑jsjeβsj∑keβsk=∑jpj(β)sj

Therefore,

F′(β)=Ep(β)[s]

The derivative of the log-partition function is the expected score under the current softmax distribution. If we differentiate a second time, we obtain

F″(β)=∑jpj(β)sj2−(∑jpj(β)sj)2=Ep[s2]−Ep[s]2

Recall our earlier identity for variance.

Var(X)=E[X2]−E[X]2

Consequently,

F″(β)=Varp(β)(s)

This is a particularly elegant connection between our two mathematical bridges. The curvature of the log-partition function with respect to inverse temperature is exactly the variance of the scores under the softmax distribution. Notice that this is the variance under the current probability weights, rather than necessarily the uniform empirical variance of the original collection. As we increase β, the expected score can only increase or remain unchanged, because variance is non-negative.

ddβEp(β)[s]=Varp(β)(s)≥0

Increasing the strength of competition makes the distribution favour higher-scoring alternatives. Moreover, we can write the entropy of this distribution as

H(p(β))=F(β)−βF′(β)

Differentiating,

dHdβ=F′(β)−F′(β)−βF″(β)=−βVarp(β)(s)

Hence,

dHdβ=−βVarp(β)(s)≤0

For β>0, increasing the inverse temperature cannot increase the entropy. This mathematically establishes the relationship we have been describing intuitively. Increasing the effective scale of a fixed collection of scores makes the distribution less uncertain and more selective. We can also understand why excessively concentrated attention can create problems for learning. Suppose there are only two available keys. Their probabilities are

p1=es1es1+es2

and

p2=1−p1

Dividing the numerator and denominator of p1 by es1 gives

p1=11+es2−s1

Let us define the score difference as δ=s1−s2. Then

p1=11+e−δ

This is the sigmoid function. Differentiating with respect to δ,

dp1dδ=e−δ(1+e−δ)2=11+e−δe−δ1+e−δ

The first factor is p1. The second factor is 1−p1. Therefore,

dp1dδ=p1(1−p1)

This derivative describes how sensitive the attention probability is to a small change in the score difference. When both keys receive equal attention,

p1=0.5

and the derivative is

p1(1−p1)=0.25

A small change in the score difference produces a meaningful change in the probability. But suppose one key already receives 99.9% of the attention.

p1=0.999

The derivative becomes

0.999(1−0.999)=0.000999

This is approximately 250 times smaller. The score can change slightly without producing a substantial change in the attention distribution. We say that the softmax distribution has become saturated. This is particularly important during the early stages of training. At initialization, the model has not yet learned which queries should match which keys. If the initial score differences are extremely large, softmax may allocate almost all the attention to a randomly preferred key. The difficulty is not simply that the initial choice is random. Neural networks routinely begin with randomly initialized parameters. The difficulty is that the attention mechanism may already behave as though it is highly certain about that random choice, making it less sensitive to corrective changes in its compatibility scores.

To see this more generally, consider a softmax probability pi.

pi=esiZ

where

Z=∑jesj

Differentiating with respect to its own score, we use the quotient rule.

∂pi∂si=esiZ−esiesiZ2=esiZ(1−esiZ)=pi(1−pi)

If we instead differentiate with respect to a competing score sj, where j≠i, only the denominator changes.

∂pi∂sj=−esiesjZ2=−pipj

We can combine both equations into one expression.

∂pi∂sj=pi(δij−pj)

Here, δij is the Kronecker delta. It equals one when i=j, and zero otherwise. This derivative is another way of seeing the competitive nature of softmax. Increasing a score increases its own probability, while decreasing the probabilities of the other alternatives. The normalization constraint also appears directly in these derivatives. Since the sum of all probabilities is always one,

∑ipi=1

differentiating with respect to any score sj gives

∑i∂pi∂sj=0

Whatever probability is gained by one alternative must be lost collectively by the others.

How gradients flow through attention

Let us now connect these derivatives back to the actual information retrieved by the attention head. Recall that

o=∑ipivi

If we change one compatibility score sj while treating the values as fixed, the output changes according to

∂o∂sj=∑ivi∂pi∂sj

Substituting the softmax derivative,

∂o∂sj=∑ivipi(δij−pj)

We can separate the two parts.

∂o∂sj=pjvj−pj∑ipivi

But the sum in the second term is simply the current attention output o. Therefore,

∂o∂sj=pj(vj−o)

This equation deserves particular attention. It gives us a direct geometric interpretation of what changing an attention score does to the retrieved information. The vector vj−o points from the current attention output towards value vector vj. Consequently, increasing the compatibility score of key j moves the attention output towards its associated value. The strength of this movement is controlled by pj. If a value receives almost no attention, changing its score slightly may have little immediate effect on the output. If the output is already almost identical to a heavily attended value, then vj−o is small. Further increasing that value's score also has little effect.

An extremely concentrated attention distribution can therefore become difficult to adjust through small changes in its scores. We can make the learning interpretation even more explicit. Suppose the derivative of the overall training loss with respect to the attention output is

g=∂L∂o

Using the chain rule,

∂L∂sj=pjg⊤(vj−o)

This tells us how the learning signal reaches the compatibility scores. Gradient descent attempts to change the scores so that the retrieved information moves in a direction that reduces the training loss. If moving towards a particular value would improve the computation, the corresponding score can receive a learning signal that increases its influence. If moving towards a value would make the computation worse, its score can receive a learning signal that decreases its influence. In other words, the model learns how to search for information partly by observing the consequences of the information it retrieves.

The query and key projections are adjusted according to whether their resulting retrieval patterns help the model accomplish its training objective. This completes an important conceptual loop. The queries and keys produce compatibility scores. Softmax transforms those scores into a distribution of influence. The distribution determines which values are combined. The resulting information affects the training loss. Gradients flow backwards through that computation and modify the query and key projections. The model gradually learns what to retrieve by learning from the consequences of its retrieval decisions.

#dl-notes