Skip to content

4.2 On-policy, off-policy, and value learning

This part of Reinforcement Learning develops the mechanisms below. Work the numerical examples or trace the code before checking the answer.

Concepts from other sections (2)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

  • Dynamic programming (DP): solving a problem by breaking it into overlapping subproblems and storing each subproblem’s answer the first time it is computed, which turns an exponential recursion into a polynomial computation. Read more: Chapter 9: Dynamic programming in Programming and Algorithms
  • On-policy / off-policy: learning about the policy being followed versus learning about a different policy. Read more: 4.4 World models, multiple agents, and RLHF

Learning objectives

  • Compare Monte Carlo, TD, SARSA, and Q-learning data assumptions.

Reading: Sutton and Barto Chapter 5.

Monte Carlo methods need no model of the environment; they learn from complete episodes. First-visit MC estimates V(s)V(s) as the average of the returns that follow the first visit to ss in each episode, and every-visit MC averages the returns after every visit. Off-policy MC learns about one policy while following another, correcting for the mismatch with importance sampling. The cost of needing no model is having to wait until an episode ends before learning anything.

Reading: Sutton and Barto Chapter 6.

Temporal-difference learning combines the benefits of the previous two approaches: like Monte Carlo it needs no model, and like dynamic programming it bootstraps from its current estimates, so it need not wait for an episode to end. TD(0) updates after every step toward a one-step target:

V(s)←V(s)+α[R+γV(s′)−V(s)].V(s) \leftarrow V(s) + \alpha\big[R + \gamma V(s') - V(s)\big].

The same idea applied to action values gives two control algorithms. SARSA is on-policy, using the action a′a' the policy actually takes next:

Q(s,a)←Q(s,a)+α[R+γQ(s′,a′)−Q(s,a)].Q(s, a) \leftarrow Q(s, a) + \alpha\big[R + \gamma Q(s', a') - Q(s, a)\big].

Q-learning is off-policy, using the best next action regardless of what the agent does:

Q(s,a)←Q(s,a)+α[R+γmax⁡a′Q(s′,a′)−Q(s,a)].Q(s, a) \leftarrow Q(s, a) + \alpha\big[R + \gamma \max_{a'} Q(s', a') - Q(s, a)\big].

Taking a max over noisy estimates is biased upward, so double Q-learning keeps two Q-functions, using one to choose the action and the other to evaluate it, which reduces this maximization bias.

What makes a method on-policy or off-policy

Section titled “What makes a method on-policy or off-policy”

A policy is a rule that maps a state to a probability distribution over actions, written π(a∣s)\pi(a \mid s). A Q-table is not a policy. It stores values, and a policy is a rule applied on top of those values, such as “take the largest” (greedy) or “take the largest most of the time and a random action otherwise” (epsilon-greedy). One table can therefore supply two different policies.

Two policies matter in every update:

  • The behavior policy picks the actions and so produces the experience.
  • The target policy is the one the update learns about.

A method is on-policy when the two are the same and off-policy when they differ. The number of tables or networks does not decide it. Q-learning has one table and is off-policy, because it acts with epsilon-greedy and its target assumes the greedy action comes next. The max⁡\max in the update is the sign.

A worked target shows the difference. Suppose R=0R = 0, γ=1\gamma = 1, and the next state has values Q(s′,⋅)=[1,3]Q(s', \cdot) = [1, 3]. The agent explores and takes the first action. SARSA’s target is 0+1=10 + 1 = 1, the value of the action it took. Q-learning’s target is 0+3=30 + 3 = 3, the value of an action it did not take.

MethodBehaves withLearns aboutType
SARSAEpsilon-greedyEpsilon-greedyOn-policy
Q-learningEpsilon-greedyGreedyOff-policy
DQN with a replay bufferOlder versions of itselfCurrent greedyOff-policy
Policy gradient on a language modelThe current modelThe current modelOn-policy

For a language model the state is every token so far, the action is the next token, and the model’s output distribution is the policy. On-policy, off-policy, offline, and online learning carries this distinction into post-training.

Reading: Sutton and Barto Chapters 9-10; Mnih et al., 2015.

Tables of values do not scale to large or continuous state spaces, so the next step is to approximate QQ with a parameterized function. Linear function approximation, Q(s,a)=w⊤ϕ(s,a)Q(s, a) = w^\top \phi(s, a), converges with TD under certain conditions. A neural network Q(s,a;θ)Q(s, a; \theta) is far more expressive but can diverge; the combination of function approximation, bootstrapping, and off-policy learning is known as the deadly triad.

DQN (deep Q-network) made neural Q-learning work with two stabilizers. Experience replay stores transitions and trains on random samples of them, breaking the correlation between consecutive updates. A target network, a periodically updated copy of the Q-network, computes the bootstrap targets so they do not shift with every gradient step. Double DQN carries the double Q-learning idea over: the online network selects the next action and the target network evaluates it, reducing overestimation.

Compare Monte Carlo, TD, SARSA, and Q-learning data assumptions. State the assumptions before applying the result to a different workload.

  1. Why does replay require attention to the behavior policy?

    Answer

    The data distribution may differ from the target policy, changing which update estimators are valid.

  2. Q-learning reads one Q-table both to choose actions and to compute its update. Does that make it on-policy?

    Answer

    No. The table stores values, and two different rules read it. Actions come from epsilon-greedy, while the update’s max⁡\max assumes the greedy action. The behavior and target policies differ, so Q-learning is off-policy.

  3. Does an off-policy method need two tables or two networks?

    Answer

    No. Off-policy describes a mismatch between the policy that generated the data and the policy being learned. Q-learning is off-policy with one table. Double Q-learning and target networks add a second estimator for stability, which is a separate concern.

  4. With R=0R = 0, γ=1\gamma = 1, and Q(s′,⋅)=[1,3]Q(s', \cdot) = [1, 3], the agent takes the first action in s′s'. What target does each of SARSA and Q-learning use?

    Answer

    SARSA uses 11, the value of the action taken. Q-learning uses 33, the largest value in s′s'.

Chapter contents · Next: Policy gradients and actor-critic