Famous machine-learning equations, decoded term by term, with shapes, a worked example and a few lines of code.
Vaswani et al., “Attention Is All You Need” (2017)
Attention(Q,K,V)=softmax(dkQK⊤)V Bridle (1990) named it; used by every classifier and attention layer since
softmax(z)i=∑j=1Kezjezi Standard; the negative log-likelihood of a categorical model
L(θ)=−N1i=1∑Nk=1∑Kyk(i)logy^k(i) Kingma and Ba, “Adam: A Method for Stochastic Optimization” (2015)
gtmtvtm^tθt=∇θft(θt−1)=β1mt−1+(1−β1)gt=β2vt−1+(1−β2)gt2=1−β1tmt,v^t=1−β2tvt=θt−1−αv^t+ϵm^t Ba, Kiros and Hinton, “Layer Normalization” (2016)
LayerNorm(x)=γ⊙σ2+ϵx−μ+β,μ=d1i=1∑dxi,σ2=d1i=1∑d(xi−μ)2 Zhang and Sennrich, “Root Mean Square Layer Normalization” (2019); used in LLaMA and most recent LLMs
RMSNorm(x)=RMS(x)x⊙g,RMS(x)=d1i=1∑dxi2+ϵ Kingma and Welling, “Auto-Encoding Variational Bayes” (2014)
logpθ(x)≥Eqϕ(z∣x)[logpθ(x∣z)]−DKL(qϕ(z∣x)∥p(z)) Kullback and Leibler (1951)
DKL(P∥Q)=x∑P(x)logQ(x)P(x) Rumelhart, Hinton and Williams, “Learning representations by back-propagating errors” (1986)
zδ∂W∂L=Wx+b,a=σ(z)=∂z∂L=∂a∂L⊙σ′(z)=δx⊤,∂b∂L=δ,∂x∂L=W⊤δ Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models” (2021)
h=W0x+ΔWx=W0x+rαBAx,B∈Rd×r, A∈Rr×k, r≪min(d,k) Hendrycks and Gimpel, “Gaussian Error Linear Units (GELUs)” (2016)
GELU(x)=xΦ(x)≈0.5x(1+tanh[2/π(x+0.044715x3)]) Srivastava et al., “Dropout: A Simple Way to Prevent Neural Networks from Overfitting” (2014)
r(l)∼Bernoulli(p),y~(l)=r(l)⊙y(l) Ioffe and Szegedy, “Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift” (2015)
μBx^iyi=m1i=1∑mxi,σB2=m1i=1∑m(xi−μB)2=σB2+ϵxi−μB=γx^i+β≡BNγ,β(xi) Su et al., “RoFormer: Enhanced Transformer with Rotary Position Embedding” (2021); used in LLaMA and most recent LLMs
qm⊤kn=(RΘ,mdWqxm)⊤(RΘ,ndWkxn)=xm⊤Wq⊤RΘ,n−mdWkxn,θi=10000−2(i−1)/d Vaswani et al., “Attention Is All You Need” (2017)
MultiHead(Q,K,V)where headi=Concat(head1,…,headh)WO=Attention(QWiQ,KWiK,VWiV) Schulman et al., “Proximal Policy Optimization Algorithms” (2017)
LCLIP(θ)=E^t[min(rt(θ)A^t, clip(rt(θ),1−ϵ,1+ϵ)A^t)],rt(θ)=πθold(at∣st)πθ(at∣st) Rafailov et al., “Direct Preference Optimization: Your Language Model is Secretly a Reward Model” (2023)
LDPO(πθ;πref)=−E(x,yw,yl)∼D[logσ(βlogπref(yw∣x)πθ(yw∣x)−βlogπref(yl∣x)πθ(yl∣x))] Williams, “Simple statistical gradient-following algorithms for connectionist reinforcement learning” (1992); Sutton et al., policy gradient theorem (2000)
∇θJ(θ)=Eτ∼πθ[t=0∑T−1∇θlogπθ(at∣st)Gt],Gt=k=t+1∑Tγk−t−1rk Bellman, “Dynamic Programming” (1957); this expectation form for a fixed policy is the standard one in reinforcement learning
vπ(s)=a∑π(a∣s)s′,r∑p(s′,r∣s,a)[r+γvπ(s′)] Watkins, “Learning from Delayed Rewards” (PhD thesis, 1989); Watkins and Dayan, “Q-learning” (1992)
Q(st,at)←Q(st,at)+α[rt+1+γamaxQ(st+1,a)−Q(st,at)] Oord, Li and Vinyals, “Representation Learning with Contrastive Predictive Coding” (2018); this temperature form as written in MoCo (He et al., 2020)
LInfoNCE=−log∑i=0Kexp(q⋅ki/τ)exp(q⋅k+/τ) Ho, Jain and Abbeel, “Denoising Diffusion Probabilistic Models” (2020), building on Sohl-Dickstein et al. (2015)
q(xt∣xt−1)=N(xt;1−βtxt−1,βtI),q(xt∣x0)=N(xt;αˉtx0,(1−αˉt)I),αˉt=s=1∏t(1−βs) Ho, Jain and Abbeel, “Denoising Diffusion Probabilistic Models” (2020)
Lsimple(θ)=Et,x0,ϵ[ϵ−ϵθ(αˉtx0+1−αˉtϵ,t)2] Kingma and Welling, “Auto-Encoding Variational Bayes” (2014)
z=μ+σ⊙ϵ,ϵ∼N(0,I) Goodfellow et al., “Generative Adversarial Nets” (2014)
GminDmaxV(D,G)=Ex∼pdata(x)[logD(x)]+Ez∼pz(z)[log(1−D(G(z)))] Loshchilov and Hutter, “Decoupled Weight Decay Regularization” (2019)
θt=θt−1−ηt(αv^t+ϵm^t+λθt−1) Loshchilov and Hutter, “SGDR: Stochastic Gradient Descent with Warm Restarts” (2017)
ηt=ηmin+21(ηmax−ηmin)(1+cos(TiTcurπ)) Jelinek et al. (1977), for speech recognition; the standard metric for language models since
PPL(X)=exp(−t1i=1∑tlogpθ(xi∣x<i)) Standard statistics; Cox, “The Regression Analysis of Binary Sequences” (1958)
P(y=1∣x)=σ(w⊤x+b)=1+e−(w⊤x+b)1 Standard statistics; least squares goes back to Legendre (1805) and Gauss (1809)
β^=(X⊤X)−1X⊤y Standard linear algebra (Beltrami 1873, Jordan 1874); Eckart and Young (1936) for low-rank approximation
A=UΣV⊤=i=1∑rσiuivi⊤ Pearson (1901); Hotelling (1933)
w1=∥w∥2=1argmaxw⊤Sw,S=n−11X⊤X=VΛV⊤,Z=XVk