The cat is a neural net
Seven small steps, about three minutes. Everything you see is computed live in your browser.
Step 1 of 7
Here's a tiny JavaScript function.
It decides how hard a cat should jump to land on a shelf. Drag the shelf and watch.
Step 2 of 7
The same function is also a A chain of weighted sums and simple on/off switches. Its numbers are usually learned from examples..
Handwired rebuilt jumpPower out of the parts neural nets are made of. Nobody trained it: its The numbers inside a neural net. In a Handwired net they are your constants. are your constants.
Every dot is one operation: a weighted sum, or a sum through an on/off switch. Bright dots are busy for this shelf.
Step 3 of 7
Try to catch it being wrong.
One shelf matching could be luck. Let's try every shelf from 0 to 100 cm, in steps of 1/32 cm.
Step 4 of 7
Then the cat had a very big dinner.
A heavier cat needs more power. Plain JavaScript doesn't know that, and it can't find out.
0 of 12 jumps landed
Each miss is the old rule giving the heavier cat too little power, or too much. The code is still the code: to fix it, someone would have to work out new numbers by hand.
Step 5 of 7
But the network can learn from those 12 jumps.
Because it's a neural net, we can One small training step: measure how wrong the 12 answers are, and move each weight a little in the direction that makes them less wrong. Hundreds of nudges take a second or two. its weights toward the right answers, the usual way nets are trained. Its weights are the constants, so watch the numbers in the code.
0 of 12 jumps land
Step 6 of 7
Why not just train a regular neural net?
Give a regular net the same 12 jumps. It's a standard design with 49 weights. We first trained it to copy the original rule, so both start from the same behavior.
Handwired net
0 shelves it would miss, out of 3,201
0 nudges · 0 impossible jumps
What it learned:
Regular neural net
0 shelves it would miss, out of 3,201
0 nudges · 0 impossible jumps
What it learned: 49 numbers. Which one means “heavier cat”?
Both saw only the 12 white dots. The miss count checks every shelf from 0 to 100 cm against what the heavier cat really needs. An impossible jump asks for under 0% or over 100%; the Handwired net can't, because Math.min and Math.max are still in it, exactly.
Step 7 of 7
Handwired turns code you trust into a neural net you can train, and it stays your code.
That's the idea behind Cheng Lou's demos: a whole desktop UI running as one net, a photo layout that learns from your sketch, and a JSON search running on the GPU.
Fine print: what's measured, and what isn't
- Everything is computed live in your browser. JavaScript runs the source shown, through
new Function. The network is the same rule built with Handwired's builder: 41 operations, usingemitCompareforshelf > 50andemitClampExactfor the min/max. - "Same answer" means the identical number with no tolerance, for all 3,201 shelves (0 to 100 cm in 1/32 cm steps). That holds because the inputs and constants are values 32-bit floats store exactly. For arbitrary fractions, JavaScript's 64-bit math and the network's 32-bit math can differ in the last digits.
- The heavier cat is a simulation: its "right" powers come from the same rule with BASE 12, PER_CM 1 and EXTRA 16. A jump lands if the power is within 0.75% of what the cat needed.
- Learning uses Adam on the gradients from
circuit.backpropagateReference. Only BASE, PER_CM and EXTRA learn. The 50 cm threshold can't learn, because a comparison has no slope. The learned constants are shown rounded (whole numbers, quarters for PER_CM); the page re-checks the rounded code against the network. - The regular net is 1 → 16 → 1 with ReLUs (49 weights), run on the same Handwired runtime with the same training code. It starts from seed 7 and is pre-trained for 2,500 steps on random shelves priced by the original rule. Its results depend on that setup, and you could bolt a clamp onto it. The Handwired net is shaped like the rule, which is exactly why it learns this change quickly; it couldn't learn an unrelated pattern.