You have a fixed log of robot experience and you may not collect more. A greedy policy trusts its value model where the log is empty, and its pick earns 1 where the model promised 9. Three fixes keep the policy where the data is.
Pick a policy and drag the coverage of the log. Watch where each policy lands and what it earns. Then we build, piece by piece, why the max lies, and the three fixes: a leash, a pessimist and a critic that never leaves the data.
You need Q-learning (the TD target with a max over next actions) and a little Python. We build the rest from zero.
Lecture 17 of 25 in Introduction to Robot Learning, CMU 16-831 (Spring 2024), taught by Guanya Shi, with a guest lecture on offline RL by Aviral Kumar. Slides on the course lectures page. Every idea is re-taught here in our own words and devices. Before: Lecture 16. Next: Lecture 18, Inverse RL.
Illustrative toy: one state, one action between 0 and 1. The log is a bell curve centred at 0.4 with the width you set. The value model matches the true reward where the log has data and guesses high where it does not.
Chapter 0
See why a value that is too high fixes itself online and stays wrong offline
A warehouse robot has ten thousand logged picks from teleoperation. A hospital has years of treatment records. Each log is large, and each comes with one rule: you may not collect more. A bad trial smashes an arm into a shelf, or harms a patient.
Offline reinforcement learning (also called batch RL) learns the best policy it can from a fixed log of transitions, with no new interaction. No trying, no exploring.
Q-learning is off-policy: its target does not care which policy collected the data. So a fixed log looks like ideal Q-learning food. Here is the catch. Say one action got one lucky sample of 9 while its true value is 3. Online, the policy tries it, sees 3, and the estimate falls. Offline, nothing ever corrects it.
Pink bars are the estimates, yellow ticks the true values. Choose online or offline, then press Act. Online, each try adds a real sample.
Illustrative toy with no reward noise. Action a1 has 20 logged samples of 5. Actions a0 and a2 each have one lucky sample, 9 and 7. True values are 3, 5 and 4.
Work it. Online, a0 drops from 9 to (9 + 3)/2 = 6 after one try. Then a2 looks best and drops to 5.5. After five wrong tries, every estimate is honest and the policy keeps a1. Offline, the policy picks a0 forever and earns 3 where it could earn 5.
The symptom is a policy whose predicted value keeps rising while its real return stays low. To measure it, compare the critic's value of the chosen actions with a held-out rollout. The fix is the rest of this lesson.
Chapter 1
Join two mediocre logged paths into one that beats both
The cheapest use of a log is to copy it. Behaviour cloning (BC) uses supervised learning for "in this state, do what the log did." It is safe, because it never leaves the data. It also can never beat the data.
Offline RL aims higher: a policy better than anything in the log. It gets there by stitching, joining good pieces of different logged trajectories.
Take four waypoints. The log holds two trips: A to B to D, and D to B to C. The goal is C. No logged trip goes from A to C.
Faint blue lines are the two logged trips. Start at A. Choose Copy or Stitch and watch where the robot ends.
Illustrative toy. Reward 1 for the step into C, 0 elsewhere, discount 0.9.
A copier at A follows trip 1 and stops at D. Reward 0.
A value learner runs the Bellman backup on the same log. The step B to C earns 1, so the value of B is 1. The step A to B earns 0, then B is worth 1, so the value of A is 0 + 0.9 × 1 = 0.9. The policy at A now prefers B, and at B prefers C. Route A, B, C: a path no one logged.
The symptom is an offline policy that does no better than BC. To measure it, count how many test starts reach the goal by a route that crosses two logged trips. If none do, the method is not stitching; check that it bootstraps over more than one step.
Chapter 2
Find the one symbol in the TD target that asks about unseen actions, and watch a value run away
Offline Q-learning is fitted Q-iteration on the log: regress Q(s, a) toward a target, again and again. The state s, the action a, the reward and the next state s′ all come from the log. They happened. The danger sits in one place.
Call the reward r, the discount γ (gamma), and the learned value of action a′ in the next state Q(s′, a′). The target is:
y equals r plus gamma times the max over a-prime of Q of s-prime and a-prime.
Look at every action a-prime in the next state s-prime. Take the largest Q. Shrink it by gamma, add r, and call it y.
With made-up numbers: the log only took a1, worth 5. The network guesses 9 for a0 and 7 for a2. Honest: y = 0.25 + 0.95 × 5 = 5.0. Naive: y = 0.25 + 0.95 × 9 = 8.8. The max added 3.8 from an action nobody tried.
Why is the error high and not just noisy? A network guesses unseen actions with errors in both directions. The max picks the luckiest positive one, every time.
Blue a1 is in the log. Grey bars are the network's guesses for unseen actions, 4 and 2 above a1. Press a backup button several times.
Illustrative toy: one state that loops to itself, reward 0.25 per step, gamma 0.95, so the true value is 0.25 / 0.05 = 5. The honest backup takes the max over logged actions only.
The naive values go 5, 8.8, 12.41, 15.84, and settle at 81: sixteen times the truth. A larger network or longer horizon makes it worse.
The symptom is a Q-value that climbs every epoch while returns stay flat. To measure it, log the mean Q of the policy's actions next to the mean discounted return in the data. A gap that grows is this spiral. The fix is to keep the max off unseen actions.
Chapter 3
Draw where the log has data, and see why a bigger log of the same kind does not help
Some policy collected the log: a teleoperator, an old controller, a mix. Call it the behaviour policy πβ(a | s), said "pi-beta of a given s": how often each action appears in the log at state s.
An action is in support if πβ gives it real probability, so the log has examples. It is out of distribution (OOD) if πβ is near zero. Distributional shift is the gap between the actions the learned policy wants and the actions πβ provided.
This is the most important picture of the lecture. Draw the action axis. The log is a blue hump. The learned value model fits the true reward under the hump and is free to guess anywhere else. A greedy optimizer climbs the model to its peak.
Drag the probe along the action axis. Then switch to a ten times bigger log of the same kind, and to a wider log.
Illustrative toy. The model's wobble under the hump shrinks with 1 over the square root of the log size. Off the hump it guesses high no matter how big the log is.
With the narrow log, the model promises 9.1 at action 1.0. The truth there is 1.0. Ten times more data shrinks the wobble under the hump by √10 ≈ 3.2. The guess at 1.0 does not move, so the greedy peak stays there. Only the wide log covers 1.0 and kills the fantasy.
The symptom is a team that adds data and sees no gain. To measure it, estimate πβ(a | s) with a density model and check it at the actions the policy picks. If it is near zero there, the fix is coverage of those actions or a method that stays in support.
Chapter 4
Limit how far the policy may move from the behaviour policy, and find the leash length that works
The policy keeps leaving the data. The direct answer: do not let it. A policy constraint changes the improvement step so the learned policy π stays close to πβ. If π only proposes logged actions, the max in the target never meets an unseen one.
Measure closeness with a divergence D, a number that is 0 when two distributions match and grows as they differ. KL divergence is the usual choice. Call the leash length ε (epsilon).
pi equals the argmax over pi of the expected Q of s and a, with a drawn from pi, subject to D of pi and pi-beta being at most epsilon.
Among all policies pi whose distance D from pi-beta is at most epsilon, pick the one with the highest average Q.
With made-up numbers: pi and pi-beta are bell curves of width 0.1, with pi-beta centred at 0.4. Their KL is the squared gap of the centres over 2 × 0.1². With epsilon = 0.5 the centre may move 0.1 × √(2 × 0.5) = 0.1, so pi can reach 0.5, where the true reward peaks.
This family includes BCQ (batch-constrained Q-learning, Fujimoto et al., 2018) and BRAC (behaviour-regularized actor-critic, Wu et al., 2019).
The purple band is how far the policy may move from the log's centre. Drag epsilon from short to long and watch the true reward of the pick.
Illustrative toy: the same log as the hero at width 0.1. The reach is 0.1 × √(2ε), from the KL of two bell curves of equal width.
| Leash ε | Behaviour | Result |
|---|---|---|
| Too short | Clings to the log | Imitation, no stitching |
| About right | Improves inside support | The best honest action |
| Too long | Reaches unseen actions | The fantasy returns |
The symptom is one epsilon that works on one task and fails on the next. To measure it, sweep epsilon and plot return. One global leash is crude: a dense state could roam, a sparse one cannot. Chapters 5 to 7 adapt to the local data instead.
Chapter 5
Push down the values of rare actions and lift the logged ones, so the max avoids the fantasy by itself
A leash leaves the critic wrong. Conservative Q-learning (CQL; Kumar, Zhou, Tucker and Levine, 2020) fixes the critic. If unseen actions look bad, the max skips them with no leash at all.
CQL adds one term to the usual Bellman regression. Call the Bellman loss LBellman and the strength α (alpha). A wide action distribution μ (mu) proposes actions to push down; in practice it favours the ones the critic likes best.
L-CQL equals alpha times the expected Q with a drawn from mu, minus the expected Q over logged pairs, plus L-Bellman.
Fit Q to the TD targets as usual. Also lower the Q of actions that mu proposes, and raise the Q of actions the log took, with strength alpha.
With made-up numbers: alpha = 1, mu proposes an action with Q 9, the logged action has Q 5. The extra term is 1 × (9 − 5) = 4, and its gradient pushes the 9 down and the 5 up.
Where does the critic settle? Take one state, a table of Q-values, and the mean logged reward r̂(a) as the target. Set the slope of the loss to zero for one action. The share of the log on action a is πβ(a).
Q of a equals r-hat of a, minus alpha times the quantity mu of a over pi-beta of a, minus one.
Divide how often mu proposes a by how often the log took a. Subtract 1, multiply by alpha, and take that off r-hat. A rare action loses a lot; a common one gains a little.
With made-up numbers: r-hat is 9, 5, 7; pi-beta is 0.05, 0.9, 0.05; mu is a third each; alpha = 1. Q(a0) = 9 − (6.67 − 1) = 3.33. Q(a1) = 5 + 0.63 = 5.63. Q(a2) = 1.33. The max now picks a1.
Pink bars are the conservative Q. Yellow ticks are true values; the blue strip shows each action's share of the log. Drag alpha and find where the pick flips.
Illustrative toy from the worked number: one state, three actions, mu uniform. The settle point is the tabular result above. The true values 3, 5 and 4 are made-up numbers.
The pick flips at α = 4 / (5.67 + 0.63) ≈ 0.64. With many steps this matters more: one over-valued action poisons every backup through it.
The symptom is a CQL policy that hugs the log and gains nothing; alpha is too big. To measure it, compare the critic's value with the logged returns: a gap far below zero means too much pessimism. Lower alpha, or tune it per task.
Chapter 6
Tilt a squared loss so the fit climbs from the mean toward the best logged value
Fixes 1 and 2 still touch unseen actions: one limits how far the policy reaches, one evaluates them to push them down. Implicit Q-learning (IQL; Kostrikov, Nair and Levine, 2021) never evaluates Q at an action the log did not take.
Start from SARSA: its target uses the next action the log took, so it is in-sample and safe. But it learns the value of πβ, good and bad actions averaged. We want the best logged action without naming it.
The tool is the expectile, a squared-loss cousin of a percentile. Fix a state and look at the Q-values of its logged actions. The mean is one summary; a high expectile is a summary near the top. Call the gap between a sample and the estimate u, and a tilt τ (tau) between 0.5 and 1.
L-two-tau of u equals the absolute value of tau minus one-if-u-is-negative, times u squared.
For each sample, square the gap u. Multiply by tau if the sample sits above the estimate, by 1 minus tau if below. The estimate that makes the total smallest is the tau-expectile.
With made-up numbers: logged Q-values 1, 2, 3, 10. At tau = 0.5 the expectile is the mean, 4. At tau = 0.9 only the 10 sits above, so the estimate is (0.1 × 6 + 0.9 × 10) / (0.3 + 0.9) = 8.0. At tau = 0.99 it is 9.77.
Left: four logged Q-values (pink) and the expectile (yellow line). Right: the loss for one sample, steep on the side tau favours. Drag tau.
Made-up values 1, 2, 3, 10 from the worked number. The device finds the expectile by reweighted averaging, the fixed point of the loss.
As tau goes to 1, the expectile goes to the largest logged value. It got there by reweighting logged samples. No unseen action was ever asked.
The symptom is an IQL value that behaves like SARSA and never beats the log; tau is too close to 0.5. With tau near 1, the fit chases rare lucky samples and gets noisy. Sweep tau and watch the value spread across seeds.
Chapter 7
Split the value into an expectile over actions and a mean over outcomes
Why not take the expectile of the whole TD target? Because a high expectile cannot tell a good action from a lucky outcome.
Take one state with two logged actions. "Safe" always ends with reward 3. "Coin" ends with +10 or −10, a fair coin flip of the world. Its true value is 0. The expectile of the coin's targets at tau is 10 × (2τ − 1): at tau = 0.7 that is 4, above safe's 3.
IQL uses two networks. A value network Vψ(s) (V-psi of s) learns an expectile over actions only. A Q network Qθ(s, a) (Q-theta) learns a plain mean over outcomes, so the coin averages out.
L-V of psi equals the expected value, over logged s and a, of L-two-tau of Q-theta-hat of s and a, minus V-psi of s.
For each logged pair, take the gap between Q-theta-hat and V-psi. Apply the tilted loss and average. V-psi settles near the best logged action's Q.
With made-up numbers: Q(safe) = 3, Q(coin) = 0, logged equally often, tau = 0.7. V = (0.3 × 0 + 0.7 × 3) / (0.3 + 0.7) = 2.1.
L-Q of theta equals the expected value, over logged s, a and s-prime, of the square of r plus gamma V-psi of s-prime minus Q-theta of s and a.
Build the target r plus gamma times V-psi of s-prime. Fit Q-theta to it with a plain squared error, so lucky and unlucky outcomes average out.
With made-up numbers: the coin's logged targets are +10 and −10, half each, and the episode ends, so V of s-prime is 0. The squared error is smallest at the mean: Q(coin) = 0. Safe's targets are all 3: Q(safe) = 3.
Warm dots are the coin's outcomes; yellow dots are safe's. Pick a method and drag tau.
Illustrative toy: two logged actions, rewards made up. The naive value of the coin is 10 × (2τ − 1).
Every expectation here runs over logged tuples. Nothing maximises over a′, and no network ever meets an action the log did not take. In short, IQL is SARSA-style TD with a tilted squared loss.
The symptom is a policy that seeks risky, high-variance actions in a noisy world. To measure it, roll out the chosen action several times and check its spread. The fix is the split above: the expectile over actions, the mean over outcomes.
Chapter 8
Turn values into actions four ways, and score each by its true reward
IQL still needs a policy. It never maximises Q over all actions, so it reweights the logged ones. This is advantage-weighted regression (AWR): behaviour cloning where each logged action counts more if it beat the state's value. Call the advantage Q(s, a) − V(s), a temperature β (beta), and the policy's log-probability of a logged action log πφ(a | s).
L-pi of phi equals minus the expected value, over logged s and a, of e to the beta times Q minus V, times log pi-phi of a given s.
Weight each logged action by e to the beta times its advantage. Train the policy to make heavy actions likely, as in behaviour cloning.
From Chapter 7: V = 2.1, Q(safe) = 3, Q(coin) = 0, beta = 3. Safe's weight is e2.7 = 14.9; coin's is e−6.3 = 0.0018. The policy puts 99.99% on safe.
Now put all four on one log. Each turns the same critic into an action differently:
| Policy | How it picks | Where unseen actions enter |
|---|---|---|
| Greedy | The max of Q over all actions | Everywhere: it hunts them |
| Leash | The max of Q inside distance ε of the log | Only past the leash |
| Pessimist (CQL) | The max of a penalised Q | Evaluated, then pushed down |
| In-sample (IQL) | AWR on logged actions | Never |
Markers show each policy's pick on the action axis; bars show what each truly earns. Move the log, shrink it, and make the critic more optimistic.
Illustrative toy, the hero's model. Leash ε = 0.5, CQL α = 0.35 with uniform μ, IQL with AWR at β = 2. The best possible true reward is 7.
With a narrow log, greedy earns about 1 while the other three earn close to 7. Restricting the policy improved it. Now set the width to 0.05 and the centre to 0.25. The peak at 0.5 sits five widths away: the leash earns 2.8, CQL 4.6 and IQL 6.3. Offline RL cannot claim actions the log barely shows.
The lecture's benchmark results (D4RL) show the same order at scale. Imitation scores below policy constraints, and both score below conservative and in-sample methods. The gap is clearest on the maze tasks that need stitching.
The symptom is a method that wins on one dataset and loses on another. To measure it, report results by dataset type: expert, mixed and random logs differ in coverage. Pick the method by how much of the good action space the log covers.
Chapter 9
Generate a whole trajectory at once, so no optimiser can drive the plan off the data
Everything so far learns a value, then extracts a policy. Diffuser (Janner, Du, Tenenbaum and Levine, 2022) uses the log another way. It generates whole plans with a diffusion model: a network that turns noise into data in many small cleaning steps.
The classic model-based recipe fits a one-step dynamics model, then hands it to an optimiser. A strong optimiser finds plans the model wrongly loves, the same trap as the max over unseen actions. Small one-step errors also compound over a long horizon.
Diffuser trains on whole trajectories: an array of states and actions over the planning horizon. To plan, it starts from noise and denoises the whole array together. Sampling and planning are one operation, so no outside optimiser exploits the model.
The purple path starts as noise. Start (teal) and goal (yellow) stay fixed. Faint blue lines show logged pieces. Press Denoise, or run to the end.
Illustrative toy, not a trained model: each step pulls every point toward its neighbours and toward a goal-reaching curve, with less noise each time. It shows the shape of the process.
A real Diffuser runs a trained network at every denoising step, so one plan costs many network calls. A plain policy needs one call per action.
The symptom is a plan that looks smooth but asks for a move the robot cannot make. To measure it, run each planned action through the real dynamics and track the drift from the plan. The fix is to execute the first few actions and replan, as in model predictive control.
Chapter 10
Keep the failure, the three fixes, the numbers, a checklist and the core code
One failure drives the field: the max asks the critic about actions the log never tried, the critic guesses high, and offline nothing corrects it. Each method keeps the policy honest about support in its own way.
| Method | What it does | Cost or catch |
|---|---|---|
| Naive offline Q | Fitted Q-iteration on the log | The max picks unseen actions; values diverge |
| Behaviour cloning | Copies the log | Cannot beat the log; no stitching |
| Leash (BCQ, BRAC) | Keeps π within ε of πβ | One ε is hard to set |
| Pessimist (CQL) | Pushes down rare actions' Q | Can be too pessimistic; slow to fine-tune online |
| In-sample (IQL) | Expectile V, mean Q, AWR policy | Tau and beta are knobs |
| Diffuser | Denoises whole trajectories | Slow sampling; guidance can still drift |
| Term | One line | Number from this lesson |
|---|---|---|
| TD target | y = r + γ max Q(s′, a′) | Honest 5.0, naive 8.8; settles at 81 |
| Stitching | Value flows back across logged trips | V(A) = 0.9 × 1 = 0.9 |
| Leash | KL(π, πβ) ≤ ε | ε 0.5, width 0.1: reach 0.1 |
| CQL settle point | Q = r̂ − α(μ / πβ − 1) | 3.33, 5.63, 1.33 at α 1; flips at 0.64 |
| Expectile | Weight tau above, 1 − tau below | 1, 2, 3, 10 at τ 0.9: 8.0 |
| Luck trap | Expectile of a ±10 coin is 10(2τ − 1) | τ 0.7: 4, true value 0 |
| AWR weight | exp(β(Q − V)) | e2.7 = 14.9 vs e−6.3 = 0.0018 |
pythondef expectile_loss(u, tau=0.7): # u = Q - V, shape [B] w = torch.where(u > 0, tau, 1 - tau) # tilt: above counts tau, below 1 - tau return (w * u**2).mean() def iql_step(s, a, r, s2, done, gamma=0.99, beta=3.0): # all tensors from the log q_t = Q_target(s, a).detach() # logged actions only loss_v = expectile_loss(q_t - V(s)) # V: expectile over actions y = r + gamma * (1 - done) * V(s2).detach() # V(s') where the max used to be loss_q = ((y - Q(s, a))**2).mean() # Q: mean over outcomes w = torch.exp(beta * (q_t - V(s).detach())).clamp(max=100) loss_pi = -(w * pi.log_prob(s, a)).mean() # AWR on logged actions return loss_v, loss_q, loss_pi
Offline RL is often a start, not an end: pretrain on the log, then improve with a little online practice. Plain CQL tends to dip at the switch, because its pessimistic values are on the wrong scale. Cal-QL (Nakamoto et al., 2023) keeps each conservative value at or above a reference policy's value, so fine-tuning starts well. Scaling offline RL to large networks needs its own fixes, which the lecture also covers.
This lesson follows Lecture 17, Offline Reinforcement Learning, of CMU 16-831 Introduction to Robot Learning, Spring 2024, taught by Guanya Shi, with a guest lecture by Aviral Kumar. The slides are on the lectures page. Papers: BCQ, CQL, IQL, Diffuser and Cal-QL. The CQL settle point in Chapter 5 is our own one-state derivation.
Now press Present or Teach and explain from memory: why the max lies offline, why more data of the same kind does not help, and the three fixes. Then go back to the four policies, shrink the log, and say why only greedy fails.