Deep Q-learning · a neural network trained live in your browser

Little Q

Little Q has never seen this world before. Its brain is a small neural network that starts out knowing nothing, and it gets better only by trying, failing, and remembering what happened. Its goal is to reach the glowing exit by the shortest route without bumping into the slimes or beetles. Turn the speed up and watch it figure that out.

Speed
Q-values now
↑—
←—
→—
↓—
  • Positive weight
  • Negative weight
  • Signal (forward pass)
  • Learning (weights just changed)
Reaches the exit (last 20 runs)—
Route efficiency—
Caught by mobs—
Random moves (ε)—
Runs · steps0 · 0
Learning curve, rolling over the last 20 runs
  • Success rate
  • Route efficiency (shortest route ÷ steps taken)

What's going on

1. Learning from rewards

Nobody shows Little Q the right way to go. This is reinforcement learning: the agent acts, the world responds, and a number called the reward says how that went. Here it is small and simple:

The small per-step rewards are reward shaping. Without them the only feedback would come at the very end of a run, and early on, when it's moving at random, it would almost never get there. The shaping tells it it's warm or cold on every step.

2. What it senses

It never sees the map. It gets 28 numbers about the four tiles around it, shown as the little four-node crosses on the left of the 3D view. There are seven questions, each asked once for up, right, down and left:

Because these features describe the world relative to where it's standing, the same small network works on every new map.

3. Q-values: how good is each move?

The network's four outputs are Q-values: its estimate of the total future reward it will collect after making that move. That isn't just the next step, it's everything after, with later rewards counting a little less (each step ahead is discounted by 0.95). It simply takes the move with the highest Q. You can see the live values under the network, and the arrows on the map show which move it would choose from every tile right now.

Learning comes from the Bellman equation. After a move, the network compares its prediction with what it actually got plus its own estimate for where it ended up:

Q(here, move) ← reward + 0.95 × max Q(next tile, any move)

The gap between the two is the error. Backpropagation nudges every weight to shrink it: the gold sparks in the 3D view run backwards along the weights that changed most. Do this thousands of times and the value of reaching the exit slowly flows backwards through the map, one step at a time, until the arrows point the right way from everywhere.

4. The tricks that make it work

5. Things to try

The size of it

28 → 16 → 12 → 4 with ReLU: 688 weights plus 32 biases. That's the whole brain. Every connection in the 3D view is a real weight, drawn brighter the bigger it is. It's plain JavaScript with no ML library. It's a pocket-sized version of DQN, the algorithm DeepMind used in 2015 to learn Atari games straight from the screen.