Skip to content
mlmentorship

Defend an ML derivation when its assumptions change

A strong derivation states assumptions, exposes the key identity, checks the result, and explains what it means for model behavior.

Published · 10 min read ·Core ·Advanced

Math / derivation · active recall

Practice before you read

15 minutes. State assumptions, derive cleanly, check dimensions or limiting cases, then interpret the result.

Math / derivation · closed-book attempt

Defend an ML derivation when its assumptions change

State assumptions, derive cleanly, check dimensions or limiting cases, then interpret the result.

Problem

Context. The fixture below defines four-dimensional query and key vectors using independent random signs, then changes their within-vector dependence. Task. Find the unscaled and scaled dot-product variances in both cases, showing the assumptions needed at each step. Explain what the result does and does not establish about attention scaling in a learned model. Constraints. Use the specified zero-mean sign distributions rather than assuming arbitrary learned coordinates are independent. Check covariance terms when independence changes; a familiar formula is not a substitute for the derivation. Let $d=4$ and $z=q^Tk$. All eight coordinates of $q$ and $k$ independently take $+1$ or $-1$, each with probability $1/2$. Find the variance of $z$ and of $z/\sqrt d$. Then decide whether the same conclusion holds when every query coordinate shares one sign and every key coordinate shares another.

15:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

Reading depth

Derive an attention-score variance from stated random-variable assumptions and test whether the reasoning survives a change in dependence.

Problem

Context. The fixture below defines four-dimensional query and key vectors using independent random signs, then changes their within-vector dependence.

Task. Find the unscaled and scaled dot-product variances in both cases, showing the assumptions needed at each step. Explain what the result does and does not establish about attention scaling in a learned model.

Constraints. Use the specified zero-mean sign distributions rather than assuming arbitrary learned coordinates are independent. Check covariance terms when independence changes; a familiar formula is not a substitute for the derivation.

Let and . All eight coordinates of and independently take or , each with probability . Find the variance of and of . Then decide whether the same conclusion holds when every query coordinate shares one sign and every key coordinate shares another.

Picture

Each product is another independent sign: mean zero, variance one. Four independent products add variance; halving their sum multiplies variance by one quarter.

An exact four-coordinate distribution

Square-root scaling stabilizes variance only under the stated dependence assumptions

Square-root scaling removes the dimension factor under the assumptionsWith independent, unit-variance coordinate products, a 64-term dot product has variance 64. Dividing by 8 returns variance 1; dependence can change this result.Variance is the square of the signalscaleSD 8Variance 64SD 1Variance 1Divide SD by eight; divide area bysixty-four.This calculation requires independentunit-variance products.
Derivation
Parallel trace of an attention logit and its variance Four query and four key coordinates are independent signs with equal positive and negative probabilities. Each product has variance one. The top row follows four products into their sum z and then z divided by two. The aligned bottom row follows variance one per product, total variance four, and scaled variance one. The scale is squared when computing variance. Sharing one query sign and one key sign across coordinates instead gives raw variance sixteen and scaled variance four. ASSUMPTIONS 4 query signs + 4 key signs: all independent Each sign: +1 or −1, each with probability 1/2 1 - PRODUCTS 2 - SUM 3 - SCALE RANDOM VARIABLE X_i = q_i k_i 4 terms z = sum X_i add products z / 2 scaled logit VARIANCE Var(X_i) = 1 each term Var(z) = 4 sum 4 ones (1/4) × 4 Var = 1 DECISIVE MOVE: SQUARE THE SCALE FACTOR (1/2)^2 × 4 = 1 CHECK: d = 1 needs no scaling SHARED SIGNS: Var(z) = 16; Var(z/2) = 4
Read it this way: follow the random variable and its variance together. Scaling the variable by one half scales variance by one quarter, not one half.
Check: Why does dividing by 2 yield variance 1 for independent terms but variance 4 for shared terms?
Read it this way: With independent, unit-variance coordinate products, a 64-term dot product has variance 64. Dividing by 8 returns variance 1; dependence can change this result.
Why does dividing the dot product by eight divide its variance by sixty-four?

References: Vaswani et al. and PyTorch scaled-dot-product attention.

Worked example: attention scaling

The decisive identity retains covariance until the assumptions remove it:

Here the covariance terms vanish, so and . The equally likely sign assignments give an exact check:

Assignment count

Check also : no scale change is needed. Dividing variance by would incorrectly give two at ; variance squares the multiplier.

Now set every and every , with independent signs . All products equal , so has variance 16. Six pair covariances contribute beyond the four individual variances. Dividing by two leaves variance four.

Change the assumptions or object

1. Change coordinate variances

New task: independent zero-mean query coordinates have variance four; independent key coordinates have variance nine.

Picture and change: retain independence of vectors and products, but each product now has variance . For , the sum has variance 144.

Check: dividing by two leaves variance 36. Dividing by restores one in this stylized distribution.

Use when: testing the unit-variance premise, not claiming exact whitening of trained heads.

2. Average correlated worker errors

New task: average four unit-variance errors with pairwise correlation .

Picture and change: apply the same covariance identity to their average.

Check: independence gives ; perfect correlation gives one.

Use when: shared worker errors invalidate independent-noise averaging.

Transfer: recover a missing identity

You forget the importance-sampling identity, and the proposal misses some target support. What can you derive safely?

Check your answer

Start from the target integral and insert only where . Identify any missing contribution; support mismatch can block equality. Large weights or an effective-sample-size number cannot recover omitted support.

Use the same four-part structure

1. Setup

Define variables, dimensions, probability direction and assumptions. For gradients, state the differentiated variable and row/column convention.

2. Key identity

Expose the decisive step: chain rule, log-ratio expansion, log-sum-exp derivative, Jensen’s inequality or a variance identity. Justify it rather than hiding it behind a name.

3. Sanity checks

Use at least two: dimensions, sign, equality case, scalar case, limit, symmetry or numerical scale.

4. Interpretation

Explain the model or system consequence, then change an assumption and identify what survives.

Coordinate independence is sufficient, not necessary, for the attention result. For independent zero-mean vectors with covariance matrices :

This conditional result does not establish universal softmax saturation or training stability. Learned projections and normalization alter empirical head distributions.

A remembered formula is not a proof

“It prevents large values” does not justify or identify when the variance calculation changes.

Eight proof checkpoints

These are separate derivations connected by a presentation protocol, not one common mathematical proof.

PromptDecisive step and check
Softmax cross-entropyLog-sum-exp differentiation gives for normalized targets; gradients sum to zero; common logit shifts preserve probabilities.
KL direction; check support, equality and asymmetry.
ELBO, when well-defined; equality uses the exact posterior.
L1 versus L2$
Geometric attemptsIndependent identical success trials counted from one give ; gives one attempt.
AveragingIndependent errors give ; equal pair correlation gives .
Importance samplingInsert in the target integral only where the proposal supports the contributing integrand.
AttentionKeep covariance terms until assumptions justify removing them; square any scale factor in variance.

For Bernoulli parameters and , the KL is about nats; reversing direction gives . The unequal values check asymmetry, not an error in the expectation direction. At geometric success probability zero, success never occurs and the expectation is not finite. Importance weights can have infinite variance despite a valid support identity. Effective sample size is a diagnostic, not a finite-variance or support certificate.

Which conclusion survives?

Independence, convexity, asymptotics, unbiasedness and support overlap are different premises. State which identity uses each one. The ELBO gap concerns posterior approximation, not an unspecified reconstruction-plus-regularization slogan.

Evidence in a derivation

Defined notation, justified identities, two checks and explicit error recovery make the reasoning inspectable. No format guarantees a hiring outcome.

Errors to expose

Undefined notation, missing support and unjustified algebra remain errors even when the final formula looks familiar. Stop at the missing step rather than inventing one.

Common follow-up

“You cannot remember the identity needed for the next step. What do you do?”

State what is known, derive the missing piece from a definition or simpler case, and identify the exact unresolved step.

Use the timed math oral with an observer and a changed-assumption follow-up.

Related: derive logistic regression, softmax and cross-entropy, and KL divergence.