Deep Q-Network

Deep Q-Network

Use a small neural network to estimate the value of every move.

Press Train to watch the network learn from scratch, or slide through its training. A neural network, not a table, learns which way to go.

  • cheese, +10
  • cat, −10
  • each step, −1
  • a move recalled from memory
L=( r+γmax⁡a′Qtarget(s′,a′)⏟y−Q(s,a) )2L = \big(\, \underbrace{r + \gamma \max_{a'} Q_{\text{target}}(s', a')}_{\htmlClass{sym-line}{y}} - Q(s, a) \,\big)^2

Move 1: A5 → B5, r = −1. y = −1 + 0.90 × 0.46 = −0.58.

Q(s, a) = −0.39, so L = (−0.58 − −0.39)² = 0.04

After step 3000: the network learned from 8 moves recalled from memory, average loss 0.01. It tries a random move with chance ε = 0.20.

Best path from the start 8 steps
Shortest possible: 8 steps

The 8 moves recalled for the last update.
1A5→−1B5−0.58−0.39−0.20
2B1→−1C16.376.300.07
3B5↑−1B40.510.460.06
4A5↓−1A5−1.26−1.370.12
5A4↑−1A30.790.790.01
6D1←−1C16.376.170.20
7C1→−1D17.988.09−0.11
8C1↑−1C16.376.43−0.06

Try it in Python

The Lab's field, ε and training so far, in Python: a small network trained from fresh weights with the Lab's own random numbers, replay memory and target network. NumPy keeps the weights in arrays; Plain Python in lists.

What you are looking at

A mouse starts in the bottom-left square of a small field. The cheese is worth +10, a cat costs 10, and every step costs 1, so a short safe path is best. This is the same game as Q-Learning, but the mouse's knowledge is kept in a small neural network instead of a table.

The triangles show the network's value for each move from each square: green good, orange bad. The arrows are its best moves, and the dashed line is the path it would take. The pink arrows are the moves it just practised from memory. Below, the chart shows how much reward each episode earned. The fields are made up, and Refresh Data shows another.

What each symbol means

s, a, r, s′
A square, a move, its reward, and the square it led to.
Q(s, a)
The network's value for that move: the reward it expects from here on.
y
The target: the reward plus γ times the best value of the next square.
γ
How much later rewards count: here 0.9.
L
The loss: how far the network's value is from the target, squared.
ε
How often the mouse tries a random move instead of its best one.

A network instead of a table

Q-Learning keeps one number for every square and move. That works for a small grid, but not when there are millions of possible situations, like the screens of a video game. A Deep Q-Network replaces the table with a neural network: it takes a description of the situation and gives a value for every move.

Q(s,⋅)=W2 ReLU(W1xs+b1)+b2Q(s, \cdot) = W_2\, \mathrm{ReLU}(W_1 x_s + b_1) + b_2

Here the description is just the square's column and row, and the network has 24 hidden units. Because nearby squares have similar inputs, what the mouse learns in one square spreads to its neighbours, even ones it has never visited. That is the strength of a network, and also its risk: a lesson can spread where it does not belong.

How it learns

Learning works like Q-Learning: a move's value should equal its reward plus the discounted value of the next square. The network is nudged, by gradient descent, to shrink the gap.

L=( r+γmax⁡a′Qtarget(s′,a′)⏟y−Q(s,a) )2L = \big(\, \underbrace{r + \gamma \max_{a'} Q_{\text{target}}(s', a')}_{\htmlClass{sym-line}{y}} - Q(s, a) \,\big)^2

Two tricks keep this steady. Replay: every move goes into a memory of the last 400, and each step the network practises on 8 moves drawn from it at random, not just on the latest move. That breaks up runs of similar moves, which would otherwise pull the network one way and then another. A target network: the targets y come from a copy of the network that is refreshed only every 100 steps, so the network is not chasing a target that moves with every update.

Exploring, and the reward chart

With chance ε the mouse tries a random move, so it keeps discovering new squares. Early episodes are long and costly; as the network improves, the reward per episode climbs towards the best possible. The chart is bumpy because random moves sometimes walk the mouse into a cat.

Training is not guaranteed to succeed. Change ε and the whole run plays out differently: sometimes the network finds the shortest path, sometimes it settles on a longer one or none at all. Deep reinforcement learning is famous for this.

The model in short

  1. Act: take the network's best move, or with chance ε a random one, and remember it.
  2. Recall a few remembered moves at random.
  3. Aim each at its target y, using the slow-changing target network.
  4. Nudge the network's weights to shrink the squared gap, and repeat.