Notes / Reinforcement Learning / Paper 1 of 2

Leer en español

Playing Atari with deep reinforcement learning

How an agent learns to play video games from the frames alone: a network that turns the last few frames into a value for each move, trained with Q-learning on moments drawn at random from a memory of its own play.

The agents of the earlier notes were handed their state, such as a grid cell or the cart-pole's angle. The paper this note follows, by Mnih and six colleagues at DeepMind (2013), gives an agent only what a player has: the frames of an Atari 2600 game, its score, the game-over signal and the controls. With one network design and one way of learning, it learned seven games.

The figures use a game of this note's own: in a frame of 8 by 6 pixels, a ball bounces off three walls, and the agent moves a paddle up and down the right edge to hit it back. A hit is worth 1 point; a miss costs 1 and ends the game.

Deep learning, networks with many layers, had learned from labeled images, each independent of the others; a game offers neither, and the paper names three obstacles:

  • +1

    The point comes frames after its move, and no label says which move earned it

  • ≈

    Frames in order nearly repeat each other, so they are not independent

  • A paddle kept near the top fills most frames, so what the agent learns from shifts with its play

The paper meets the first obstacle with Q-learning and the other two with a memory of its own play. Q-learning carries each point back to the moves that earned it, and drawing from the memory at random mixes old moments with new.

Throughout this page, blue ink is the network's estimates, red ink what they aim for (a target, or a move's exact value), and yellow the move just chosen.

1Values from the frames

Besides the score, the agent sees only the game's frames, and the figures' agent looks at the last two. A network turns that pair into a value for each move: the points to expect from making it and playing well after, later points counting for less.

The figure shows the network that section 3's run trains. Drag the ball: where you drop it is where it is now, on the later frame, and the way you moved it sets where it was a step before, drawn faint.

  • The bars are the network's values of the three moves, and the blue arrow beside the paddle points to the move worth most.
  • The short red line across each bar is that move's exact value under best play, computed from the game's rules, which the network never sees.
  • The One frame button shows the values of a network whose input is only the later frame.
Input:

Drag the ball. With a keyboard, select it with Tab and use the arrow keys.

Where a move decides a hit, the network's best move hits; moves seldom made in a situation keep rough values. The rest of this section builds what the figure shows: the state the network sees, the values it aims at, and the network itself.

The paper's agent plays through an emulator of the console, ℰ. At each time step t it picks an action at from 𝒜 = {1, …, K}, here K = 3: up, stay and down; it sees an image xt ∈ ℝd, the pixels of the frame (here d = 48, each 0 or 1); and it receives a reward rt, the change in the score. The rest of this note's game:

  • There are 12 serves, from the left wall on any of the 6 rows, rising or falling, with the paddle on rows 2 and 3; within a step the paddle moves before the ball.
  • A rally is cut at 10 hits: the cut is not an ending, and the agent learns as if play went on.

With One frame, a ball beside the paddle gets the same values rising or falling, though the move that hits it depends on which: one frame does not show the whole situation.

Perceptually aliased states, in the paper's words: different situations that one frame shows alike.

So the paper takes as the state the whole sequence of frames and actions so far,

st = x1, a1, x2, …, at−1, xt

The sequence holds everything that decides what comes next, and since every episode of the paper's games ends, there are finitely many sequences: a large but finite problem, to which the earlier notes' methods apply.

A network needs an input of fixed length, so the paper feeds it φ(st), the last four frames, each shrunk to 84 × 84 gray pixels. In this note's game the ball moves one pixel diagonally per step, in a straight line between bounces, so two frames show where it is going, except at the serve, where the first frame counts twice:

φ(st) = (xt−1, xt)

The agent aims at the return, the rewards from t on, each discounted by γ for every step it comes later (the paper gives no value; the figures use γ = 0.9):

Rt = ΣTt′=tγt′−trt′

and the value of an action is the largest expected return after it, over all ways of playing on,

Q*(s, a) = maxπ E[Rt | st = s, at = a, π]

The figure's red lines are Q*, which the page computes exactly for this game, by the value iteration of the model-based note over the game's 552 true situations, which the agent, knowing neither the rules nor the state, cannot do. Since the cut is not an ending, these are the values of a rally that never ends:

Where the rally isExact value
One step before a hit, then a hit every 12 steps1 + 0.912 + 0.924 + ⋯ = 1/(1 − 0.912) ≈ 1.39
At a serve0.95 × 1.39 ≈ 0.82
A miss certain k steps ahead−0.9k−1: −1 one step before it

With a value for each sequence, or each pair of frames, the agent would learn nothing about a pair it had not seen, and while this game has only 1,336 pairs, an Atari game has far too many. So the paper estimates Q* with weights θ, as the approximation notes did:

Q(s, a; θ) ≈ Q*(s, a)

A linear estimate like those notes' fails on the pixels. Each lit pixel adds its own weight, so the estimate cannot say that moving up is good when the ball is above the paddle and bad when it is below, wherever the two are. A network learns such combinations from the pixels.

A network is built in layers, each multiplying what it receives by a table of weights and passing the result through a fixed function (the network of the word2vec note). The figures' network has two layers:

  • 96 inputs, the pixels of the two frames;
  • 32 hidden rectifier units, max(0, z), which keep a positive number and turn a negative one into 0;
  • and 3 outputs, one value for each move:
Q(φ, ·; θ) = W2 max(0, W1φ)

with a 1 in front of φ and of the hidden layer, so that the first columns of W1 and W2 hold the biases, as in the word vectors note; θ is W1 (32 × 97) and W2 (3 × 33), 3,203 weights; the paper's Q(s, a; θ) is computed as Q(φ(s), a; θ).

The paperTwo convolutional layers come first, sliding one small set of weights over the image to find a pattern wherever it appears.

In the rallyThe frames are small enough for a layer that joins every pixel to every unit.

One output per move is the paper's choice: earlier networks took the action as an input and needed a pass for each action; this one gives every move's value in one pass. In the earlier notes' notation:

The paperThe earlier notes
θthe w of the approximation notes
Q(s, a; θ)their q(s, a, w)
Q*the q* of the Q-learning note
rt, the reward that follows atRt+1
the return RtGt

2Learning from its own estimates

Q* obeys the Bellman optimality equation of the Q-learning note, here in the paper's notation (its equation 1):

Q*(s, a) = Es′∼ℰ[r + γ maxa′ Q*(s′, a′) | s, a]

Value iteration, which the model-based note ran on the values of states, turns it into an update repeated iteration after iteration, and converges to Q*:

Qi+1(s, a) = E[r + γ maxa′ Qi(s′, a′) | s, a]

It needs a value for every sequence, though, and learns nothing about one sequence from another.

The paper keeps value iteration's shape with a network. At iteration i it fits the weights θi to targets computed with the weights of the iteration before, θi−1, by making a loss small, a number that says how wrong the estimates are, as the VE of the prediction note did (the paper's equation 2):

Li(θi) = Es,a∼ρ(·)[(yi − Q(s, a; θi))2]

where the target is

yi = Es′∼ℰ[r + γ maxa′ Q(s′, a′; θi−1) | s, a]
  • ρ(s, a) is the paper's behavior distribution, how often each sequence and action come up in play (the prediction note's μ, not the ratio ρ of the n-step note).
  • θi−1 is not a second network: it is the weights before a step, and θi the weights the step changes.

So the targets depend on the weights. In supervised learning each example's target, its label, is fixed before learning begins. Here every step that changes the weights can change the targets too.

The figure takes the game's first step from a serve: the paddle stays, the reward is 0, and the next pair of frames is φ2. The network learns Q(φ1, stay; θ) from that transition alone, one gradient step on Li per iteration, in two runs, the value in blue and its target in red:

  • the left one keeps the first target, as a label would be kept;
  • the right one computes it again before every step, from the weights as they stand.
stay, 0 φ1 φ2 Target held 01020300.10.20.3 target value 0.167 Target from the network 0102030 0.329 iteration i value
Held like a label (left), the target stays at y1 ≈ 0 + 0.9 · 0.186 ≈ 0.167, and the value reaches it by iteration 4. Recomputed from the weights before every step (right), the target rises with the value, and the two meet at y30 ≈ 0 + 0.9 · 0.365 ≈ 0.329, about twice the first target.

The target moved because one network computes both values:

  1. The step that raised staying in φ1 changed staying's output weights, which every pair of frames uses, and hidden units fed by pixels the two frames share; so it raised staying in φ2 too, by about four fifths as much.
  2. Moving down had been φ2's largest value, just above staying; after the step, staying is, so the target borrows the very value the steps raise.
  3. Times γ = 0.9, the target gains about 0.73 of the value's gain, so the gap shrinks by about a fifth per step until the two meet at 0.329, a fixed point, as the TD fixed point of the prediction note was for a linear estimate.

Had a step raised the value of the next pair of frames by more than 1/0.9 times as much as the value it updates, the target would have run ahead, and both would have grown without bound: learning from its own estimates can diverge, as section 4 takes up again. Only a final transition's target, the reward −1, cannot move.

To make Li small, the paper follows its gradient. With θi−1 held fixed it is (the paper's equation 3, with a factor added):

−½∇θiLi(θi) = Es,a∼ρ(·); s′∼ℰ[(r + γ maxa′ Q(s′, a′; θi−1) − Q(s, a; θi)) ∇θiQ(s, a; θi)]

A slip in the paperIt prints its equation 3 without the −½: its right side is then the direction a descent step takes, not the gradient itself, which points the other way and is twice as long.

The prediction note's ½ gave the same step, w ← w − ½α∇[…]2 = w + α[…]∇v (prediction note), and holding θi−1 fixed makes it what that note called a semi-gradient: the step follows how the weights move the estimate, and ignores how they move the target. For a network, ∇θQ is computed layer by layer, from the output back, by the chain rule of the linear classifiers note, a computation called backpropagation.

Rather than computing the expectations, the paper takes stochastic gradient steps; with a single sample of ρ and of ℰ after every time step, the update is

θ ← θ + α[r + γ maxa′ Q(s′, a′; θ) − Q(s, a; θ)] ∇θQ(s, a; θ)

This is Q-learning: with a table, a weight for each sequence and move, ∇θQ is 1 for the pair updated and 0 for every other, and the step is the update of the Q-learning note; with the next move's value in place of the maximum, it would be semi-gradient Sarsa.

Like the methods of the TD note, it is model-free: it learns from samples of ℰ and builds no model of it. And it is off-policy, as in the Q-learning note: it learns about the greedy policy, a = argmaxa Q(s, a; θ), while it plays ε-greedy. The paper writes max there, and in its algorithm, where it means argmax.

After the paperThe later version of DQN (Mnih et al., Nature, 2015) computes its targets with a second copy of the network, the target network, whose weights it writes θ−i and uses in place of θi−1:

yi = Es′∼ℰ[r + γ maxa′ Q(s′, a′; θ−i) | s, a]

Every C updates of θ, 10,000 for every game, it copies θ into θ−i, so the target stays still between copies, as in Target held, the left chart of this section's figure. A step that raises the value of the next pair of frames then moves the target only at the next copy, which makes oscillation and divergence much less likely. The 2013 paper's θi−1 is the case of a copy before every step.

3Deep Q-learning

Deep Q-learning adds one thing to that update: a memory. After every step the agent stores the transition, et = (st, at, rt, st+1), in a data set (the algorithm stores each s as its φ, all the network reads),

𝒟 = e1, …, eN

the replay memory, which keeps the last N transitions, pooled over many episodes.

Experience replay: learning from such a memory, an idea that goes back to Lin (1993). Each update draws a minibatch of transitions at random from 𝒟, 32 in the paper, and takes one gradient step on their average.

That lies between the prediction note's one state per step and the TD note's batch updating. Dyna-Q, in the model-based note, also drew remembered steps at random, but kept only the last outcome of each pair. Section 4 says why the draws are random.

The algorithm

Deep Q-learning with experience replay, for a network Q with weights θ, a memory of N transitions, minibatches of 32, a step size α and an ε that falls over time, as the Monte Carlo note let it shrink, for M episodes:

  1. Start with 𝒟 empty, with room for N transitions, and θ at random.
  2. At the start of each episode, observe the first frame x1, and set s1 = x1 and φ1 = φ(s1).
  3. With probability ε choose a random action at; otherwise at = argmaxa Q(φt, a; θ).
  4. Take at, observe rt and xt+1, and set st+1 = st, at, xt+1 and φt+1 = φ(st+1).
  5. Store (φt, at, rt, φt+1) in 𝒟, dropping the oldest transition once it holds N.
  6. Draw a minibatch of transitions (φj, aj, rj, φj+1) at random from 𝒟, and set each one's target yj (below).
  7. Take one gradient step on their squared errors, with the targets held,
    θ ← θ + α · 132 Σj[yj − Q(φj, aj; θ)] ∇θQ(φj, aj; θ)
    If φt+1 is terminal, or the rally has reached the game's cut, start the next episode at step 2; otherwise go back to step 3.

The targets of step 6 are

yj = rj, if φj+1 is terminal;
yj = rj + γ maxa′ Q(φj+1, a′; θ), otherwise.

Here j counts the transitions drawn, where section 2's i counted iterations; the targets use θ before the step, section 2's θi−1, and α absorbs the 2 of the squared error. A rally cut at 10 hits is not terminal, so its last target still adds γ times the value of the next pair of frames. The paper calls these networks deep Q-networks, DQN.

A slip in the paperIts algorithm writes Q* for the network's Q.

The paperThe figures
Frames in φ42
Network16 filters 8 × 8 (stride 4), 32 filters 4 × 4 (stride 2), 256 units, 4 to 18 outputs32 units, 3 outputs, 3,203 weights
Rewardsclipped to −1, 0, +1 in training, so one step size suits every gamealready −1, 0, +1
ε1 to 0.1 over the first million frames, then 0.11 to 0.1 over the first 10,000 steps, then 0.1
Memory Nthe last million framesthe last 10,000 transi­tions
OptimizerRMSProp, which divides each weight's step by the typical size of its recent gradientsgradient descent, α = 0.1

The figure runs these steps on the game from random weights, with the settings of the table, in three regions:

  • At the top, the game, with section 1's bars and arrow; the move just chosen fills its row yellow, and the bars a target borrows are outlined red.
  • In the middle, the memory, as cells, then as a strip; green numbers or ticks mark each step's draws.
  • At the bottom, one drawn transition: its two pairs of frames and a gauge from its value (blue) to its target (red), with a green marker where the step takes it; one step moves a value too little to see on the bars.

Each press is one line of the steps above, as in the Sarsa figure of the control note; episode 1 is played in full, episode 2 up to its first hit, and then the figure skips to moments where something new is learned.

Next goes forward, Back goes back. Keys: → and ←.

Each step lowered the squared error of its 32 draws taken together, but not of each draw: about a third of the drawn transitions moved away from their targets in a step, pulled by the others.

4Why experience replay

Sarsa, Q-learning and TD-Gammon, an earlier program that learned backgammon with a network, used each step once, in the order it came. A memory changes three things, the paper's advantages:

  1. Each step is used in many updates: transition 12 of section 3's figure was drawn 243 times while it stayed in the memory.
  2. Consecutive steps are strongly correlated, the opening's second obstacle, so learning from them in order chases the current rally; draws from the last 10,000 steps break the correlation and lower the variance of the updates.
  3. When an agent learns from the data its current weights produce, the weights choose the next data, the opening's third obstacle: a network that prefers moving up trains mostly on frames with the paddle at the top. Such loops can trap the weights or make them diverge; replay averages ρ, the behavior distribution, over many past behaviors.

Replay makes learning off-policy, in section 2's sense, whether one wants it or not: a drawn transition holds a move that older weights chose, often at random. That decides the method:

Its target uses
Sarsathe move that came next, which older weights chose too, so with replay it would learn about old policies
Q-learningthe best move of the next pair of frames, under the weights as they stand, which is why the paper uses Q-learning

Q-learning's one-step target needs no correction for the other policy, as the Q-learning note showed: given the pair it updates, the reward and the next frame do not depend on who chose the move. The paper also names the limits of its memory: it keeps only the last N transitions, dropping the oldest whatever they hold, and draws every transition alike, though in this run only 3 or 4 of each 32 draws carry a nonzero reward; it suggests favoring the transitions there is most to learn from.

The figure runs section 3's agent from 16 seeds for 30,000 steps, with three ways of choosing each step's minibatch:

Each step learns fromAdvantages kept
Replay32 transitions drawn at random from the memoryall three
Last 32the 32 most recent, in orderthe first only
Latest onlythe newest alone, oncenone

Every 250 steps a check plays the 12 serves with no random moves, and each curve counts the agents that hit ten times from every serve. Replay is blue, the others faint, Latest only dashed.

agents perfect in a check, of 16 010,00020,00030,0000481216 training step ε reaches 0.1 Replay Last 32 Latest only
Replay's agents are first perfect sooner (a median of 9,000 steps, against Last 32's 11,000) and nearly always stay so: 8% of their later checks miss a serve, against Last 32's 30%. At step 30,000, only 6 of 16 Latest only agents play every serve.

Replay's gain is that it stays stable at a step size large enough to learn fast: at three times the run's α, every replay agent still learns, while no agent without replay does, and on more than half of them the values blow up. The loop of advantage 3 is not the cause: with every move random, half the Last 32 agents blow up just the same. Without replay, each step learns from the last few transitions, nearly the same frames, and pushes the same weights again and again, toward targets those weights set. Drawn at random from 10,000 steps, the pushes average out.

The paper cites results showing that Q-learning can diverge:

  • when its estimate is not linear, such as a network (Tsitsiklis and Van Roy, 1997);
  • or when it learns off-policy (Baird, 1995).

It offers no guarantee that its own method converges, and it reports that none of its runs diverged.

5Summary

The key ideas next to their NumPy equivalents, for the figures' network and settings. In the code:

  • x_prev and x are the last two frames, w1 and w2 the weights (biases in the first column), theta both as one vector, and q the three values.
  • rs holds the rewards of an episode from t on, and g is the return Rt.
  • In the minibatch rows, r, done and q_a hold the 32 rewards, whether each next pair is terminal (1 or 0) and the values of the moves taken; q_next holds each next pair's values and grad_q the gradient of each Q(φj, aj; θ), as rows, which backpropagation computes.

The step is the figures' plain gradient step, α absorbing the 2 of the squared error.

Return
Rt = ΣTt′=tγt′−trt′
g = rs @ gamma ** np.arange(len(rs))
Stacked frames
φ(st) = (xt−1, xt), where the paper stacks four
phi = np.concatenate([x_prev, x])
Q-network
Q(φ, ·; θ) = W2 max(0, W1φ), with a 1 in front of φ and of the hidden layer
h = np.maximum(0, w1 @ np.append(1, phi))
q = w2 @ np.append(1, h)
ε-greedy action
random with probability ε, otherwise argmaxa Q(φ, a; θ)
a = rng.integers(3) if rng.random() < eps else np.argmax(q)
ε falling over time
from 1 to 0.1 over the first 10,000 steps
eps = max(0.1, 1 - 0.9 * t / 10_000)
Replay memory
𝒟 = e1, …, eN, the last N transitions
memory = deque(maxlen=n)
memory.append((phi, a, r, phi_next, done))
Minibatch
32 drawn at random from 𝒟
idx = rng.integers(len(memory), size=32)
Targets
yj = rj + γ maxa′ Q(φj+1, a′; θ), or rj if φj+1 is terminal
y = r + gamma * q_next.max(axis=1) * (1 - done)
Loss
Li(θi) = E[(yi − Q(s, a; θi))2]
loss = np.mean((y - q_a) ** 2)
Gradient step
θ ← θ + α · 132 Σj[yj − Q(φj, aj; θ)] ∇θQ(φj, aj; θ)
theta += alpha * (y - q_a) @ grad_q / 32