The functions that run the machine

October 8, 2026 · math · llms

Every AI model you have ever used is, underneath the hype, about ten math functions applied a few billion times. Squash a number here. Split a pie there. Measure how surprised you were. That is the whole game, and this post makes each function something you can touch.

People talk about neural networks like they are mysterious. They are not. A network is a long chain of very ordinary operations, and the interesting parts are just a handful of functions with funny names: sigmoid, softmax, ReLU, GELU, LayerNorm, cosine similarity. Learn these six families and you can read any model paper without flinching.

Each section below gives you one analogy and one toy, and we start at section zero, because one idea underpins half of what follows: the logarithm. Drag the sliders, spin the dials, and watch what each function actually does. By the end you will know exactly what is happening inside a transformer block, an embedding search, and a game-playing agent.

0. Logarithms, from scratch

Before any of the functions below, you need one idea: the logarithm. It is older than calculus and older than the steam engine. A Scottish astronomer invented it in 1614 because he was sick of multiplying big numbers by hand. Four hundred years later it is the reason your chatbot can learn at all. Here is the whole thing, built up from zero.

A logarithm is just a counter. log10(1000) = 3 means: multiply 10 by itself 3 times and you get 1000. That is all it is. The log asks one question: how many multiplications deep are you?

The multiplication counter

Pick a base, then step the counter. The step count IS the logarithm.

Multiplications 3

How Napier discovered it. In 1614, John Napier was an astronomer drowning in arithmetic. Multiplying two 7-digit numbers by hand took all afternoon, and one slip ruined everything. Then he noticed something: if you write numbers as powers of a base, multiplication becomes addition. 10² × 10³ = 10⁵. So instead of multiplying the numbers, you add the exponents and look the answer up. He published tables of these exponents, logarithms, and cut calculation time to a fraction. It was a machine that turned hard multiplications into easy additions.

Napier's machine

Drag A and B. The machine multiplies them the painful way and the Napier way: add the logs, then un-log. Same answer.

1,554
A × B, the painful way
1554.00
e^(ln A + ln B), the Napier way

Logs compress ridiculous ranges. An earthquake's energy, a sound's loudness, a solution's acidity: all of them span so many zeros that raw numbers are useless. So we invented the Richter scale, decibels, and pH, and all three are just logarithms in disguise. A magnitude 9 quake is not a little stronger than a magnitude 5. It is about a million times stronger, and the log scale is what lets you say that in one syllable. Language models live in the same world: the token probabilities below span twelve orders of magnitude. Flip the scale and watch the linear view crush everything to zero.

The scale zoomer

One row of token probabilities, from certain down to one in a trillion. Toggle the scale.

On the linear scale, six of the seven bars are invisible. They are not zero, your eyes just cannot see twelve orders of magnitude at once. The log scale gives every value its fair share of pixels.

Why base e? Because e is what continuous growth converges to. Put $1 in a bank at 100% interest. Compounded once a year you get $2. Compounded every day you get about $2.71. Compounded every single instant, you approach $2.71828 and never pass it. That number is e: the speed limit of growth. Whenever something grows or decays continuously, e shows up, and calculus is cleanest in base e, because the derivative of $e^x$ is just $e^x$ again. So in ML, a log with no base written always means natural log. It is not a fashion choice. It is the base the math wants.

The speed limit of growth

Step up how often the bank compounds your $1. Watch the total stall out at e.

Compounds per year 1
Your $1 becomes $2.00000

Build e, term by term

Add one term at a time. Each new term is one over n factorial.

Terms added 1
Latest term
1.0000000000
running total
0
correct decimals

$e^x$ is the one function that equals its own derivative and equals 1 at x = 0. Force a power series to obey that, term by term, and every coefficient gets pinned to $1/n!$. The series is not a trick. It is what "derivative equals itself" demands. The compound-interest limit $(1 + 1/n)^n$ also reaches e from first principles, but it crawls. Computers use the series.

Taylor series: fake the function

Pick a function. Drag the terms. The polynomial hugs the curve near zero and wanders off far away.

true curvetaylor polynomial
~0
worst miss near center (|x| < 1)
0
worst miss far away (|x| > 4)

Wait, what IS a Taylor series?

It is a translation. $\sin x$ looks impossible to compute: no plus, no times, just a wave. A Taylor series rewrites it using only plus and times: $a_0 + a_1 x + a_2 x^2 + a_3 x^3 + \cdots$. Those are the only two operations a computer does easily. The series is sin, translated into plain arithmetic.

How it was found. Brook Taylor published it in 1715, building on Newton and Gregory. The question: if you know EVERYTHING about a function at ONE point, its value, its slope, how its slope is changing, and onward, can you rebuild the function nearby? Yes. Each fact pins one coefficient: $a_0$ matches the value, $a_1$ matches the slope, $a_2$ matches the curvature. The $/n!$ shows up because differentiating $x^n$ exactly $n$ times spits out $n!$, so you divide it back out to keep each coefficient honest.

The road trip. Imagine describing a drive from a single instant: where you are (position), how fast you are going (velocity), whether you are speeding up (acceleration), and onward. Know all of that at one moment and you can predict a moment later. More terms, accurate further out. A Taylor series is that snapshot, written in algebra.

Why sin and cos fall out of it

The derivatives of sin run in a cycle. Tap through it:

At $x = 0$ that cycle reads 0, 1, 0, $-1$, 0, 1, and on. So the coefficients become $0, 1, 0, -1/3!, 0, 1/5!, \ldots$ and the sine series is just that cycle written down: $\sin x = x - x^3/3! + x^5/5! - \cdots$. Same story for cos. The series does not invent sin. It reveals what sin is. This is also why $e^{i\theta}$ splits into cos and sin: feed $i\theta$ into the $e^x$ series, sort the pile with $i$ from the pile without it, and the two piles are exactly the sin and cos series.

Other Lego sets

Taylor builds with polynomials, which are great near one point. Fourier series build ANY repeating pattern out of sines and cosines. That is how JPEG squeezes your photos and MP3 squeezes your music: keep the big waves, drop the tiny ones. (The Fourier transform behind your camera, from the day-in-the-life post, is this idea at work.) Geometric series: $1 + x + x^2 + \cdots = 1/(1-x)$, the simplest Lego set of all. The kicker: there is no single best set. Pick the right Lego for the job.

The money demo: why log-softmax, not probabilities. A language model writes one token at a time, and the probability of a whole sentence is every token's probability multiplied together. Each one is small. Multiply enough small numbers and the computer surrenders: the product rounds to exactly zero, and training dies on the spot. Logs fix it, because adding logs is the same as multiplying the originals, and addition never underflows. Slide N up and watch the left column die while the right column cruises.

Watch a probability die

Each token gets the same small probability p. The left column multiplies them, the way probabilities do. The right column adds their logs, the way training does.

0
p multiplied N times UNDERFLOW
-921.0
log-probabilities added N times
Honest answer 10-400

The ladder

Tap a rung. Every operation has a "how many deep" question, and the answer is always one rung down.

Repeat it and you build multiplication
"How many deep?" division

Division is log's analog for addition: "how many +b steps to reach x" is x / b, the same way "how many ×b steps" is the log. Climb one rung and the log steps exponentiation down to multiplication too. At the top, power towers get the super-logarithm, which asks "how many exponentiations deep." The pattern never breaks.

Now that logs make sense, here is why softmax wears a log coat.

1. The squashers: sigmoid and tanh

Neural nets pass huge wild numbers around, and sometimes they need to calm one down. That is what the squashers do. Sigmoid takes any number and squeezes it into a 0-to-1 range. Think of a dimmer switch: any input becomes a brightness level between off and fully on. Tanh is the same idea but for a tug of war: any input becomes a pull between -1 and 1, negative one way, positive the other.

Where they live: inside LSTM memory cells, where sigmoid acts as a gate that decides how much old memory to keep, and in older networks as plain on/off switches. Modern transformers mostly replaced them, but the gate idea never left.

The dimmer and the tug of war

Drag the input. The red dot rides the sigmoid curve, the blue dot rides tanh. Same input, two different squeezes.

0.50
sigmoid: dimmer brightness
0.00
tanh: tug-of-war pull

Notice how both flatten out at the extremes. Push the input to 8 and more input barely changes anything. That flat zone is called saturation, and it is why training can stall: no change in output means no signal to learn from.

One curve becomes the other

Drag the morph. At 0 you have pure sigmoid, 0 to 1. At 100 you have pure tanh, -1 to 1. The exact link is $\tanh(x) = 2\,\sigma(2x) - 1$.

Formula in use sigmoid(x)

Same rope, different markings: sigmoid measures off-to-on, tanh measures full-left to full-right with zero in the middle. Zero-centered matters for training. Sigmoid outputs are always positive, so gradients zigzag instead of heading straight downhill. Tanh averages to zero and converges faster. That is why old RNNs used tanh.

Undo buttons

Drag the input. Sigmoid squashes it into 0 to 1, logit stretches it right back. Watch the round trip land home.

0.82
sigmoid(x): squashed to 0-1
1.50
logit of that: stretched back
Round-trip error 0

They are undo buttons for each other. $\operatorname{logit}(p) = \log\left(\frac{p}{1 - p}\right)$ is the exact inverse of sigmoid. Log-softmax is this same idea grown up for many choices instead of two.

Paint the bell

Drag the dot along the bell curve. The red paint is everyone at or below your x. That painted fraction is the CDF.

0.500
CDF: fraction painted so far
0.000
$\operatorname{erf}\!\left(x / \sqrt{2}\right)$: the same area, renamed

CDF answers "what fraction is at or below x." Erf is that same area wearing a costume: the bell's area cannot be written with normal algebra, so it earned a name. The link is $\Phi(x) = \frac{1}{2}\left(1 + \operatorname{erf}\!\left(\frac{x}{\sqrt{2}}\right)\right)$. And look at the shape: erf is an S-curve from -1 to 1, just like tanh. That is why the GELU tanh approximation works. Tanh is erf's stunt double.

2. The chooser: the softmax family

Often the model must pick: which word comes next, which word to attend to, which action to take. Raw scores are messy, so softmax turns them into a clean split of a pie. Think of splitting a pie where the loudest voice gets the biggest slice: bigger score, bigger slice, and the slices always add to 100%.

Two knobs matter. Temperature is the randomness dial: cold temperature makes the winner take almost everything, hot temperature hands everyone a similar slice. And log-softmax is the Richter scale version of the same split: same information, but measured in logs so the math never underflows when probabilities get microscopic. There is also gumbel-softmax, which is softmax you can learn through, used for agents that sample an action and then improve from the result.

Where it lives: next-token probabilities, attention weights, and anywhere a model must choose.

Split the pie

Drag the scores. The bars show each candidate's slice. Drop the temperature for a ruthless winner, raise it to share the pie.

Slices add to 100.0%

Temperature 0.2 is a dictatorship: the top score takes nearly everything. Temperature 4 is a commune: almost equal slices no matter the scores. Sampling with high temperature is why the same prompt gives different answers each time.

3. The spark: ReLU, GELU, SiLU

After all the careful mixing, each neuron needs a moment of nonlinear mischief, or the whole network collapses into one big boring linear function. That mischief is the activation function. Think of them as bouncers at a club door. ReLU is the strict bouncer: negative numbers get bounced outright, positives walk straight in. GELU and SiLU are velvet-rope bouncers: entry is smooth, small negatives get a little slack instead of a hard no.

Where they live: inside every transformer block, right after the attention and MLP layers. GPT models use GELU. Llama models use SiLU (also called Swish). The smooth ones train a little better, which is why the strict bouncer mostly retired.

Meet the bouncers

Pick a bouncer, then drag the dot along the curve. Negatives on the left, positives on the right.

Input 0.0 0.00

Drag into the negatives with ReLU: flat zero, hard bounce. Switch to GELU or SiLU and the curve dips slightly below zero before recovering. That tiny dip is the velvet rope, and it gives the optimizer a gradient to follow where ReLU gives it nothing.

4. The stabilizers: LayerNorm and RMSNorm

Stack a hundred layers and the numbers drift: some explode, some vanish, and training falls over. Normalization is the fix. Think of a sound mixer resetting every instrument's volume before each song so no single guitar drowns the band. LayerNorm centers the values (subtract the mean) and rescales them (divide by the spread). RMSNorm skips the centering and just rescales, which is cheaper and works just as well.

Where they live: between every sub-layer of every modern model. Transformers without normalization do not train. Full stop.

Reset the mixer

Eight wild input values. Hit normalize and watch the mixer pull them into a sane range.

-
mean
-
spread (std)

LayerNorm lands the mean at 0. RMSNorm leaves the mean alone and only tames the spread. Either way the next layer receives numbers it can actually work with.

5. The measurers: cosine similarity and cross-entropy

Two questions come up constantly. First: how alike are these two things? Embeddings turn words, images, and sentences into arrows in meaning-space, and cosine similarity asks do these two arrows point the same way. 1 means same direction, 0 means unrelated, -1 means opposite. This is the entire engine behind search-by-meaning.

Second: how wrong was the guess? Cross-entropy asks how surprised you were by the right answer. The model gave the correct word 90% probability? Barely surprised, tiny loss. Gave it 2%? Deeply surprised, huge loss. Training is just the long project of being less surprised.

Where they live: cosine similarity powers embedding search and retrieval; cross-entropy is the loss function behind almost all LLM training.

Point the arrows

Rotate each arrow with its slider. The readout is the cosine similarity: 1 when they align, 0 when they are perpendicular.

Cosine similarity 0.50

The surprise dial

The model gave the correct answer some probability. Drag it down and feel the loss explode.

Cross-entropy loss (nats) 0.36

Loss is negative log probability. At 99% the loss is nearly zero. At 1% it is 4.6 nats of pure surprise. Every training step is the model trying to push this number down across billions of guesses.

6. The agent corner: softmax over actions

An agent is a model with a to-do list. At every step it scores its options, what to click, which tool to call, which move to play, and then it must commit. That commit is softmax over actions: the same pie-splitting from section 2, but the slices are things to do. Think of it as the agent's gut feeling, written as probabilities.

Temperature is the personality dial here. Cold agent: always picks its best guess, reliable but predictable. Hot agent: explores, tries weird moves, sometimes discovers something brilliant, sometimes faceplants. Game-playing agents and tool-using agents live on this dial.

Be the agent

Set the scores, pick a temperature, then press Act. The agent samples an action from its gut feeling. Run it twenty times and watch the history.

Last action -

With temperature at 0.2 the history is a boring column of the top action. Crank it to 4 and every action shows up. Real agents often start hot to explore, then cool down to exploit what they learned.

Six functions, and you now know what each one feels like. Next time a paper says "we apply RMSNorm before a SiLU MLP with softmax attention," you will hear: reset the mixer, velvet-rope bouncer, split the pie.

Keep exploring

These functions do not live alone. See them running inside real training, fine-tuning, and retrieval.