The Softmax Function
Softmax converts any list of numbers (logits) into probabilities that sum to 1. It's the bridge between raw model scores and interpretable outputs.
Interactive: Type logits, see probabilities
Enter comma-separated numbers (logits) to see the softmax output. Try values like 2, 1, 0.1 or 5, 5, 5 (equal inputs = equal probabilities).
Optional labels (comma-separated):
Bar chart
Key properties
Cross-Entropy Loss
The loss function that tells the model how wrong its predictions are. Lower is better. Used in almost every classification and language model.
Intuition
Imagine the correct next token is "cat" (probability should be 1.0). Cross-entropy measures how surprised the model is by the truth.
Interactive: Predicted vs Actual
Drag the slider to change the model's predicted probability for the correct class.
The curve shows -log(p): loss explodes as probability approaches 0. The model is harshly punished for being confidently wrong.
Gradient Descent & Backpropagation
How neural networks learn: compute the loss, figure out which weights caused it (backprop), then nudge them in the right direction (gradient descent).
The training loop
compute predictions
cross-entropy
compute gradients
w = w - lr * grad
Gradient Descent: The Ball on a Hill
Imagine standing on a hilly landscape where elevation = loss. You want to reach the lowest point. The gradient tells you which direction is "uphill," so you step the opposite way.
learning_rate = step size (too big: overshoot, too small: slow)
Click to place the ball, then watch it roll downhill via gradient descent. The red dot follows the gradient to the minimum.
Backpropagation: Computing Gradients Efficiently
Backpropagation applies the chain rule of calculus from the loss backward through each layer. It computes how much each weight contributed to the error.
Each layer receives the gradient from above, multiplies by its own local gradient (chain rule), and passes the result down. This way every weight knows its contribution to the final loss.
The Chain Rule & Computation Graphs
Backpropagation is just the chain rule applied to a computation graph. Every neural network operation becomes a node in this graph.
Example: y = sigmoid(w*x + b)
input
weight
w*x = 1.0
1.0 + 0.3 = 1.3
bias
sig(1.3) = 0.786
Chain rule in action (backward pass)
To compute dL/dw, we chain the derivatives from the loss back to w:
where z = w×x + b, y = sigmoid(z)
② dz/dw = x = 2.0
③ dL/dw = dL/dy × 0.168 × 2.0 = dL/dy × 0.336
Each node in the graph only needs to know its own local derivative. Backpropagation chains them together automatically. This is why frameworks like PyTorch can compute gradients for arbitrarily complex models.
A model like LLaMA 3 70B has billions of parameters. The computation graph has millions of nodes. Backpropagation computes all gradients in one backward pass (same cost as the forward pass), making training feasible.
Activation Functions: ReLU, GELU, SwiGLU
Non-linear functions applied after linear transformations. Without them, stacking layers would be pointless — a stack of linear layers is just one big linear layer.
Interactive: Compare activation functions
Hover over the graph to see input/output values. Toggle functions on/off.
Simplest. Zero for negatives, identity for positives. Fast but "dead neurons" can occur (gradient = 0 for x < 0).
Used in: Earlier CNNs, some feed-forward layers
Smooth approximation of ReLU. Allows small negative values through. Probabilistic interpretation: gates by how likely x is positive under a Gaussian.
Used in: GPT-2, BERT, many modern models
Combines Swish activation with a gating mechanism (GLU). Uses two linear projections — one is activated, one is a gate. State-of-the-art in modern LLMs.
Used in: LLaMA, Mistral, Gemma, PaLM
KL Divergence
Measures how one probability distribution differs from another. Crucial for knowledge distillation, VAEs, and understanding cross-entropy.
Interactive: Compare two distributions
Adjust the Q distribution sliders to see how KL divergence changes. P (true distribution) is fixed.
P (true): [0.7, 0.2, 0.1]
Fixed reference distribution
Blue bars: P (true). Red bars: Q (model). When they match, KL = 0.
The Jacobian Matrix
When a function takes a vector in and produces a vector out, the Jacobian captures all partial derivatives at once. It's the multi-dimensional version of a derivative.
What it looks like
For a function f: R^n -> R^m, the Jacobian is an m x n matrix of partial derivatives:
Row i, column j tells you: "How much does output i change when input j wiggles?"
Concrete example: Softmax Jacobian
For softmax with 3 inputs, the Jacobian is 3x3. Each entry tells you how changing one logit affects each output probability.
Diagonal (green): increasing logit i increases probability i. Off-diagonal (red): increasing logit i decreases other probabilities (they must sum to 1).
During backpropagation, the gradient at each layer is multiplied by that layer's Jacobian. If Jacobian entries are consistently less than 1, gradients shrink (vanish). If greater than 1, they explode. This is why activation functions, normalization, and residual connections are so carefully designed.
Eigenvalues & Dimensionality Reduction
Eigenvectors point along the directions in your data; each one's eigenvalue (a scalar) tells you how much information that direction carries. PCA uses them together to compress high-dimensional data while preserving the important structure.
Interactive PCA Visualization
The blue dots are 2D data points. The colored arrows show the principal components — the directions of maximum variance. The longer the arrow, the larger the eigenvalue.
Click "New Data" to generate a different distribution and see how the principal components change.
How it works
When you see 2D t-SNE or PCA plots of embeddings, they're projecting 768+ dimensional vectors down to 2D using these techniques. The eigenvalues tell you how much information is lost. Tools like LoRA also exploit low-rank structure — many weight matrices have a few dominant eigenvalues, meaning most of their "knowledge" lives in a low-dimensional subspace.
How Residual Connections Help Gradients
Without residual (skip) connections, deep networks can't train. The gradient signal dies before reaching early layers. Residuals provide a highway for gradients.
Gradient magnitude through layers
This chart shows how gradient magnitude changes as you go deeper into a network. Without residuals, gradients shrink exponentially. With residuals, they stay healthy.
Why it works
Number Formats: FP32, FP16, BF16, INT8, INT4
Every weight, activation, and gradient in an LLM is a number stored in a specific format. The format you choose determines memory usage, speed, and whether training converges at all.
How floating point numbers work
A floating point number has three parts: sign (positive or negative), exponent (the scale/magnitude), and mantissa (the precision). More bits = more precision and range, but more memory.
Full precision. Used for optimizer states (Adam momentum/variance). Range: ±3.4×10³⁸, ~7 decimal digits of precision.
More precision, less range. Range: ±65,504. Can overflow during training if gradients spike. Good for inference weights.
Same range as FP32, less precision. Range: ±3.4×10³⁸. Truncated FP32 — just drop the bottom 16 mantissa bits. Preferred for training because it never overflows.
Integer, not floating point. 8 bits stored in two's complement (no separate sign bit — the top bit carries weight -128). Range: -128 to 127. Requires calibration (scale + zero-point). 4× smaller than FP32. Good for inference with minimal quality loss.
Only 16 possible values (4 bits, no separate sign bit). NF4 (QLoRA) uses a normal distribution to pick 16 optimal quantization levels. 8× smaller than FP32. Used for QLoRA and aggressive inference compression.
When to use each format
| Format | Bits | Size (7B model) | Precision | Best for |
|---|---|---|---|---|
| FP32 | 32 | 28 GB | ~7 digits | Optimizer states (Adam), loss accumulation |
| BF16 | 16 | 14 GB | ~2 digits | Training (forward/backward pass) — same range as FP32 |
| FP16 | 16 | 14 GB | ~3 digits | Inference weights, mixed-precision training with loss scaling |
| INT8 | 8 | 7 GB | 256 levels | Post-training quantization for inference |
| INT4 / NF4 | 4 | 3.5 GB | 16 levels | QLoRA fine-tuning, aggressive compression |
Why BF16 won the training format war
FP16 has only 5 exponent bits — its max value is 65,504. During training, gradient magnitudes can exceed this, causing overflow → NaN → training crash. BF16 has 8 exponent bits (same as FP32) so it handles the same range of magnitudes. The tradeoff: BF16 has less precision (7 mantissa bits vs FP16's 10), but in practice neural networks are robust to this — the gradient noise during training is larger than the precision loss.
Mixed precision training: Forward and backward pass in BF16 (fast, memory-efficient), but optimizer states and loss accumulation in FP32 (precise). This gives you the speed of 16-bit with the stability of 32-bit. Used by every modern LLM training run.
Interactive: See what precision loss looks like
Type a number and see how each format represents it. Notice how FP16 and BF16 round differently, and how INT8 snaps to the nearest of 256 levels.
Walkthrough: Mixed precision training pipeline
Every modern LLM training step uses multiple formats simultaneously. Click each stage to see what format is used and why:
What is quantization?
Quantization reduces the number of bits used to store each weight, trading precision for smaller model size and faster inference. It's like rounding — you lose some detail, but the model still works surprisingly well.
4 bytes per weight
1 byte per weight (4× smaller)
Interactive: Quantization quality loss
Drag the slider to see how reducing bits affects a set of model weights. The red bars show the error between original and quantized values.