Search

Search papers and lessons

Deep Q-learning from Demonstrations

Todd Hester, Matej Vecerik, Olivier Pietquin et al. · 2017

Hester and coauthors (2017) pre-train a deep Q-network on a small set of human game logs with value and imitation losses, then continue with reinforcement learning, which gives strong early Atari scores.

On this page

  • Colour = topic mix
  • Probability
  • Reinforcement learning
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Hester and coauthors show how a reinforcement learning agent can start well on a real task when a small log of an existing controller's play is available. The method, Deep Q-learning from Demonstrations (DQfD), first trains a deep Q-network on the demonstration log alone, using a mix of value losses and a loss that copies the demonstrator's actions. Only after that does the agent start acting and learning from its own experience, with demonstration data kept in the replay buffer. On 42 Atari games it was ahead of a strong reinforcement learning baseline on the first million steps in 41 of them.

Builds on: Deep Q-Network, Double Q-Learning, Prioritized Experience Replay, Behavior Cloning · Leads to: a line of work that combines demonstrations with value-based reinforcement learning

Why it mattered

Most deep reinforcement learning results were obtained in simulators, where an agent can afford millions of poor actions while it learns. Many real systems, such as data centres, vehicles and recommendation services, have logs of how a human or an older controller operated them, but no accurate simulator. This paper showed that a small amount of such data, a few minutes of human play in the Atari games, can be used to give an agent a good starting policy and to speed up learning by a large factor. The paper's central move, pre-training on a log and then switching to reinforcement learning on a mixed buffer, gives a concrete recipe for any setting where such a log exists.

The problem

A deep Q-network learns by bootstrapping: it uses its own estimate of the next state's value as the target for the current estimate. Starting from random weights, it spends millions of steps close to random play, and in a real system those steps are costly. Simply copying the demonstrator's actions with supervised learning gives a policy that looks reasonable, but it cannot improve beyond the demonstrator and does not estimate the long-run value of actions. Naively combining the two fails in a specific way: the demonstration log covers only part of the state space, so the value estimates for untried actions are unconstrained, and a bootstrapped update can propagate those unreal values through the whole network.

The question the paper asks is how to get the benefits of both signals, a policy that starts out like the demonstrator and a value function that keeps improving with reward, without the two undoing each other.

How it works

The method has two phases. The sketch below shows the flow; the walkthrough follows one update of the pre-training phase.

  1. Load the demonstrations into the replay buffer. The demonstration transitions are stored with their state, action, reward and next state. Unlike ordinary replay, these transitions are never overwritten.
  2. Pre-train on demonstrations only. For about 750,000 updates the agent samples a mini-batch from the buffer and does not interact with the environment at all. The loss has four parts: a one-step double Q-learning loss, an n-step double Q-learning loss with n equal to 10, a large margin classification loss that pushes the demonstrator's action above the others, and an L2 penalty on the weights.
  3. Check one pair of numbers in the margin loss. Suppose a state has values 1.0 for the demonstrator's action and 0.5 for another action, and the margin is 0.8. The large margin loss is the maximum over actions of the value plus the margin, minus the demonstrator's value. For the other action that is 0.5 + 0.8 = 1.3, and the maximum is 1.3 because the margin term is zero for the demonstrator's own action. So the loss is 1.3 minus 1.0, which is 0.3. Check: the loss is zero only when the demonstrator's action beats every other action by at least the margin.
  4. Switch to acting. The agent starts acting with an epsilon-greedy policy on its own value estimates, and it writes each new transition into the buffer, overwriting only the oldest self-generated transitions.
  5. Mix the two sources. Each mini-batch is drawn with prioritised sampling, and demonstration transitions get an extra priority bonus so they keep appearing. The supervised loss is switched off for self-generated transitions, so the classification term only ever sees demonstrator actions.
flowchart LR
  D["demonstration log"] --> P["pre-training: TD losses, n-step loss, margin loss, L2"]
  P --> A["act with epsilon-greedy policy"]
  A --> B["replay buffer: demonstrations kept, own data added"]
  B --> U["prioritised mini-batches, TD losses on all data"]
  U --> A

The math

Write Dreplay\mathcal{D}^{replay} for the replay buffer, θ\theta for the parameters of the online network and θ′\theta' for the target network, which is copied from θ\theta every τ\tau steps. The discount factor is γ\gamma.

Step 2 in symbols: the four losses

The one-step double Q-learning loss uses the online network to pick the next action and the target network to value it:

JDQ(Q)=(R(s,a)+γ Q(st+1,at+1max⁡;θ′)−Q(s,a;θ))2,at+1max⁡=arg⁡max⁡aQ(st+1,a;θ).J_{DQ}(Q) = \left(R(s,a) + \gamma\, Q\big(s_{t+1}, a^{\max}_{t+1}; \theta'\big) - Q(s,a;\theta)\right)^2, \qquad a^{\max}_{t+1} = \arg\max_a Q(s_{t+1}, a; \theta).

The n-step version replaces the one-step target by the sum of n discounted rewards followed by a bootstrapped value, with n = 10 in the paper. The margin loss is

JE(Q)=max⁡a∈A[Q(s,a)+l(aE,a)]−Q(s,aE),J_E(Q) = \max_{a \in A}\big[Q(s,a) + l(a_E, a)\big] - Q(s, a_E),

where aEa_E is the demonstrator's action and l(aE,a)l(a_E, a) is zero when a=aEa = a_E and positive otherwise. The paper uses a margin of 0.8 for the positive case. The L2 term JL2(Q)J_{L2}(Q) penalises the squared size of the weights. The full pre-training loss is

J(Q)=JDQ(Q)+λ1Jn(Q)+λ2JE(Q)+λ3JL2(Q),J(Q) = J_{DQ}(Q) + \lambda_1 J_n(Q) + \lambda_2 J_E(Q) + \lambda_3 J_{L2}(Q),

with λ1=1.0\lambda_1 = 1.0, λ2=1.0\lambda_2 = 1.0 and λ3=10−5\lambda_3 = 10^{-5} in the supplementary parameters.

Step 4 in symbols: prioritised sampling

A transition ii has priority pi=∣δi∣+ϵp_i = |\delta_i| + \epsilon, where δi\delta_i is its last TD error and ϵ\epsilon is a small constant. It is sampled with probability

P(i)=piα∑kpkα,P(i) = \frac{p_i^{\alpha}}{\sum_k p_k^{\alpha}},

and the update is weighted by wi=(1N⋅1P(i))βw_i = \left(\frac{1}{N}\cdot\frac{1}{P(i)}\right)^{\beta} to correct the bias that the sampling introduces. The paper's constants are α=0.4\alpha = 0.4, β0=0.6\beta_0 = 0.6 annealed towards 1, and ϵa=0.001\epsilon_a = 0.001 for agent transitions and ϵd=1.0\epsilon_d = 1.0 as the bonus for demonstration transitions.

Step 5 in symbols: the reward scale

Atari scores differ by orders of magnitude between games, so the agent learns from a squashed reward ragent=sign(r) log⁡(1+∣r∣)r_{agent} = \mathrm{sign}(r)\,\log(1 + |r|), while the reported results use the raw game score. The paper uses it to keep rewards on a reasonable scale for the network and to make the agent's reward signal closer to the one the demonstrator effectively played for.

Results

Early performance. Across 42 Atari games, DQfD had higher on-line scores than prioritised dueling double DQN (PDD DQN) over the first million steps in 41 of 42 games. The paper reports that on average PDD DQN needs about 83 million steps to catch up to DQfD, and that PDD DQN never surpasses DQfD in mean score. DQfD also beat pure imitation learning in mean score on 39 of 42 games.

Versus the demonstrator. DQfD learns to beat the best human demonstration it was given in 14 of 42 games. Pure imitation learning was worse than the demonstrator in every game, so the reinforcement learning phase is doing real work.

State of the art on hard games. On 11 of 42 games DQfD's score, averaged over the best 3 million step window across four seeds, is higher than the best previously published deep RL result. On Hero it is 105,929.4 against 23,037.7, and on Private Eye 40,908.2 against 15,806.5. The paper also notes that on Pitfall, no earlier approach had achieved a positive reward.

Ablations. Removing the supervised loss, or the n-step loss, degrades early performance on Montezuma's Revenge and Q-Bert, and the authors say the n-step loss matters nearly as much as the supervised term for initial performance. Against three demonstration-using baselines, Replay Buffer Spiking, Human Experience Replay and Accelerated DQN with Expert Trajectories, DQfD is better on both games shown. The one that comes closest, ADET, uses a cross-entropy loss in place of the margin loss, which the authors identify as the main difference.

Limits. The parameters were tuned informally on six games and then fixed for all 42. The comparison in the 11-game table is not a like-for-like protocol: previous results take the best agent at its best iteration, averaged over 100 episodes, while DQfD's figures are a best window averaged over four seeds. The demonstrations are small, 5,574 to 75,472 transitions per game, and the human player was not always better than the learned policy: on Breakout the best demonstration scored 79, and DQfD reached 308.1 on average. The paper also reports that imitation learning often cannot fit the demonstrator's own actions, even on the demonstration data.

Reading guide

Read the paper in this order, with the lessons each part depends on.

Check yourself

QuestionIn plain words, why does DQfD add a supervised margin loss to the Q-learning losses during pre-training?Show answer

The demonstration log covers only part of the state space and only some actions. Q-learning alone would push the values of untried actions towards the highest value seen in the next state, which is unconstrained data, and that error would spread through the network. The margin loss forces the demonstrator's action to score at least a margin above every other action in each demonstrated state, which grounds those values and makes the greedy policy imitate the demonstrator.

QuestionIn a state, the demonstrator's action has value 2.0 and another action has value 1.6. The margin is 0.8. What is the margin loss for this state?Show answer

The other action gives 1.6 + 0.8 = 2.4, and the demonstrator's own action gives 2.0 + 0 = 2.0 because its margin is zero. The maximum over actions is 2.4. Subtracting the demonstrator's value gives 2.4 − 2.0 = 0.4.

QuestionWhy does DQfD keep the demonstration transitions in the replay buffer for the whole run, instead of letting them be overwritten like the agent's own data?Show answer

Once the agent starts acting, the demonstrations are the only data that is guaranteed to be good, and the agent's early data is poor. If they were overwritten, the agent would lose the anchor that keeps its value estimates grounded, and it would drift back towards random-play behaviour. Keeping them, and boosting their sampling priority with the bonus, lets the agent keep learning from a good signal while it collects its own.

QuestionA team has a log of 10,000 transitions from a human operating a robotic arm and no simulator. They want to start a value-based agent from this log. Which parts of DQfD apply, and what would they check before trusting the early policy?Show answer

They would pre-train on the log with the TD losses and the margin loss, keep the log in the buffer, and then switch to acting with prioritised replay. Before trusting the early policy they would check whether the demonstration covers the states the agent will reach, since the margin loss can only ground values for states present in the log, and they would measure how the learned policy performs against the demonstrator on held-out demonstration states, looking for the imitation-only failure that the paper describes.

Sources

Read the full paper

Todd Hester, Matej Vecerik, Olivier Pietquin et al., 2017. arXiv:1704.03732