Deep RL with Q-Functions
Last lecture proved that fitted Q-iteration has no convergence guarantee; this one builds Q-learning algorithms that work anyway. Levine adds replay buffers to fix correlated samples and target networks to fix the moving target, arriving at DQN — then diagnoses the systematic overestimation caused by the max in the target, fixes it with double Q-learning, and extends the whole toolkit to multi-step returns and continuous actions, ending at DDPG.
Replay buffers: decorrelating the data
Last time ended on a sour note — value-based methods with function approximation are “not guaranteed to converge” — but “in practice we can actually get some very powerful and very useful reinforcement learning algorithms out of them.” The starting point is fitted Q-iteration: collect transitions with any broad-support policy (these are off-policy methods), compute targets , then regress . Set every loop count to one — one transition, one target, one gradient step — and you get online Q-learning: “if you hear someone say Q-learning, they really mean this method.”
Two things are wrong with it. First, the update in step 3 “is not actually the gradient of any well-defined objective” — appears inside but no gradient flows through it, so “Q-learning is not gradient descent.” (Differentiating properly gives residual gradient methods, so ill-conditioned they work poorly in practice.) Second, sequential transitions are highly correlated: looks a lot like , so the network locally overfits each little window of trajectory in turn, never seeing enough broader context to fit the whole function. Parallel workers, as in online actor-critic, mitigate the correlation; but the fix that is easy and works very well is older — from the 1990s.
The buffer still needs feeding: an initial bad policy won’t have visited the interesting regions, so periodically the latest greedy policy — with epsilon-greedy exploration — is deployed back into the world to refresh with better coverage. The resulting loop: collect data into the buffer, then repeat times (with very common) sample a batch and take the gradient step .
Target networks: stop chasing your own tail
The other problem remains: the target contains , so every gradient step moves the target. The regression “is kind of chasing its own tail — it’s trying to catch up to itself and then changing out from under itself,” and the gradient does not account for the change. Full fitted Q-iteration doesn’t have this problem within step 3 — it regresses to convergence against frozen targets — but training to convergence on the wrong targets isn’t obviously good either. What we want is something in between.
Specializing to recovers the classic deep Q-learning algorithm — DQN — which you will implement in homework 3:
One strangeness: right after a flip the target is zero steps stale, right before the next flip it is — the lag is uneven. A popular alternative updates at every step, with , so every step is lagged by the same amount. Interpolating network parameters sounds like it should produce garbage — networks are not linear in their parameters — but since is just a lagged version of that stays close to it, the connection to Polyak averaging gives the trick some informal justification.
One algorithm, three processes
All the variants — online Q-learning, DQN, fitted Q-iteration — unify into one picture: a replay buffer at the center, with three processes running at different rates. Process 1, data collection, builds a policy from the current (epsilon-greedy or Boltzmann), runs it in the world, and streams transitions into the buffer (a ring buffer of, say, a million transitions, evicting the oldest). Process 2, the target update, periodically copies into — or Polyak-averages — and is typically very slow. Process 3, the main learning loop, samples batches, computes targets with , and updates . Online Q-learning: buffer of size one, all three processes at the same speed. DQN: processes 1 and 3 at the same speed (“a slightly arbitrary choice when you think about it”), process 2 slow, buffer large. Fitted Q-iteration: process 3 nested inside process 2 nested inside process 1.
Are the Q-values accurate? Overestimation and double Q-learning
A Q-function is not just an internal object — it is a prediction of total future discounted reward, so we can check it against reality. On Atari the qualitative picture is excellent: predicted Q rises as return rises; in Breakout the value spikes just before tunneling through to the ceiling; in Pong the Q-values for all actions agree while the ball is far away (many actions still recover) and sharply diverge at the moment only up saves the point. But the absolute numbers are systematically wrong: the predicted Q-value runs consistently above the actual discounted return — a pattern you will reproduce in homework 3.
The route to a fix is noticing where the max comes from: . The same noisy network selects the action and evaluates it — if noise makes some action look erroneously good, that action gets picked and its inflated value gets used. Double Q-learning decorrelates the two roles: network selects the action, network evaluates it (and symmetrically for updating ), so an action selected for its positive noise under gets a sober evaluation from — the system is self-correcting. In practice you don’t add a new network; you use the two you already have:
— the current network selects the action, the target network still evaluates it. This isn’t a perfect decorrelation, since is periodically set to , but “it actually mitigates a large fraction of the problems with overestimation.”
Multi-step returns
Where does learning signal come from in the target ? Early in training the Q-function is garbage, so the second term is mostly noise and the reward does all the work; later the Q-values dominate. Exactly as in actor-critic, we can trade bias for variance with an -step target:
Less biased — the wrong Q-value is multiplied by , which is small — and typically much faster early in training, when the summed rewards carry the signal. The catch: for greater than 1 this is only correct with on-policy data, because the intermediate actions come from the sample, not your current policy. Fixes: ignore the problem (often works very well); dynamically cut the trace, choosing the largest for which the sampled actions all match the current greedy policy; or importance-sample the estimator (Munos et al., “Safe and efficient off-policy reinforcement learning”). Levine also leaves a thought exercise: there is a way to make -step returns off-policy by learning a Q-like object conditioned on additional information — worth puzzling out.
Continuous actions: three ways to take the max
Everything so far assumed the is easy — enumerate the discrete actions. With continuous actions the max appears in two places, and the one inside the target computation sits in the inner loop of training, so it must be fast. Option one: stochastic optimization. The dead-simple version approximates over randomly sampled actions — crude, trivially parallelizable on a GPU, and if overestimation is your worry, a worse max may even help. The cross-entropy method (CEM) iteratively refines the sampling distribution toward the good regions, and CMA-ES is its fancier cousin; these “work okay for up to about 40-dimensional action spaces.” Option two: a function class whose max is closed-form. The NAF architecture outputs , , and a matrix defining a Q-function quadratic in the action — arbitrarily nonlinear in the state, but with and for free, at the cost of representational capacity when the true Q-function isn’t quadratic in .
Option three: learn the maximizer. Train a second network such that , by solving — backpropagate through the Q-function into via . This is DDPG (essentially the much earlier NFQCA), interpretable as a deterministic actor-critic method but “simplest to think of conceptually as a Q-learning algorithm”:
Making it work in practice
Q-learning methods are “quite a bit more finicky to use than policy gradient methods.” Debug on easy, reliable problems first — separate bug-hunting from hyperparameter tuning. Expect wild variation across problems and seeds: on Pong every seed produces a near-identical curve; on Venture some seeds work and some fail completely — run several, and remember flukes come in both directions. Large replay buffers (around a million transitions) improve stability — the algorithm looks more like fitted Q-iteration. Be patient: performance “might be no better than random for a long time” while exploration finds the good transitions, so schedule epsilon from high to low (early on the Q-function is garbage anyway, so random exploration does the heavy lifting), schedule learning rates, and use Adam.
Finally: double Q-learning “helps a lot in practice, is very simple to implement, and basically has no downsides” — use it. -step returns help a lot early but bias the objective, especially for large — use with care.
Check yourself
Work these on paper, in the spirit of the lecture’s derivations and back-of-envelope arguments.
Two actions at state have true Q-values . Your target network’s estimates carry independent noise: each is or with probability . Compute and compare to the true value . What happens with actions instead of 2?
Show solution
Enumerate the four equally likely noise outcomes: ; ; ; . So — exactly the lecture’s “75 percent chance at least one noise is positive” argument made quantitative. With actions, the max is only if all noises are negative, probability , so , which approaches as grows: the more actions you max over, the worse the overestimation. The bias then compounds, since each overestimated target inflates the Q-values used to compute the next targets.
At the true Q-values are , . The current network reports , (noise made look great); the target network reports , . With and , compute the standard Q-learning target (using only ) and the double Q-learning target. Which is closer to the true next value of 1?
Show solution
Standard: select and evaluate with , so — a modest overestimate, and had the noise pattern been flipped it would pick up whichever action inflated. Double: (the current network’s noise picks the wrong action), but the evaluation comes from : . Here the decorrelated noises punish the inflated pick — the selected action’s value is judged by a network that doesn’t share the flattering error, which is the self-correction mechanism. Note double-Q is not uniformly closer on every single sample (here it underestimates); the claim is about the systematic bias: standard Q-learning’s error is always pushed positive by the shared max, while double-Q’s errors can land on either side and largely cancel in expectation.
Your deterministic greedy policy would take actions at the states along a stored trajectory, and the stored actions match at steps and but differ at . You want an -step target for time . What is the largest that keeps the target valid off-policy, and why does never have this problem?
Show solution
The -step target uses the stored rewards , which are only correct for your policy if the stored actions at steps are the ones your policy would take ( itself is fine — the Q-function conditions on it). Stored actions match at but not , so rewards through would be off-policy: the largest valid choice is , using . For the target uses no sampled action beyond : the reward is conditioned on , and the next step is handled by the max rather than the logged action — which is precisely the mechanism that makes one-step Q-learning off-policy in the first place. This dynamic trace-cutting works well when data is mostly on-policy and the action space is small, so matches are common.
With the flip-every- scheme, a target computed right after a flip uses parameters 0 steps stale, and one computed just before the next flip uses parameters steps stale. Polyak averaging instead mixes a fraction of the current parameters in every step. With , roughly how many steps until the target network is “half new” — i.e., the total weight on parameters older than the switchover drops below one half? Compare to .
Show solution
After Polyak steps, the weight remaining on the original is (each step keeps fraction of the old mixture). Setting : steps. So gives an effective time constant of about steps, with a half-life near 700 — every target is lagged by the same effective amount, removing the uneven staleness of the flip scheme (0 to steps, averaging around ). The interpolation of nonlinear network parameters is defensible here exactly because is a gradually lagged version of and never strays far from it — the Polyak-averaging connection Levine cites; mixing two independently trained networks this way would be meaningless.
The dead-simple continuous-action max approximates by the best of uniformly sampled actions. Suppose “good” actions — those within the top fraction of Q-values — occupy a fraction of the action space. What is the probability that at least one of samples is good, and how many samples do you need for 95 percent confidence when ? Why does this scheme degrade in high-dimensional action spaces, and why might its sloppiness even help?
Show solution
Each sample misses the good set with probability , so . For 95 percent: , so — about 300 samples, an easily parallelized mini-batch. The degradation is that this analysis is about quantiles, not distances: in dimensions the region within a small distance of the single best action has volume shrinking exponentially in , so hitting a nearly-optimal action requires exponentially many samples — which is why sampling-based maxes (and refinements like CEM and CMA-ES) are only recommended “up to about 40 dimensions.” The sloppiness can help because the target’s max is exactly what drives overestimation (Problem 8.1): a weaker max selects noise less aggressively, so a cheaper, approximate maximization can act as a mild regularizer against overestimated targets.