Lecture 07 · Part 2

Value Function Methods


What if we drop the actor entirely? Levine shows that an arg max over the advantage is itself a policy — at least as good as whatever policy produced the advantage — and builds the whole value-based family from it: policy iteration, value iteration, fitted Q iteration, and online Q-learning. The lecture ends with the honest theory: with neural networks these methods are not gradient descent and are not guaranteed to converge, because a Bellman backup followed by a projection is not a contraction.

Can we skip learning a policy entirely?

Actor-critic, as covered last lecture, still carries a policy network: sample from the current policy, fit a value function Vϕπ(s)V^\pi_\phi(s), form advantage estimates A^π(si,ai)=r(si,ai)+γVϕπ(si)Vϕπ(si)\hat{A}^\pi(s_i, a_i) = r(s_i, a_i) + \gamma V^\pi_\phi(s_i') - V^\pi_\phi(s_i), and push them through the policy gradient. The question that opens this lecture: “can we maybe omit the policy gradient entirely?” The value function already tells us which states are better than which other states — so just select the actions that lead to the better states.

The formal version runs through the advantage. Aπ(st,at)=Qπ(st,at)Vπ(st)A^\pi(s_t, a_t) = Q^\pi(s_t, a_t) - V^\pi(s_t) measures how much better ata_t is than the average action the policy would have taken. Then argmaxatAπ(st,at)\arg\max_{a_t} A^\pi(s_t, a_t) is the best action available from sts_t if you follow π\pi thereafter.

This slots into the usual three-box anatomy with one box gutted: the orange box still generates samples and the green box fits QπQ^\pi or VπV^\pi, but the blue box no longer does any learning — it just sets the policy to the arg max. Alternating the two steps — evaluate AπA^\pi, then set ππ\pi \leftarrow \pi' — is policy iteration. Step two is trivial with discrete actions (check every action’s advantage); continuous actions wait for the next lecture. The whole puzzle is step one: how do you evaluate Aπ(s,a)=r(s,a)+γE[Vπ(s)]Vπ(s)A^\pi(s, a) = r(s, a) + \gamma \mathbb{E}[V^\pi(s')] - V^\pi(s)? It suffices to evaluate VπV^\pi.

Dynamic programming when you know everything

Suspend the model-free setting for a moment: assume known transition probabilities p(ss,a)p(s' \mid s, a) and state and action spaces small enough to enumerate — a grid world with 16 states and 4 actions is the mental picture. Then VπV^\pi is literally a table of 16 numbers and the transitions are a 16×16×416 \times 16 \times 4 tensor; this is what “tabular RL” means. The bootstrapped update from lecture 6 can now be computed exactly, expectations and all:

Vπ(s)Eaπ(as) ⁣[r(s,a)+γEsp(ss,a)[Vπ(s)]]V^\pi(s) \leftarrow \mathbb{E}_{a \sim \pi(a \mid s)}\!\left[r(s, a) + \gamma\, \mathbb{E}_{s' \sim p(s' \mid s, a)}[V^\pi(s')]\right]

using the current table entry for Vπ(s)V^\pi(s'). And since the arg max policy is deterministic, the outer expectation collapses:

Vπ(s)r(s,π(s))+γEsp(ss,π(s))[Vπ(s)]V^\pi(s) \leftarrow r(s, \pi(s)) + \gamma\, \mathbb{E}_{s' \sim p(s' \mid s, \pi(s))}[V^\pi(s')]

Repeating this recursion is policy evaluation; it provably converges to a fixed point, which is the true VπV^\pi. (Levine’s aside for the mathematically inclined: the fixed-point condition is a system of linear equations in the table entries, solvable directly with any linear solver — “a good exercise to make sure that you really understand dynamic programming.”)

Policy iteration can then be short-circuited. Since subtracting Vπ(s)V^\pi(s) — a term that doesn’t depend on the action — can’t change an arg max over actions, argmaxaAπ(s,a)=argmaxaQπ(s,a)\arg\max_a A^\pi(s, a) = \arg\max_a Q^\pi(s, a). Picture the Q-function as a table with a row per state and a column per action: the arg max finds the biggest entry in each row (the policy), and evaluating that policy just plugs the index back in to read off the same entry (the value). So skip the indices and take the values directly. That is value iteration:

1. Q(s,a)r(s,a)+γE[V(s)]2. V(s)maxaQ(s,a)\text{1. } Q(s, a) \leftarrow r(s, a) + \gamma\, \mathbb{E}[V(s')] \qquad \text{2. } V(s) \leftarrow \max_a Q(s, a)

No explicit policy ever appears — it lives implicitly inside the max. Plug step two into step one and you don’t even need to store VV: only the Q-table remains.

Neural networks and fitted value iteration

Why not always use a table?

So represent Vϕ:SRV_\phi : \mathcal{S} \rightarrow \mathbb{R} with a neural network and fit it the way lecture 6 fit the critic — least-squares regression onto target values. Using the value-iteration targets gives fitted value iteration:

1. yimaxai(r(si,ai)+γE[Vϕ(si)])2. ϕargminϕ12iVϕ(si)yi2\text{1. } y_i \leftarrow \max_{a_i}\left(r(s_i, a_i) + \gamma\, \mathbb{E}[V_\phi(s_i')]\right) \qquad \text{2. } \phi \leftarrow \arg\min_\phi \frac{1}{2} \sum_i \left\lVert V_\phi(s_i) - y_i \right\rVert^2

A reasonable algorithm — but step one still requires the dynamics, in two ways: the expectation over ss', and, “perhaps more importantly,” evaluating the max requires trying every action from the same state, which you cannot do by running policies in the world unless you can teleport back to a state and try again.

Trading V for Q makes it model-free

Return to policy iteration and write the policy-evaluation recurrence for the Q-function instead of the value function:

Qπ(s,a)r(s,a)+γEsp(ss,a)[Qπ(s,π(s))]Q^\pi(s, a) \leftarrow r(s, a) + \gamma\, \mathbb{E}_{s' \sim p(s' \mid s, a)}\left[Q^\pi(s', \pi(s'))\right]

The change looks cosmetic but is “a very very important one”: the distribution being sampled is conditioned on the (s,a)(s, a) tuple you already have, so as π\pi changes, the samples you need do not. The policy appears in exactly one place — as an argument to the Q-function at ss' — not in the simulator. Any bucket of (s,a,s)(s, a, s') transitions, collected by any policy, can fit QπQ^\pi for every π\pi. “This is the basis of most value-based model-free RL algorithms.”

Applying the max trick to this recurrence gives the workhorse of the lecture.

What is this optimizing? Step three minimizes the Bellman error — the squared difference between Qϕ(s,a)Q_\phi(s, a) and the targets. If that error is driven to zero, then Qϕ(s,a)=r(s,a)+γmaxaQϕ(s,a)Q_\phi(s, a) = r(s, a) + \gamma \max_{a'} Q_\phi(s', a'), which is an optimal Q-function corresponding to the optimal policy. In the tabular case the error does go to zero and you recover QQ^\star. If it is not zero, “most guarantees are lost when we leave the tabular case.”

Online Q-learning and exploration

Set the hyperparameters to their smallest values — one transition, one target, one gradient step — and fitted Q iteration becomes classic online Q-learning (Watkins): take an action, observe (si,ai,si,ri)(s_i, a_i, s_i', r_i), compute yi=r(si,ai)+γmaxaQϕ(si,a)y_i = r(s_i, a_i) + \gamma \max_{a'} Q_\phi(s_i', a'), then

ϕϕαdQϕdϕ(si,ai)(Qϕ(si,ai)yi)\phi \leftarrow \phi - \alpha \frac{dQ_\phi}{d\phi}(s_i, a_i)\left(Q_\phi(s_i, a_i) - y_i\right)

where the parenthesized quantity is the temporal difference error. It is off-policy: the action in step one need not come from the latest greedy policy. And it had better not, at least not exactly. The greedy arg max policy is deterministic, and if the initial Q-function is bad — not random, arbitrary — it will commit to the same poor action every time it enters a state, potentially “in perpetuity,” never discovering that better actions exist. So inject randomness:

  • Epsilon-greedy: take the greedy action with probability 1ϵ1 - \epsilon, and each other action with probability ϵ/(A1)\epsilon / (|\mathcal{A}| - 1). Simple, ubiquitous, and what you implement in homework 3; a common refinement anneals ϵ\epsilon from large (bad early Q-function) to small (trustworthy late Q-function).
  • Boltzmann (softmax) exploration: π(atst)exp(Qϕ(st,at))\pi(a_t \mid s_t) \propto \exp(Q_\phi(s_t, a_t)). Two advantages over epsilon-greedy: two nearly-equal actions get nearly-equal probability instead of one hogging the greedy mass, and an action already known to be terrible is almost never wasted exploration.

More sophisticated exploration gets its own lectures in the second half of the course.

The sad theory: why none of this converges with neural nets

Does value iteration converge, and to what? Define the Bellman operator on the value table (a vector with one entry per state):

VV^\star — the value function of the optimal policy, satisfying V(s)=maxa[r(s,a)+γE[V(s)]]V^\star(s) = \max_a \left[r(s, a) + \gamma \mathbb{E}[V^\star(s')]\right] — is a fixed point of B\mathcal{B}, and it always exists, is always unique, and always yields the optimal policy through the arg max. So the question is whether the fixed point iteration VBVV \leftarrow \mathcal{B}V reaches it.

Fitted value iteration adds a second operator. Supervised learning searches a hypothesis set Ω\Omega — all neural nets of your architecture — for the element closest to the targets, and since the targets are exactly BV\mathcal{B}V, step two is a projection: ΠV=argminVΩ12V(s)V(s)2\Pi V = \arg\min_{V' \in \Omega} \frac{1}{2} \sum \lVert V'(s) - V(s) \rVert^2. Fitted value iteration in one line is VΠBVV \leftarrow \Pi \mathcal{B} V. Geometrically: BV\mathcal{B}V steps off the manifold of representable functions, and Π\Pi drops it back down at a right angle, in the 2\ell_2 norm. Π\Pi is itself a contraction in 2\ell_2 — projecting two points onto a line can only bring them closer.

So: value iteration converges in the tabular case; fitted value iteration does not converge in general — and often doesn’t in practice. The same story holds verbatim for fitted Q iteration (QΠBQ\mathcal{Q} \leftarrow \Pi \mathcal{B} \mathcal{Q} with the max moved after the transition operator), for online Q-learning, and — a sad corollary — for the actor-critic value fitting of last lecture, which also composes a bootstrap backup with a projection.

One seeming contradiction remains: step three looks like regression, and regression converges — isn’t this just gradient descent? No. “Q-learning is not actually gradient descent”: the target values themselves depend on QϕQ_\phi, but no gradient flows through them, so the update is not the gradient of any well-defined objective. You could differentiate through the targets — the result is the residual algorithm, which is guaranteed to converge but has such poor numerical properties that plain, unguaranteed Q-learning “tends to work much much better” in practice. The next lecture is about making it work.

Check yourself

Derivation and calculation exercises in the lecture’s spirit — work them on paper.

Problem 7.1 policy evaluation as linear algebra

Levine notes that tabular policy evaluation is really a system of linear equations. Take a two-state MDP with a deterministic policy π\pi: from s1s_1 the policy’s action moves to s2s_2 with probability 1 and earns reward 0; from s2s_2 it stays in s2s_2 with probability 1 and earns reward 1. With γ=0.9\gamma = 0.9, write the fixed-point equations Vπ=r+γTVπV^\pi = r + \gamma T V^\pi, solve them exactly, and confirm that repeated application of the bootstrapped update converges to the same answer from Vπ=(0,0)V^\pi = (0, 0).

Show solution

The equations are Vπ(s1)=0+γVπ(s2)V^\pi(s_1) = 0 + \gamma V^\pi(s_2) and Vπ(s2)=1+γVπ(s2)V^\pi(s_2) = 1 + \gamma V^\pi(s_2). The second gives Vπ(s2)=1/(1γ)=10V^\pi(s_2) = 1/(1 - \gamma) = 10; substituting gives Vπ(s1)=0.9×10=9V^\pi(s_1) = 0.9 \times 10 = 9. In matrix form this is (IγT)Vπ=r(I - \gamma T)V^\pi = r with T=(0011)T = \begin{pmatrix} 0 & 0 \\ 1 & 1 \end{pmatrix} and r=(0,1)r = (0, 1)^\top, solvable by any linear solver. Starting the recursion from (0,0)(0,0): after one sweep (0,1)(0, 1), then (0.9,1.9)(0.9, 1.9), then (1.71,2.71)(1.71, 2.71), …; each sweep adds γk\gamma^k-scaled corrections, and the iterates converge geometrically to (9,10)(9, 10) — exactly the contraction-by-γ\gamma behavior of the Bellman backup, here for a fixed policy.

Problem 7.2 arg max policy improvement

Show that the implicit policy π(as)\pi'(a \mid s) that puts probability 1 on argmaxaAπ(s,a)\arg\max_a A^\pi(s, a) satisfies Eaπ[Aπ(s,a)]Eaπ[Aπ(s,a)]\mathbb{E}_{a \sim \pi'}[A^\pi(s, a)] \ge \mathbb{E}_{a \sim \pi}[A^\pi(s, a)] for every state, computing the right-hand side explicitly. Why does the argument make no assumption about the quality of π\pi?

Show solution

First the right-hand side: Eaπ[Aπ(s,a)]=Eaπ[Qπ(s,a)]Vπ(s)=Vπ(s)Vπ(s)=0\mathbb{E}_{a \sim \pi}[A^\pi(s, a)] = \mathbb{E}_{a \sim \pi}[Q^\pi(s, a)] - V^\pi(s) = V^\pi(s) - V^\pi(s) = 0, since VπV^\pi is by definition the expectation of QπQ^\pi under the policy’s own action distribution. The left-hand side is maxaAπ(s,a)\max_a A^\pi(s, a), and a maximum is always at least the mean: maxaAπ(s,a)Eaπ[Aπ(s,a)]=0\max_a A^\pi(s, a) \ge \mathbb{E}_{a \sim \pi}[A^\pi(s, a)] = 0. So the greedy action is at least as good, in advantage terms, as the average action π\pi takes — with equality only when π\pi already concentrates on maximizing actions. Nothing in the chain used any property of π\pi beyond the definitions of QπQ^\pi, VπV^\pi, and AπA^\pi; that is why even an arbitrary random policy improves under the arg max construction, which is the engine of policy iteration.

Problem 7.3 locating the policy dependence

Consider the two policy-evaluation recurrences from the lecture: Vπ(s)r(s,π(s))+γEsp(ss,π(s))[Vπ(s)]V^\pi(s) \leftarrow r(s, \pi(s)) + \gamma\, \mathbb{E}_{s' \sim p(s' \mid s, \pi(s))}[V^\pi(s')] and Qπ(s,a)r(s,a)+γEsp(ss,a)[Qπ(s,π(s))]Q^\pi(s, a) \leftarrow r(s, a) + \gamma\, \mathbb{E}_{s' \sim p(s' \mid s, a)}[Q^\pi(s', \pi(s'))]. You have a fixed buffer of transitions (si,ai,si)(s_i, a_i, s_i') collected by an old policy, and π\pi keeps changing as policy iteration proceeds. Which recurrence can you keep fitting from the buffer without collecting new data, and exactly why does the other one fail?

Show solution

The Q recurrence works. Its sampling distribution p(ss,a)p(s' \mid s, a) is conditioned on the stored (si,ai)(s_i, a_i) pair, which is fixed data — when π\pi changes, the distribution the stored sis_i' were drawn from does not change, so the samples remain valid forever. The policy appears only as an argument to the Q-function, Qπ(si,π(si))Q^\pi(s_i', \pi(s_i')), which is a network evaluation, not an environment interaction. The V recurrence fails because π\pi sits on the right of the conditioning bar: the expectation is over sp(ss,π(s))s' \sim p(s' \mid s, \pi(s)), so when the policy changes its action at ss, you need next-state samples for the new action — which requires returning to ss and trying a different action, impossible without teleportation. This one-symbol difference is why fitted Q iteration is a model-free, off-policy algorithm and is “the basis of most value-based model-free RL algorithms.”

Problem 7.4 exploration rule arithmetic

A state has three actions with current Q-values Q=(1.0,0.9,5.0)Q = (1.0,\, 0.9,\, -5.0). Compute the action probabilities under epsilon-greedy with ϵ=0.1\epsilon = 0.1 and under Boltzmann exploration π(a)exp(Q(a))\pi(a) \propto \exp(Q(a)). Which of Levine’s two arguments for Boltzmann over epsilon-greedy do the numbers illustrate?

Show solution

Epsilon-greedy: the greedy action gets 1ϵ=0.91 - \epsilon = 0.9; the other two split ϵ\epsilon evenly, ϵ/(A1)=0.05\epsilon / (|\mathcal{A}| - 1) = 0.05 each. So (0.9,0.05,0.05)(0.9, 0.05, 0.05). Boltzmann: exp(1.0)2.718\exp(1.0) \approx 2.718, exp(0.9)2.460\exp(0.9) \approx 2.460, exp(5.0)0.0067\exp(-5.0) \approx 0.0067; the sum is 5.185\approx 5.185, giving probabilities (0.524,0.474,0.001)\approx (0.524, 0.474, 0.001). Both of Levine’s arguments appear at once. First, actions 1 and 2 are nearly equally good, and Boltzmann explores them nearly equally (0.5240.524 vs 0.4740.474), while epsilon-greedy gives the marginal winner eighteen times the probability of the runner-up. Second, action 3 is already known to be terrible, and Boltzmann essentially never wastes a sample on it (0.1%0.1\%), while epsilon-greedy insists on trying it a full 5%5\% of the time.

Problem 7.5 contractions in mismatched norms

B\mathcal{B} is a contraction with coefficient γ\gamma in the infinity norm, and the projection Π\Pi onto the hypothesis set Ω\Omega is a contraction (coefficient 1) in the 2\ell_2 norm. (a) If both were contractions in the same norm, what could you conclude about fitted value iteration VΠBVV \leftarrow \Pi \mathcal{B} V? (b) Explain, via the lecture’s geometric picture, how the composition can nonetheless move VV further from VV^\star. (c) What does this imply about the actor-critic algorithm of lecture 6?

Show solution

(a) Contraction coefficients compose multiplicatively within a single norm: ΠBVΠBVˉ1γVVˉ\lVert \Pi\mathcal{B}V - \Pi\mathcal{B}\bar{V} \rVert \le 1 \cdot \gamma \lVert V - \bar{V} \rVert. The composition would be a γ\gamma-contraction, would have a unique fixed point, and fitted value iteration would converge to it — the tabular convergence argument would go through. (b) The guarantees live in different geometries. The backup shrinks the largest per-state gap to VV^\star; the projection then moves the point back onto Ω\Omega along the shortest Euclidean path — but “as close as possible in 2\ell_2” to BV\mathcal{B}V can be far from VV^\star in either norm. In the lecture’s picture, BV\mathcal{B}V steps toward the star, off the line representing Ω\Omega, and the right-angle projection lands at a point on the line further from the star than VV was; iterating can drift away indefinitely. Neither step ever violates its own contraction property. (c) The critic update in actor-critic is exactly a bootstrap backup followed by an 2\ell_2 regression — the same ΠB\Pi \mathcal{B} composition — so fitted bootstrapped policy evaluation inherits the same failure: it is not guaranteed to converge with function approximation either.