Tabular Solution Methods/TD Control: Sarsa, Q-learning, and Double Learning

Lesson 2.62,356 words

TD Control: Sarsa, Q-learning, and Double Learning

With TD prediction in hand, control follows the generalized-policy-iteration pattern with TD as the evaluation step. We build Sarsa (on-policy), Q-learning (off-policy, targeting the optimal policy), and Expected Sarsa that spans the two, then confront the maximization bias every max-based method inherits and fix it with Double Q-learning.

╌╌╌╌

This builds on Temporal-Difference Learning, which developed TD(0) prediction, the TD error, and the optimality of batch TD. Here we turn from predicting a fixed policy's values to improving the policy — control — by slotting TD into the same generalized-policy-iteration loop as every other tabular method, now over state-action values.

Sarsa: on-policy control

Turn now from prediction to control. As with the other tabular methods, we follow generalized policy iteration, this time with TD for the evaluation step. The first change is to learn an action-value function rather than a state-value function: for the current behavior policy we estimate for all and . This is done with the very TD idea from prediction, applied to state–action pairs. An episode is an alternating chain of states and state–action pairs, and we now consider transitions from one state–action pair to the next:

This update runs after every transition from a nonterminal state ; if is terminal, is defined as zero. The rule uses every element of the quintuple that makes up a transition from one state–action pair to the next — and it is that quintuple that gives the algorithm its name, Sarsa.1

Building control on top is straightforward. As in all on-policy methods, we continually estimate for the behavior policy while nudging toward greediness with respect to — typically an -greedy policy, which takes the greedy action most of the time but with probability explores a random one.

Algorithm:Sarsa\textsc{Sarsa} — on-policy TD control, estimate QqQ \approx q_\ast
  1. 1
    Q(s,a)Q(s,a) \gets arbitrary, with Q(terminal,)=0Q(\text{terminal}, \cdot) = 0, for all sS,aAs \in \mathcal{S}, a \in \mathcal{A}
  2. 2
    for each episode do
  3. 3
    initialize SS
  4. 4
    choose AA from SS using a policy derived from QQ (e.g. ε\varepsilon-greedy)
  5. 5
    repeat
  6. 6
    take action AA, observe RR, SS'
  7. 7
    choose AA' from SS' using a policy derived from QQ (e.g. ε\varepsilon-greedy)
  8. 8
    Q(S,A)Q(S,A)+α[R+γQ(S,A)Q(S,A)]Q(S,A) \gets Q(S,A) + \alpha[R + \gamma Q(S',A') - Q(S,A)]
  9. 9
    SSS \gets S'
  10. 10
    AAA \gets A'
  11. 11
    until SS is terminal

Sarsa is on-policy: the action that appears in the target is the action the policy actually takes next, exploration and all. It converges to an optimal policy and action-value function as long as all state–action pairs are visited infinitely often and the policy converges in the limit to greedy — arranged, for example, by an -greedy policy with .

Windy gridworld

The windy gridworld (Example 6.5) shows Sarsa learning control where Monte Carlo cannot easily be applied. The agent moves on a grid from a start to a goal , with the four standard moves, but a crosswind blows upward through the middle columns: the wind strength (in cells shifted up) is across the ten columns, added to the move's result. Every step costs until the goal is reached; the task is undiscounted and episodic.2

The wind makes the shortest path indirect — a straight rightward march is blown past the goal, so the agent must aim below and let the wind carry it up. Run -greedy Sarsa with , , and initial . Early episodes are long (the agent wanders), but the slope of episodes-completed against time steps steepens as sharpens; by about time steps the greedy policy is optimal, reaching the goal in the minimum steps, and continued -greedy exploration holds the average around .

The windy gridworld. The agent goes from S to G on a 7-by-10 grid; the number under each column is the upward wind that displaces the agent after each move. The blue path is the optimal 15-step route Sarsa learns: it must aim below the goal and ride the wind up. Monte Carlo struggles here because some policies never terminate.

Some policies in the windy gridworld never terminate: a policy that pushes the agent into a wall it cannot escape loops forever, so Monte Carlo, which needs an episode to end before it can learn, would hang. Sarsa learns during the episode that such a policy is bad and moves off it, so it never gets stuck. Online, incremental learning is what makes control possible here at all.

Q-learning: off-policy control

One of the early breakthroughs in reinforcement learning was Q-learning, an off-policy TD control method.3 Its update looks almost like Sarsa's, but with one change in the target:

Where Sarsa bootstraps from — the value of the action it will take — Q-learning bootstraps from — the value of the best action available next, whether or not the policy takes it. The learned therefore approximates , the optimal action-value function, directly and independently of the policy being followed. The policy still governs which pairs are visited, but all that is required for convergence is that all pairs keep being updated. Under that condition and the usual step-size schedule, converges with probability 1 to .

Sarsa versus Q-learning at the same next state . Both take one real step to ; Sarsa bootstraps from the value of the action the policy will actually take there (following the same exploratory policy), while Q-learning bootstraps from the maximum over next actions — the greedy value, regardless of what is taken.

The difference shows up on the cliff-walking gridworld. The agent walks from start to goal along the edge of a cliff; stepping off costs and resets it to the start, every other step costs . Q-learning learns the values of the optimal policy, which hugs the cliff edge — but because it still explores -greedily while acting, it occasionally steps off and its online return suffers. Sarsa, learning the values of the policy it actually follows, accounts for the exploration and prefers the longer but safer path away from the edge; its online return is better even though the policy it learns is not optimal. Reduce toward zero over time and both converge to the optimal policy.3 The lesson generalizes: off-policy Q-learning learns the optimal target policy but can behave worse online than on-policy Sarsa, which learns a policy that is safe given its own exploration.

Expected Sarsa

Between Sarsa and Q-learning sits a third method. Take the Q-learning schema but replace the maximum over next actions with the expected value under the current policy — weighting each next action by how likely the policy is to take it:

This is Expected Sarsa. Given the next state it moves deterministically in the same direction that Sarsa moves in expectation — the move its name records. It costs more computation than Sarsa — a sum over actions rather than a single sample — but in return it removes the variance that comes from sampling at random. Given the same experience it generally performs slightly better than Sarsa, and it can safely use on deterministic problems, where Sarsa needs a small and pays for it in slow early learning.

Expected Sarsa also subsumes Q-learning. If the policy used to compute the expectation is the greedy policy while behavior is more exploratory, the expected value is just the max, and Expected Sarsa becomes Q-learning exactly. In this sense, Expected Sarsa is a single algorithm with Sarsa and Q-learning as two endpoints: on-policy when the target policy is the behavior policy, off-policy (and equal to Q-learning) when the target policy is greedy.4

Maximization bias and Double Q-learning

Every control method so far builds its target policy by maximizing: Q-learning's target is greedy, and Sarsa's -greedy policy maximizes too. A maximum over estimated values is being used, implicitly, as an estimate of the maximum of the true values — and that introduces a positive bias.

Consider a single state with many actions whose true values are all exactly zero, but whose estimates are noisy, scattered above and below zero. The true maximum is zero. The maximum of the estimates is almost surely positive, because the max picks out whichever action happened to be overestimated. This systematic overshoot is maximization bias: the same sample is used both to choose the maximizing action and to estimate its value, and that double use tilts the estimate upward.

To address this, break the coupling by keeping two independent estimates. Split the experience into two sets and learn and , each an unbiased estimate of the true . Use one to pick the maximizing action, , and the other to read off its value, . Because was not used to select , its estimate is unbiased: . Swap the roles to get a second unbiased estimate. This is double learning. It doubles the memory but not the computation per step — only one of the two estimates is updated on each step.

Maximization bias and its fix. With a single noisy estimate (left), the max picks the action that happened to be overestimated, so the reported max is biased above the true value 0. Double learning (right) uses Q1 to pick the action and the independent Q2 to value it, decoupling selection from evaluation.

Applied to full MDPs this gives Double Q-learning. On each step, flip a coin. If heads, update using to select the next action and to value it:

If tails, do the same with and swapped. The behavior policy can use both estimates — for instance -greedy in .5

Algorithm:Double-Q-Learning\textsc{Double-Q-Learning} — estimate Q1Q2qQ_1 \approx Q_2 \approx q_\ast
  1. 1
    Q1(s,a),Q2(s,a)Q_1(s,a),\, Q_2(s,a) \gets arbitrary, with Q(terminal,)=0Q(\text{terminal}, \cdot) = 0, for all sS,aAs \in \mathcal{S}, a \in \mathcal{A}
  2. 2
    for each episode do
  3. 3
    initialize SS
  4. 4
    repeat
  5. 5
    choose AA from SS using the policy ε\varepsilon-greedy in Q1+Q2Q_1 + Q_2
  6. 6
    take action AA, observe RR, SS'
  7. 7
    with probability 0.50.5 do
  8. 8
    Q1(S,A)Q1(S,A)+α[R+γQ2(S,argmaxaQ1(S,a))Q1(S,A)]Q_1(S,A) \gets Q_1(S,A) + \alpha\big[R + \gamma\, Q_2(S', \arg\max_a Q_1(S',a)) - Q_1(S,A)\big]
  9. 9
    else
  10. 10
    Q2(S,A)Q2(S,A)+α[R+γQ1(S,argmaxaQ2(S,a))Q2(S,A)]Q_2(S,A) \gets Q_2(S,A) + \alpha\big[R + \gamma\, Q_1(S', \arg\max_a Q_2(S',a)) - Q_2(S,A)\big]
  11. 11
    SSS \gets S'
  12. 12
    until SS is terminal

On the small MDP where plain Q-learning strongly favors a spuriously positive-looking action, Double Q-learning is essentially unaffected by the bias and finds the optimal policy. There are double versions of Sarsa and Expected Sarsa too; the principle — decouple the action selected from the estimate used to value it — is general.

Beyond the table: TD in deep reinforcement learning

The four updates above are one-step, tabular, and model-free. Drop tabular — represent by a neural network instead of a lookup table — and the same TD error drives deep reinforcement learning. This has a cost: bootstrapping, function approximation, and off-policy training together form what Sutton and Barto later call the deadly triad, a combination that can make the learning process diverge. Much of the practical work in deep RL addresses this instability.

Deep Q-Networks. Mnih and colleagues' DQN learned to play Atari 2600 games from raw pixels using exactly the Q-learning update, with a convolutional network.6 Two ideas stabilize the network's TD updates. An experience replay buffer stores past transitions and samples them at random for each update, breaking the temporal correlations that would otherwise make the gradient steps dependent. And a periodically-frozen target network supplies the bootstrap value from an old copy of the weights, so the target does not move with the parameters being trained — the same concern that motivates the warm-started, slow-moving targets throughout tabular DP, now made explicit because a network's fast, correlated updates would otherwise destabilize the fixed point.

DQN adapts tabular Q-learning to a neural network. The Q-learning target still bootstraps from the next state, but the value comes from a frozen target network (theta-minus), and transitions are drawn from a replay buffer rather than the live stream, decorrelating the updates.

Maximization bias, at scale. The bias that Double Q-learning fixes is worse with networks, because the over a noisy network's outputs is systematically optimistic. Van Hasselt, Guez, and Silver's Double DQN ports double learning directly: select the maximizing next action with the online network but read its value from the target network , cutting the overestimation and improving scores on many Atari games.7 This is the same decoupling of selection from evaluation as in the tabular section, applied to two networks that already exist for a different reason.

Combining improvements. Expected Sarsa's variance-reduction motive, TD's -step generalization, and prioritized replay (drawing high-TD-error transitions more often — the same backward-focusing logic as prioritized sweeping) all reappear as components. Hessel and colleagues' Rainbow combined six such extensions — double Q-learning, prioritized replay, dueling networks, multi-step returns, distributional value learning, and noisy exploration — and showed they largely add up, each addressing a distinct weakness of plain DQN.8 Nearly every idea in deep value-based RL is a neural restatement of a one-step tabular update from this lesson.

The tabular-to-deep map. Each tabular idea on the left has a direct descendant in deep value-based RL on the right: Q-learning becomes DQN, Double Q-learning becomes Double DQN, Expected Sarsa's variance control and n-step returns feed multi-step targets, and prioritized sweeping's focus becomes prioritized replay.

What TD unifies, and where it points

TD learning is central to the whole subject. It keeps Monte Carlo's freedom from a model and gains DP's ability to learn before an outcome is known, all in a handful of single-equation updates that a small program can run online. The four methods here are one family under generalized policy iteration: Sarsa evaluates on-policy, Q-learning targets the optimal policy off-policy, Expected Sarsa spans the two, and Double Q-learning removes the maximization bias that afflicts all of them.

These are the one-step, tabular, model-free special cases. The next lesson, n-step bootstrapping, loosens the one-step by backing up from steps ahead, forming a bridge on which TD() and Monte Carlo () are the two ends of a continuum. Planning and learning loosens model-free by folding a learned model back into the same updates. Later the tabular restriction falls away under function approximation, carrying TD into deep reinforcement learning.

The TD error also appears outside algorithms. The quantity — a reward-prediction error, the gap between the prediction and what one observed step delivered — closely matches the phasic firing of the brain's dopamine neurons. That correspondence is the subject of dopamine and the TD error.

Footnotes

  1. Sutton & Barto, §6.4 — Sarsa: On-policy TD Control: the action-value update (6.7) over the quintuple , the Sarsa control box, and convergence under -greedy exploration with ; Example 6.5 (Windy Gridworld).
  2. Sutton & Barto, §6.4, Example 6.5 (Windy Gridworld): the grid with column winds , constant rewards, and -greedy Sarsa (, , ) reaching the optimal 15-step policy by about 8000 time steps; the note that Monte Carlo is hard to apply because some policies never terminate.
  3. Sutton & Barto, §6.5 — Q-learning: Off-policy TD Control (Watkins, 1989): the update (6.8) with , direct approximation of independent of the behavior policy, and Example 6.6 (Cliff Walking) contrasting the online performance of Sarsa and Q-learning. 2
  4. Sutton & Barto, §6.6 — Expected Sarsa: the update (6.9) using , its reduced variance relative to Sarsa, safe use of on deterministic tasks, and how it becomes Q-learning when the target policy is greedy.
  5. Sutton & Barto, §6.7 — Maximization Bias and Double Learning: maximization bias from using a max of estimates as an estimate of the max, Example 6.7's small MDP, and the Double Q-learning algorithm (6.10) with its box, using independent estimates to decouple action selection from value estimation.
  6. Mnih, V., et al. (2015), Human-level Control through Deep Reinforcement Learning, Nature 518:529–533 (DQN) — Q-learning with a convolutional -network learning Atari 2600 from pixels, stabilized by experience replay and a periodically-updated target network.
  7. van Hasselt, H., Guez, A., & Silver, D. (2016), Deep Reinforcement Learning with Double Q-learning, AAAI — porting Double Q-learning to DQN by selecting the next action with the online network and evaluating it with the target network, reducing overestimation.
  8. Hessel, M., et al. (2018), Rainbow: Combining Improvements in Deep Reinforcement Learning, AAAI — the combination of double Q-learning, prioritized replay, dueling networks, multi-step returns, distributional RL, and noisy nets, showing the extensions are largely complementary.

╌╌ END ╌╌