jaxverse
Contents

jaxverse

A little universe of learning machines, designed and built by Neo Mohsenvand.

Chapter 1 · Neural networks · ≈12 min

The Approximator

A neuron is a bump of influence; a layer is a sum of bumps. Watch a tiny network sculpt itself into any curve you draw.

The prologue left you on a landscape. Every knob of a machine became an axis, wrongness became height, and learning became one small move repeated: measure the slope, step downhill. What it never said is what the machine is. This chapter builds one — the smallest one worth building — and then hands its knobs to gradient descent while you watch.

The working part is small enough to hold in your head. A neuron takes a number in, multiplies it by a weight w\htmlClass{eq-model}{w}, adds a bias b\htmlClass{eq-model-2}{b}, and passes the result through a fixed curve called an activation. This book starts with the gentlest activation, the hyperbolic tangent:

a=σ(wx+b),σ(z)=tanh(z)\htmlClass{eq-out}{a} = \htmlClass{eq-op}{\sigma}(\htmlClass{eq-model}{w}x + \htmlClass{eq-model-2}{b}), \qquad \htmlClass{eq-op}{\sigma}(z) = \htmlClass{eq-op}{\tanh}(z)

That is the whole organism. In one dimension its output is a smooth step: flat, a rise, flat again. The weight sets how steep the rise is and which way it faces; the bias slides it along the input. One step is a poor vocabulary — so we hire more. A layer is a row of neurons reading the same x, each with its own weight and bias: a palette of steps, tilted and placed differently. An output layer mixes the palette, scaling each step by an output weight and adding them all up:

f(x)=ivitanh ⁣(wix+bi)+cf(\htmlClass{eq-world}{x}) = \sum_i \htmlClass{eq-model-3}{v_i}\,\htmlClass{eq-op}{\tanh}\!\left(\htmlClass{eq-model}{w_i}\,x + \htmlClass{eq-model-2}{b_i}\right) + c

Subtract one step from a slightly shifted copy and you get a bump. Sums of steps are therefore sums of bumps: little hills of influence you can place, widen, and flip. The universal approximation theorem makes this precise — with enough hidden neurons, f can match any continuous curve on an interval as closely as you like.1 It holds from the other direction too: keep the layer narrow, barely wider than the input, and stack it deep enough, and you get the same guarantee.2

Read either of them honestly, though. They say the right weights exist; they say nothing about how to find them, and “enough” can be an absurd number. And a theorem about what a machine could represent is a weaker thing than it sounds — a large enough lookup table is a universal approximator too, and no use to anyone. The question that decides whether a network is worth building is not which curves it could draw in principle. It is which ones it finds easily, from a random start, by walking downhill. That is the previous chapter's business: write the error down as a loss, and send gradient descent looking.

One neuron, three knobs

Before training anything, get your hands on the unit itself. Below is the same neuron twice: on the left as a circuit — the ultramarine edge carries w\htmlClass{eq-model}{w}, the violet edge carries b\htmlClass{eq-model-2}{b}, and the blue-cyan edge on the way out carries the amplitude v\htmlClass{eq-model-3}{v}, while the teal curve on the dial between them is whichever σ\htmlClass{eq-op}{\sigma} is selected — and on the right as vσ(wx+b)\htmlClass{eq-model-3}{v}\,\htmlClass{eq-op}{\sigma}(\htmlClass{eq-model}{w}x + \htmlClass{eq-model-2}{b}), the one shape it can draw. The third knob, v\htmlClass{eq-model-3}{v}, is just another coefficient — an amplitude that stretches the bend taller or flips it upside down. Move each slider until you can predict what both views will do before you touch it. Then swap σ\htmlClass{eq-op}{\sigma} itself and see which of your intuitions survive: the same three knobs pull very different shapes out of a relu than out of a tanh.

Plate I One neuron a = 1.00 · tanh(3.0x + 0.00)
x+1Σσ = tanhaw 3.0b 0.00v 1.00
−10+1−2−1+1+2
3.0
0.00
1.00
The neuron as a circuit and as a curve. Pick an activation, then move the sliders: the ultramarine weight w sets the bend's steepness, the violet bias b slides it along x, and the blue-cyan amplitude v stretches its height — the teal disk around σ always plots the very curve being bent. Hover the plot to push one x through the circuit and watch it come out as a, in green.

The bend is the whole point

Strip σ\htmlClass{eq-op}{\sigma} out and a neuron is just wx+b\htmlClass{eq-model}{w}x + \htmlClass{eq-model-2}{b} — a line. Stack a hundred all-linear layers and the stack collapses: a line of a line is still a line, so the deepest such network can be multiplied out into a single matrix wearing a hundred costumes. The activation is the only part of the machine that refuses to be linear, and everything a network can do that a line cannot — every curve, corner, and decision — is purchased at that little bend.

Training cares about a second, quieter property: the slope. Gradient descent reaches every weight through the chain rule, and the chain rule multiplies by σ\htmlClass{eq-op}{\sigma'} at each layer it crosses on the way back. The field guide below therefore draws every activation twice — the function solid, its derivative dashed. Wherever the dashed curve hugs zero, learning goes quiet. The two classics saturate at both ends, which is how deep sigmoid networks starved for decades — the vanishing gradient, and a large part of why the field spent those decades shallow.3

relu4 is silent across its entire left half instead, which sounds worse and is not: on the right it never saturates at all, so a gradient can cross ten layers without being multiplied down to nothing. The cost is that a unit driven far enough left stops receiving gradient and is called dead; its leaky cousin5 keeps a trickle flowing on purpose. That half of relu's units are silent at any moment turns out to be part of the appeal rather than a defect — the network computes on a changing, sparse subset of itself.6

The last three — gelu7 , silu8 , mish9 — sand the corner off and keep a little slope slightly below zero, which is one reason the transformer era settled on them. Silu arrived twice: once from researchers building reinforcement learners, and again the same year, out of an automated search over candidate formulas that reported it as a discovery under a different name.10 That is roughly how this corner of the field works. Nobody derives an activation from first principles; they are found, measured, and kept if they win.

None of this changes what networks can express — the universal approximation theorem is not picky about the bend. What it changes is the handwriting of the fit and the temperament of training, and you will see both first-hand in the workshop that follows.

Plate II A field guide to activations σ(z) σ′(z)
Sigmoid 1845
σ(z)=1/(1+ez)\sigma(z) = 1/(1+e^{-z})

squashes to (0, 1) — the original; survives as the gate in LSTMs and GLUs

Tanh 1989
σ(z)=tanhz\sigma(z) = \tanh z

the zero-centered squash — the gentle default this chapter starts with

ReLU 2010
σ(z)=max(0,z)\sigma(z) = \max(0,\,z)

the crease that made deep nets train — cheap, sharp, occasionally dead

Leaky ReLU 2013
σ(z)=max(0.1z,z)\sigma(z) = \max(0.1z,\,z)

a trickle of slope below zero keeps dead units trainable

ELU 2015
σ(z)=max(z,0)+min(ez1,0)\sigma(z) = \max(z,0)+\min(e^{z}{-}1,\,0)

exponential below zero: smooth, and its negative outputs center the layer

Softplus 2001
σ(z)=ln(1+ez)\sigma(z) = \ln(1+e^{z})

relu with the corner sanded off — everywhere in theory, rare in practice

GELU 2016
σ(z)=zΦ(z)\sigma(z) = z\,\Phi(z)

z weighted by the Gaussian CDF — BERT and the GPT line run on it

SiLU · Swish 2017
σ(z)=zσ(z)\sigma(z) = z\,\sigma(z)

z gated by its own sigmoid — inside the SwiGLU blocks of the Llama family

Mish 2019
σ(z)=ztanh(ln(1+ez))\sigma(z) = z\tanh(\ln(1{+}e^{z}))

self-gated like silu, a touch softer — computer vision’s pick

Nine bends on identical axes, one unit square everywhere. The solid teal curve is the activation σ; the dashed vermilion curve is its derivative σ′ — the gate every gradient must pass through on the way back down. Read the flat stretches of σ′ as places learning goes quiet: saturation on the classics, the dead zone on relu, and the small negative slope the modern trio keeps alive. Hover any tile for exact values; six of these are live in Plate I.

The curve workshop

Now give the knobs away. The workshop below holds a whole network and trains it live, in a worker beside this page, by the only rule this book uses — θθηL\htmlClass{eq-model}{\theta} \leftarrow \htmlClass{eq-model}{\theta} - \htmlClass{eq-knob}{\eta} \htmlClass{eq-world}{\nabla \mathcal{L}} — with the loss set to the mean-squared error, the average of (f(x)y)2(f(\htmlClass{eq-world}{x}) - \htmlClass{eq-world}{y})^2 over the training points. Every step pulls the network’s curve a little closer to a target.

You see the machine three ways at once. On the left sits the network itself: every edge is one weight, drawn thicker as it grows, ultramarine when positive and vermilion when negative. On the right, the curve it currently draws against the dashed target. And underneath, the palette — the curves the deeper layers output, ending in the last hidden layer’s contributions, each drawn in the color of its output weight’s sign and thicker as that weight grows. Hover any neuron in the diagram to light up its wiring — and, if it owns a tile, the curve it outputs; or hover a tile to find its neuron.

Train on the sine and watch the palette organize — units that began as arbitrary steps drift into position, each claiming a stretch of the curve. Switch the target to bumps mid-training and the units renegotiate. Then take the pen and draw your own curve straight onto the plot: the network will chase it while you drag.

Plate III The curve workshop
The network
waking the training worker…
−10+1
The palette

the curves the deeper layers output — the last hidden layer is drawn as v·tanh(…), ultramarine where its output weight v is positive, vermilion where negative, thicker as |v| grows; the fit above is exactly their sum. hover any tile or any node in the diagram — the highlight runs both ways

the palette appears once the worker is up
target
hidden layers
16
activation
0 params · —
The network itself, live — every edge is one weight, read by the legend beneath it. The plot: the dashed target, and the fit the network currently draws against it. Underneath, the palette — the curves the deeper layers output, ending in the last hidden layer's contributions: colored by the sign of each output weight, thicker as it grows, and summing exactly to the fit. Hover any node or tile; the highlight runs both ways.

What width buys, what depth buys

Width buys vocabulary. Every extra hidden unit is one more step the output layer can place, so a wider palette affords finer detail: at width 2 the sine defeats the network — two steps cannot make three bends — while at width 16 it has bumps to spare. Watch the parameter count as you widen, though. Vocabulary is not free.

Depth buys reuse. With one hidden layer, every wiggle must be purchased with its own neurons. Add a second layer and the new neurons stop reading x directly: they read the first layer’s steps, and can bend an already-bent thing — bumps of bumps. A third layer bends the bumps of bumps again. The same budget of parameters goes further because pieces are reused instead of re-made.

That is not just a nicer story; it is a measurable gap. Count the straight pieces a relu network's fit is made of and the count grows roughly in proportion to width, but multiplies with each layer of depth — every layer folds the folds beneath it.11 And there are functions a deep network draws with a handful of units that no shallow network can match without an exponential number of them.12 In one dimension the difference is subtle; in the chapters ahead, where inputs are images, it is most of the story.

The activation sets the network’s handwriting. tanh is a soft wave, so its sums are smooth and rounded everywhere. The rectified linear unit — relu, zero on the left, a straight ramp on the right — creases instead of curves: its sums are piecewise-linear, and you can count the folds in the fit. Switch the workshop to relu on the |x| target and it lands almost at once, because the target is itself two creases. Switch back to tanh and watch it round a corner it can never make sharp. The workshop also carries the modern pair from the field guide — gelu and silu — which crease like relu with the corners sanded smooth; try them and read the difference straight off the palette.

One honest warning before you scale everything up. The workshop samples its target at 256 points packed densely along the interval, so fitting the data and fitting the curve are nearly the same task. They usually aren’t. With few points and many neurons, a network can pass through every training point exactly and still be wrong everywhere between them — wiggling where it should glide. That failure is called overfitting, and nothing in the loss ever asked the network to behave between the points. This book meets it properly once real data arrives.

The old lesson drawn from that is: capacity is dangerous, so keep the model small. The last decade made a mess of the lesson. Take a network that reads photographs well, shuffle the labels so every answer is now noise, and train it again — it fits all of them, perfectly, memorising the lot.13 Its capacity to overfit is total. Hand back the real labels and the very same network generalizes. Whatever stops it from doing the same to those, it is not a shortage of room.

Stranger still is what happens if you keep growing it. Test error rises as the textbook promises, peaks right at the size where the model can just barely fit its training set exactly — and then, as the model gets bigger still, comes down again, often below anything the small models managed.14 The curve has two descents, and the classical one is only the first. The models in the news live far out on the second, in the region the textbook picture calls hopeless, and the same double dip appears in how long you train, not only in how big you build.15 None of this repeals overfitting. It moves the explanation somewhere more interesting: what keeps an over-parameterised network honest is not how few parameters it has, but which of the enormous number of perfect fits gradient descent happens to walk to.

This chapter happened in one dimension on purpose: you could see every neuron, every bump, and the whole sum at once. Nothing was hidden — the palette under the plot is the network, laid out flat.

The next chapter keeps the machine and changes the canvas. In two dimensions a network stops looking like a sum of bumps and starts doing something stranger and better: it bends the space the data lives in, until problems that looked hopeless become straight lines. When you’re ready, go bend space.

Sources

  1. 1 Approximation by superpositions of a sigmoidal function Cybenko · 1989 · Mathematics of Control, Signals and Systems 2 · web.njit.edu The universal approximation theorem, for one hidden layer of sigmoids. Read the proof and notice what it never mentions: how many neurons, or how to find their weights.
  2. 2 The Expressive Power of Neural Networks: A View from the Width Lu et al. · 2017 · NeurIPS 2017 · arXiv:1709.02540 The mirror image of Cybenko: hold the width just above the input dimension and stack deep, and you are universal again. Width and depth are two ways to buy the same guarantee.
  3. 3 Understanding the difficulty of training deep feedforward neural networks Glorot & Bengio · 2010 · AISTATS 2010 · proceedings.mlr.press Measures the saturation directly, layer by layer, and shows how a badly scaled start drives sigmoid units flat and holds them there.
  4. 4 Rectified Linear Units Improve Restricted Boltzmann Machines Nair & Hinton · 2010 · ICML 2010 · cs.toronto.edu Where the crease enters modern practice.
  5. 5 Rectifier Nonlinearities Improve Neural Network Acoustic Models Maas, Hannun & Ng · 2013 · ICML 2013 WDLASL · ai.stanford.edu Leaky relu — a trickle of slope below zero, so a unit that falls silent can still be argued back.
  6. 6 Deep Sparse Rectifier Neural Networks Glorot, Bordes & Bengio · 2011 · AISTATS 2011 · proceedings.mlr.press The case that relu wins not despite its dead half but partly because of it: at any moment most units are silent, and the network computes on a sparse subset of itself.
  7. 7 Gaussian Error Linear Units (GELUs) Hendrycks & Gimpel · 2016 · arXiv preprint · arXiv:1606.08415 The bend the GPT and BERT lines are built on: weight the input by the chance a standard normal falls below it.
  8. 8 Sigmoid-Weighted Linear Units for Neural Network Function Approximation in Reinforcement Learning Elfwing, Uchibe & Doya · 2017 · Neural Networks 107 · arXiv:1702.03118 SiLU, found while building reinforcement learners.
  9. 9 Mish: A Self Regularized Non-Monotonic Activation Function Misra · 2019 · BMVC 2020 · arXiv:1908.08681 Self-gated like silu, a shade softer, and the one computer vision kept.
  10. 10 Searching for Activation Functions Ramachandran, Zoph & Le · 2017 · arXiv preprint · arXiv:1710.05941 The same function found again from the other end — by automated search over candidate formulas, which reported it as Swish before noticing it already had a name.
  11. 11 On the Number of Linear Regions of Deep Neural Networks Montúfar et al. · 2014 · NeurIPS 2014 · arXiv:1402.1869 Counts the creases. A relu network folds its input space, and each new layer folds the folds — so the pieces multiply with depth and only add with width.
  12. 12 Benefits of depth in neural networks Telgarsky · 2016 · COLT 2016 · arXiv:1602.04485 A function a deep network draws with a handful of units that any shallow network would need an exponential number to match. Depth is not a convenience.
  13. 13 Understanding deep learning requires rethinking generalization Zhang et al. · 2017 · ICLR 2017 · arXiv:1611.03530 Replace every label in a photograph dataset with a random one. The same networks fit all of it, perfectly. Whatever stops them from memorising real data, it is not a shortage of capacity.
  14. 14 Reconciling modern machine learning practice and the bias-variance trade-off Belkin et al. · 2019 · PNAS 116(32) · arXiv:1812.11118 The double-descent curve. Test error peaks exactly where a model can just barely fit its training set, and then falls again as the model keeps growing.
  15. 15 Deep Double Descent: Where Bigger Models and More Data Hurt Nakkiran et al. · 2019 · ICLR 2020 · arXiv:1912.02292 The same second descent in real deep networks — in model size, in training time, and, uncomfortably, in dataset size.