Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Reference

Formula sheet

Keep the conditions and units beside each equation.

1

Probability and estimation

Quantity Formula Conditions
Conditional probability P(A∣B)=P(A∩B)/P(B)P(A\mid B)=P(A\cap B)/P(B) P(B)>0P(B)>0
Sequence probability ∏i=1TP(ti∣t<i)\prod_{i=1}^T P(t_i\mid t_{<i}) Condition on the prompt if given. Include stop for a completed sequence.
Bayes’ rule P(B∣A)=P(A∣B)P(B)/P(A)P(B\mid A)=P(A\mid B)P(B)/P(A) P(A)>0P(A)>0 and the stated right-hand conditional is defined.
Bernoulli maximum-likelihood estimate p^=k/n\hat p=k/n n>0n>0 independent trials share the same probability.
Estimated standard error s/ns/\sqrt n Independent observations with common distribution and n≥2.n\ge 2\mathclose{\text{.}}
2

Information and loss

Quantity Formula Conditions
Entropy H(p)=−∑ipiln⁡piH(p)=-\sum_i p_i\ln p_i Sum over positive pi.p_i\mathclose{\text{.}}
Cross-entropy H(p,q)=−∑ipiln⁡qiH(p,q)=-\sum_i p_i\ln q_i Average over positive pi.p_i\mathclose{\text{.}} Infinite if any such qiq_i is zero.
KL divergence DKL(p∣q)=H(p,q)−H(p)D_{\rm KL}(p|q)=H(p,q)-H(p) Nonnegative. Zero exactly for equal distributions.
Token negative log-likelihood ℓ=−∑iln⁡q(ti∣t<i)/N\ell=-\sum_i\ln q(t_i\mid t_{<i})/N Use the same mask for the sum and count N>0.N>0\mathclose{\text{.}}
Perplexity eℓe^\ell Use natural logarithms in ℓ.\ell\mathclose{\text{.}}
Temperature odds e(zi−zj)/Te^{(z_i-z_j)/T} T>0.T>0\mathclose{\text{.}}
3

Optimisation and arithmetic

Quantity Formula Conditions
Mean half-squared-loss gradient XT(Xw−y)/nX^\mathsf T(Xw-y)/n One example per row of X.X\mathclose{\text{.}}
Quadratic stability 0<η<2/λmax⁡(H)0<\eta<2/\lambda_{\max}(H) Positive definite Hessian HH of a quadratic loss, fixed step.
Mini-batch gradient covariance Σ/b\Sigma/b bb independent uniform samples with replacement, fixed parameters.
Log softmax (zj−m)−ln⁡∑iezi−m(z_j-m)-\ln\sum_i e^{z_i-m} m=max⁡izi,m=\max_i z_i\mathclose{\text{,}} finite logits and nonempty input.
Dot-product quantisation error bound s2∥x∥1\tfrac{s}{2}\lVert x\rVert_1 Unchanged input x,x\mathclose{\text{,}} common scale s,s\mathclose{\text{,}} no clipping, exact arithmetic.