∑∫∇π∞λθΣ∂√
Interactive Visualizer

Math is All You Need

Generative AI feels like magic, but underneath it's linear algebra, calculus, probability theory, and statistics. Explore each layer of how models like GPT, Claude, and Gemini actually work — from raw text to generated output.

The GenAI Pipeline

Text→Tokenize→Embed→Attention→Neural Net→Probabilities→Next Token

Every step above is powered by mathematics. Click “Start Exploring” to see each one in detail.

01Set Theory & Encoding

Tokenization

Turning words into numbers

Tokens (6)Each colored block = 1 token
“The”#464
“ cat”#3797
“ sat”#3290
“ on”#319
“ the”#262
“ mat”#2616
Text (Human)
“The cat sat on the mat”
→
Token IDs (Machine)
[464, 3797, 3290, 319, 262, 2616]

How it works: Before any math happens, text must be converted to numbers. Tokenizers split text into subword pieces using algorithms like Byte-Pair Encoding (BPE). Each token maps to an integer ID — this is the model's actual input. A typical LLM vocabulary has 50,000-100,000 tokens.

02Linear Algebra

Word Embeddings

Words as vectors in space

cat
dog
fish
bird
king
queen
prince
princess
algebra
calculus
geometry
statistics
computer
software
algorithm
Dimension 1
Dimension 2
Sample embedding vector for “cat” (768 dimensions, showing first 8):
0.234-0.8910.1560.723-0.4450.612-0.0890.934... 760 more
Vector Arithmetic (the magic of embeddings):
king - man + woman = queen
Vector arithmetic captures semantic relationships. The direction from 'man' to 'woman' is similar to 'king' to 'queen'.
paris - france + japan = tokyo
The 'capital-of' relationship is encoded as a consistent vector direction in embedding space.

How it works: Each token is mapped to a high-dimensional vector (e.g., 768 or 4096 dimensions). Words with similar meanings cluster together. These vectors are learned during training — the model adjusts them so that the geometric relationships between vectors capture semantic relationships between words. This is pure linear algebra.

03Matrix Multiplication & Softmax

Self-Attention

How tokens relate to each other

Click a token to see what it “attends to”:
Attention Matrix (Self-Attention Heatmap)
Q \ KThecatsatonthemat
The
80
5
5
3
4
3
cat
10
50
15
5
5
15
sat
5
35
40
10
2
8
on
3
10
15
50
12
10
the
30
2
3
15
40
10
mat
2
5
8
15
10
60

How it works: Self-attention lets each token “look at” every other token and decide how much to focus on each one. It's computed via matrix multiplication and softmax (a probability function). This is the core innovation of the Transformer architecture that powers GPT, Claude, and all modern LLMs.

04Linear Algebra & Calculus

Neural Network Layers

The computational backbone

Watch data flow through the network:
Input
Hidden 1
Hidden 2
Hidden 3
Output
Each layer performs a matrix multiplication + activation:
input
[4×1]
×
weights
[6×4]
+
bias
[6×1]
→ReLU(·)→
output
[6×1]
y = ReLU(Wx + b)
GPT-2
124M
parameters
GPT-3
175B
parameters
GPT-4
~1.8T
parameters
Claude
???
parameters

How it works: A neural network is layers of matrix multiplications followed by non-linear activation functions. Each “parameter” is just a number in a weight matrix. GPT-4 has ~1.8 trillion of these numbers — all learned through training. The “intelligence” is entirely encoded as matrices of floating-point numbers.

05Probability Theory & Statistics

Next Token Prediction

Generating text one token at a time

Prompt:
The cat sat on the|
Next token probability distribution:
mat
48.4%
floor
18.7%
couch
10.5%
bed
7.0%
table
4.9%
roof
2.2%
chair
2.2%
ground
1.4%
rug
0.8%
...
3.9%
Temperature: 0.7
0 (Greedy)
Always picks the most likely token
0.7 (Balanced)
Good mix of quality & creativity
1.5 (Creative)
More random, surprising outputs
The Softmax Function (converts scores to probabilities):
P(tokenᵢ) = e^(zᵢ/T) / Σ e^(zⱼ/T)
Where z = raw logits (model output), T = temperature, and e = Euler's number (2.718...). This ensures all probabilities are positive and sum to 1.

How it works: The model outputs a score (logit) for every possible next token. The softmax function converts these to a probability distribution. Temperature controls randomness — lower values make the model more deterministic, higher values more creative. The next token is then sampled from this distribution. It's all probability theory.

06Calculus & Optimization

Training Loop

How models learn from data

Epoch: 0Loss: 4.500
Training Loss over Time
Learning Rate: 0.0010
Slow & Stable (0.0001)Fast & Risky (0.01)
→1. Forward Pass

Feed data through the network (matrix multiplications)

△2. Compute Loss

Measure how wrong the prediction is (cross-entropy loss)

←3. Backpropagation

Compute gradients using the chain rule (calculus!)

↻4. Update Weights

Adjust parameters to reduce loss (gradient descent)

Gradient Descent Update Rule:
θ ← θ - α · ∇L(θ)
θ = model parameters (weights), α = learning rate, ∇L = gradient of the loss function. Repeat billions of times across trillions of tokens.

How it works: Training is an optimization loop powered by calculus. The model makes a prediction, measures the error (loss), then uses backpropagation (chain rule from calculus) to figure out how to adjust each of its billions of parameters. This repeats millions of times on massive datasets. Training GPT-4 reportedly cost >$100M in compute.

At the End of the Day, It's All Math

Every “intelligent” response from an AI model is the result of billions of floating-point numbers being multiplied, added, and transformed through mathematical functions.

Linear algebra gives us the computational framework. Calculus lets us optimize. Probability & statistics make predictions possible. Information theory tells us what to optimize for.

AI didn't replace math — math created AI.

[ ]Linear Algebra

Matrix multiplications power every layer. Embeddings, attention, and weights are all matrices and vectors.

∫Calculus

Backpropagation uses the chain rule to compute gradients. Without calculus, models can't learn.

P(x)Probability & Statistics

Softmax, sampling, cross-entropy loss — the model outputs and learns through probability distributions.

∇Optimization Theory

Gradient descent, Adam optimizer, learning rate schedules — finding the minimum of the loss landscape.

“Mathematics is the queen of the sciences.”

— Carl Friedrich Gauss

The greatest thing is not AI — it's the mathematics that makes AI possible. Every breakthrough in AI is fundamentally a breakthrough in applied mathematics.