Equations · Hu et al., “LoRA: Low-Rank Adaptation of Large Language Models” (2021)
The LoRA formula (W₀ + BA), explained
LoRA fine-tunes a large model by freezing its weights and learning a small low-rank correction to each chosen matrix.
Term by term
The pretrained weight matrix, frozen. The subscript 0 marks it as the original.
Subscript (index or label) · Real numbers (ℝ) and shapes · Bold uppercase (matrix)
The learned update. As a product of a and an matrix, it has rank at most .
The two small trainable matrices. starts random and starts at zero, so training begins from the original model.
The rank is much smaller than the matrix's dimensions: often 4 to 64 against thousands.
Shapes
- , , , matching .
A small worked example
For and : full fine-tuning of the matrix trains million numbers; LoRA trains , about 0.4%. After training, can be added into , so inference costs nothing extra.
In code
import numpy as np
def lora_forward(x, W0, A, B, alpha, r):
return W0 @ x + (alpha / r) * (B @ (A @ x))Every symbol in it
Other equations
- Scaled dot-product attention
- Softmax
- Cross-entropy loss
- Adam update
- Layer normalization
- RMSNorm
- The ELBO
- KL divergence
- Backpropagation through one layer
- GELU
- Dropout
- Batch normalization
- Rotary position embedding (RoPE)
- Multi-head attention
- PPO clipped objective
- DPO loss
- Policy gradient (REINFORCE)
- Bellman equation
- Q-learning update
- InfoNCE contrastive loss
- Diffusion forward process
- DDPM simplified loss
- VAE reparameterization trick
- GAN minimax objective
- AdamW
- Cosine learning-rate schedule
- Perplexity
- Logistic regression
- Normal equation (linear regression)
- Singular value decomposition (SVD)
- Principal component analysis (PCA)