Learn
What a neural network is, and how one learns to play Snake
This page explains what a neural network is, how the one in this lab turns what the snake sees into a move, and how evolution finds its weights. The lab's network is a list of 740 numbers and the arithmetic that combines them, and everything described here can be watched in the lab.
What a neural network is
A neural network is a function: numbers go in, numbers come out. Here, 24 numbers describing what the snake sees go in, and 4 numbers come out, one for each move (up, down, left, right). Whichever of the four is highest is the move the snake makes. It does this again on every step of the game.
Inside, it is built from simple units called neurons, arranged in layers. They are loosely inspired by brain cells, but each one is a small calculation. What makes a network play well or badly is a set of adjustable numbers called weights. The same network with different weights behaves completely differently, so "learning" means one thing: finding good weights.
What the snake can see
A neural network only knows what you feed it. Each snake looks outward in eight directions — the four straight lines plus the four diagonals — and in every direction it measures three things: how close the food is, how close its own body is, and how close the wall is. Closeness is 1 ÷ distance, so something right next to the head counts as 1, something far away as a small number, and nothing at all in that direction as 0. Eight directions times three signals gives 24 inputs, and those 24 numbers are the network's entire view of the world on each step.
The directions are fixed to the board, not to the snake: "up" is always the top of the board, whichever way the snake is moving, and the four outputs are the same four board directions. Nothing in the 24 numbers says which way the snake is heading; the network can only tell from where its own body is.
Switch on binary vision and the food and body signals collapse to a simple yes/no: the snake knows something is there, but not how near. It is a harder, more abstract problem that some strategies still solve.
What one neuron computes
A neuron does three things:
- It multiplies each of its inputs by a weight, its own number for that input.
- It adds those up, plus one more number of its own, the bias.
- It passes the total through an activation function, which decides what it sends on.
Take a neuron with two of those inputs: how close the apple is to the right of the head, and how close the wall is in the same direction. In the position above, the apple is 2 squares to the right (1 ÷ 2 = 0.5) and the wall 5 squares to the right (1 ÷ 5 = 0.2), the "→ right" row of the table. The neuron's weights are 2.0 for the apple and −1.0 for the wall, and its bias is 0.1:
Now put the apple far to the right (0.1) and the wall right next to the head on that side (1.0):
0.1 × 2.0 + 1.0 × (−1.0) + 0.1 = −0.7 → the neuron sends 0
The positive weight means "an apple to the right pushes me up", the negative one "a wall to the right pushes me down". So this neuron fires when there is food to the right and the wall on that side is still far, and stays silent otherwise. The rule isn't programmed anywhere; it follows from these weights.
Turning −0.7 into 0 is the activation function at work. The lab's hidden neurons use ReLU by default: negative totals become 0, positive ones pass through unchanged. The four output neurons use sigmoid, which squeezes any total into 0 to 1. Both are settings in the lab, along with tanh (−1 to 1) and leaky ReLU (lets a trickle of negatives through). The activation function is also what makes several layers useful: without it, any number of layers would add up to a single weighted sum, and the network could not express "this, but not when that".
Layers: from 24 senses to one move
A single neuron can only notice one simple pattern, so a network uses many, in layers. In the lab's default network:
- The input layer is the 24 vision numbers. It doesn't compute anything; it just holds them.
- The first hidden layer has 16 neurons. Each one looks at all 24 inputs, with its own 24 weights and its own bias, and sends on one number. Together they turn 24 numbers into 16.
- The second hidden layer has 16 more neurons, each looking at all 16 numbers from the first. They can combine patterns: "food on the left" and "no body in the way on the left" into "left is a good idea".
- The output layer has 4 neurons, one per move, each scoring its move from the 16 numbers before it. The highest score is the move.
One trip from inputs to outputs is called a forward pass. It takes about 700 multiplications, and the snake does one on every step, so a 500-move game is 500 forward passes. Nobody tells the hidden neurons what to look for: the patterns they detect are whatever turned out to help, so they are hard to interpret.
The weights are the snake's brain
Count every weight and bias in the default network:
24 inputs × 16 neurons + 16 biases = 400
16 × 16 + 16 biases = 272
16 × 4 outputs + 4 biases = 68
total = 740 numbers
Those 740 numbers are everything the snake "knows". The lab calls the list its genome, and it is the only thing that changes as the snakes improve: the network's shape stays the same, only the numbers move. A brand-new snake gets 740 random numbers, which is why the first generation drives straight into walls.
You choose the shape in the lab: more hidden layers, or more neurons per layer, can represent more elaborate strategies, but the genome gets longer and takes more generations to tune. Because the input and output counts are fixed by the game, changing the shape starts a fresh population — the genome length changes, so old and new genomes are not comparable.
Two ways to find good weights
The familiar way, used by most deep-learning systems, is gradient descent with backpropagation: you show the network many examples with the right answer, measure how wrong it is, and work out exactly which way to nudge each weight to be a little less wrong. Snake has no labelled "correct move" for every board, so this lab uses the other approach — neuroevolution. Instead of teaching one network, you breed a whole population of them. The networks that happen to play better are more likely to become parents, and each new generation inherits and slightly varies what worked. Over many generations the population drifts toward strategies that eat more food and survive longer. It is Darwinian selection applied to network weights.
Nothing here is reinforcement learning, and nothing is NEAT. There is no reward-signal backpropagation and no evolving of the network's shape — you fix the shape and the genetic algorithm searches for good weights.
Fitness: what "good" means
Evolution needs a single number to rank snakes, and that number is fitness. The Classic fitness balances staying alive against eating food, so early snakes are rewarded just for not dying immediately — an important stepping stone before any of them can find food reliably. The Score-priority fitness leans hard toward food eaten and only lightly rewards survival, which pushes a population that has already learned to move safely into hunting more aggressively. Because a single game involves luck (where the food happens to appear), you can average several games per snake so that fitness reflects skill rather than a lucky board.
Building the next generation
One generation is one full round: every snake plays, each gets a fitness score, and then three genetic operators produce the next population.
Selection — choosing the parents
Rank-slice selection keeps the top fraction of the population and draws parents uniformly from that surviving slice. Tournament selection instead samples a small group at random and keeps the fittest of them; larger tournaments apply more pressure toward the very best snakes, which speeds convergence but can narrow exploration. A share of the strongest individuals is also preserved unchanged (elitism), so a good solution is never lost to bad luck in breeding.
Crossover — mixing two parents
A child's genome is built from its two parents' 740 numbers. Single-point crossover picks one break point and takes the numbers before it from parent A and the rest from parent B. Uniform crossover decides each number independently, coin-flip style, from either parent — a more thorough mixing that can combine features from both parents more finely.
Mutation — introducing new ideas
Without mutation a population can only recombine what it already has and will stall. Random-reset mutation replaces a number with a brand-new random weight; Gaussian mutation nudges it by a small random amount, preserving learned structure while still exploring nearby. The mutation rate sets how many numbers change; too little and progress stalls, too much and promising strategies get scrambled. A common pattern is to explore with more mutation early, then lower it once scores stabilise.
Why progress is bumpy
Watch the score-per-generation chart in the lab and you will see it climb overall but dip along the way. That is normal: crossover and mutation are exploratory, so any generation can produce weaker candidates, and a different food layout adds noise. What matters is the trend, not any single generation. When you find a population you like, save it to a file — it is the only copy, since nothing is stored on a server.
Everything runs in your browser
There is no training server. The entire simulation — the game, the neural networks, and the genetic algorithm — runs locally in WebAssembly, and evaluation is spread across a pool of Web Workers to use the cores your device already has. Your populations never leave your machine unless you choose to download a save file and share it yourself.
Try it yourself
Change one setting at a time and compare the runs:
- Keep Show live network on and watch which neurons light up before each turn.
- Try a smaller network (one hidden layer of 8) and a bigger one, and compare how fast each improves.
- Switch the hidden activation function from ReLU to tanh or sigmoid.
- Compare rank-slice and tournament selection on the same seed.
- Raise the mutation rate, then lower it once scores plateau.
- Turn on a fixed food seed so two runs are directly comparable.
Common questions
What is a neural network, in one sentence?
A function made of simple units called neurons, arranged in layers, that turns input numbers into output numbers; how it behaves is set entirely by its adjustable weights.
What does a single neuron compute?
It multiplies each input by a weight, adds the results plus a bias, and passes the total through an activation function such as ReLU, which turns negative totals into 0.
Is this reinforcement learning?
No. There is no reward signal and no backpropagation. A genetic algorithm scores complete networks and evolves their weights across generations.
Does it use NEAT?
No. NEAT evolves the network's topology as well as its weights. Here the shape is fixed and only the weights evolve.
How many inputs does the network have?
24: eight directions, each reporting the distance to food, to the snake's own body and to the wall. The four outputs are the possible moves.
How long does it take to see good snakes?
It depends on population size, network shape, mutation and luck. You will usually see steady improvement within a few dozen generations, and with the default settings genuinely good snakes typically emerge somewhere between 150 and 500 generations. Larger networks take longer.
Can I train my own?
Yes, that is what the lab is for: pick a network shape and genetic-algorithm settings, create a population and press Start training. It runs in your browser, with nothing to install.
Can I reproduce someone else's run?
Yes. Enable the fixed food seed and share your experiment link; anyone who opens it starts from the same settings.