Introduction to Reinforcement LearningPart 3 · Deep RL and planning

Lecture 8Deep reinforcement learning

Neural networks as value functions: automatic differentiation, deep Q-learning in detail, why online deep RL is hard, and the tricks that make deep learning and RL work together, such as target networks, experience replay, double DQN, prioritised replay and dueling networks.

By the end you should be able to compute a gradient through a small network by hand, explain each part of DQN, and say why each trick helps.

Key ideas, one per episode

  • Ep. 27Swap the table for a network, keep the idea.
  • Ep. 28Let the network learn the representation.
  • Ep. 29Shuffle the data, freeze the target.
  • Ep. 30Spend your updates where you're wrong.

Based on: Deep Reinforcement Learning (deep value functions, deep-learning-aware RL and RL-aware deep learning), DeepMind × UCL Reinforcement Learning Lecture Series (2021), by Matteo Hessel. The slides follow that order of topics, in our own words, figures and examples; they are not official material of the original course.

The title slide of Lecture 8: Deep reinforcement learning

36 slides, written to be read on your own, with worked examples, “try it” boxes, small simulations and check-yourself questions (answers at the end). PDF, 1.1 MB.

Watch

4 short episodes of ACML Explains for this lecture. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 27 · 5:25

From a Q-table to DQN

Tic-tac-toe in about ten lines of Python. Then we swap the table for a network.

Swap the table for a network, keep the idea.

TranscriptEpisode 27

Tic-tac-toe

Let's start with code. In class, we built Q-learning for tic tac toe. Nine squares, so nine actions. Plus one for a win, minus one for a loss, zero for a draw. First question, always: is your reward really what you want? Stop and check. It's usually not trivial. γ is point nine, so Bit looks many moves ahead. And ε is point one. The reward only arrives at the end of the game. Every other move gets zero.

The weights are the table

The Q-table starts at zeros. And here's a nice way to see it: the model's weights are the table's values. Look for the policy in the code. There's no policy function anywhere. The policy is implicit. It's the argmax of Q. The update itself is four lines. Elegant to the bone. Short, readable, and it really works. If both players share one table, they play symmetrically. With two tables, they can play differently. And against a random opponent, you learn the best play against a random player. Nothing more.

Our run

We ran it: Bit plays crosses against a random opponent, for forty thousand games. After a hundred games, playing greedily, he wins about sixty percent. After forty thousand: about ninety six percent, and he almost never loses. By then, the table holds about two thousand board positions. A nice trick from class: make a video of the table filling in. It looks like abstract art. These are the values of the first move. The centre comes out on top. Another trick: plot the table's average value over time. It's jagged, and more jagged with a bigger ε. Ninety six percent sounds great. But remember who the opponent is. Against a strong opponent, it would have a lot more to learn.

Least squares TD

Now, gradient descent uses each experience once, and then throws it away. That's not very sample efficient. Batch methods ask a different question: which weights best fit everything we've seen so far? For linear TD, there's a closed form. Least squares TD: build a matrix and a vector from the data, then solve. Plain TD costs about n operations per step, for n features. Least squares TD costs about n squared. Better answers, more work. It's the same answer TD converges to. You just get it directly, from the data you already have. And you can update it incrementally, one transition at a time, without solving from scratch. Least squares TD extends to q values too, and even to whole policy iteration.

Experience replay

Or keep it simple: experience replay. Store the transitions in a buffer. Then sample old ones at random, and update on them again. Each experience gets used many times. And random sampling breaks up the order. One catch: old data came from old policies. Replay is off-policy, whether you like it or not. Replay is also a form of batch learning: go over the past again and again, a little at a time. One more benefit: rare, important experiences don't get lost. They stay in the buffer.

A network instead

Now swap the table for a neural network. The observation goes in. One q value per action comes out. Q-learning's update becomes a loss to minimise: the squared TD error. But we stop the gradient through the target. We treat the target as a constant. So it's not a real loss. It just happens to have the right gradient. And we only score the action we took. The other outputs get no feedback on this step. With three actions, the network has three outputs. Each output is one action's q value. The rest is plain deep learning: a mini batch, a gradient step, and an optimiser like

DQN

Adam. Put it all together, and you get DQN, from DeepMind, more than ten years ago. A network that maps the screen to action values. ε-greedy exploration. A replay buffer. A second, frozen copy of the network for the targets, refreshed every ten thousand steps or so. And an optimiser. Function approximation, bootstrapping, off-policy replay. All three corners of the deadly triad. And it works. Every piece we met in this module, finally in one place. DQN learned to play dozens of Atari games from the raw pixels, with one recipe. How it gets away with that is the story of the next module.

Cloud servers

One more example from class, closer to real work: a cloud of servers. There are small servers and large servers. Jobs come in small, medium and large. The actions: send a job to a small server, send it to a large one, or wait. The reward mixes completion time, utilisation and idle machines. I like giving each part a weight, and playing with the weights. Is that reward correlated with what the client wants? Stop and check. Again. With ε fixed, the reward curve stays noisy. Lower ε slowly, and it settles.

Module seven, done

And that's module seven, done. Too many states, so Bit generalises: with features, with tiles, and now with networks. He knows the deadly triad, and the one table to remember. And the code went from a table to a network, without changing the idea. Next module: deep reinforcement learning. Where everything is a vector.

Play

Games that go with this lecture, and what to look for while you play.

Pirates War RL

Code your captain. Read the sea.

In this lecture: Bring your own captain: Pirates War RL accepts sandboxed JavaScript, a declarative Dense JSON network or a restricted TensorFlow.js package. Train a network offline and send it to sea.

A naval strategy game where every decision belongs to an agent. Write your own captain in sandboxed JavaScript, Dense JSON or a restricted TensorFlow.js package, then raid, defend and capture flags against rival agents, with or without fog of war.

Echo Heist

Teach a little robot. Steal back a city.

In this lecture: Echo Heist can also train a deep-RL learner in the browser, with no server. Compare what it learns with the tabular learner of Lecture 6.

Choose what your partner Echo senses, which actions it can take and what success means, then let a real learner train in your browser: tabular, planning, policy-gradient, deep-RL or imitation. When the door opens, the policy is frozen and Echo runs with what it learned.