Foundations to Frontier
Tests

Offline reinforcement learning

Offline (batch) reinforcement learning learns a policy from a fixed dataset of past experience, with no further interaction. The central difficulty is that value-based methods must evaluate actions the data never shows, and a learner that maximises over those guesses chases its own errors. This page builds naive offline Q-learning, a batch-constrained variant, conservative Q-learning (CQL) and behaviour cloning on a gridworld with exact evaluation, and measures how each depends on the quality and size of the dataset.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learning only from records

Imagine learning to drive only from recordings of other people’s drives, without ever touching a car. You can copy what they did, or you can try to work out which of their choices worked best and combine them. The trap is guessing about manoeuvres nobody in the recordings ever tried: those guesses may look brilliant on paper and be disastrous on the road. Offline RL methods are careful to stay close to what the data actually shows.

Reinforcement learning lab
012345
A 5×5 slippery grid with four pits (−1), goal +1, −0.04 per step, γ = 0.95. 50 episodes from a near-optimal policy with ε = 0.1 give 450 transitions; nothing else is allowed. Bars, true start value of: 0 the optimal policy (0.251), 1 the behaviour policy (0.121), 2 behaviour cloning (0.251), 3 naive offline Q-learning (0.226), 4 batch-constrained Q (0.251), 5 CQL (0.251). The line on bar 3 is naive Q's own estimate, 0.333; its policy chooses an action absent from the data in 1 visited state(s). Actions the data never shows keep arbitrary initial values, and the max in the target trusts them.
Why engineers care

Offline RL is used where trial and error is expensive or dangerous: treatment policies from medical records, recommendation systems from logs, and robots from previously collected demonstrations.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Offline RL learns from…
  2. 2. The main danger is trusting…
  3. 3. Offline RL suits medicine because…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Offline (batch) RL: learn from a dataset D of transitions (s, a, r, s′) collected by some behaviour policy π_β, with no further interaction.
  • Behaviour cloning (BC): supervised learning of π(a|s) from the data’s actions. It can at best match the data.
  • Extrapolation error: a Q-learning target r + γ max_a′ Q(s′, a′) uses Q at actions a′ that may never appear at s′ in D; their values are unconstrained guesses, and the max selects the most optimistic ones.
  • Batch-constrained Q (BCQ, Fujimoto et al. 2019): restrict the max, and the policy, to actions supported by the data.
  • Conservative Q-learning (CQL, Kumar et al. 2020): add α·E_s[log Σ_a exp Q(s, a) − Q(s, a_data)] to the TD loss, pushing down actions absent from the data.
  • Implicit Q-learning (IQL, Kostrikov et al. 2022): never query unseen actions; estimate an upper expectile of in-data values, then extract a policy by advantage-weighted regression.
  • Coverage: offline RL can only find a good policy if the data covers its actions in the states it visits.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Behaviour cloning can at best…
  2. 2. Extrapolation error comes from…
  3. 3. CQL pushes down Q values of…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Offline Q-learning on a gridworld

Exercise 1.

  • A 5 × 5 slippery grid (slip 0.1) with four pits (−1), goal +1, −0.04 per move, γ = 0.95; exact values by a linear solve.
  • Datasets of episodes from the start state, collected by an ε-greedy version of the optimal policy.
  • Q-values start at random N(0, 0.3²) values; entries the data never touches keep them, like a network’s output on inputs it was never trained on.
  • Naive fitted Q iteration (max over all actions), batch-constrained Q (max and policy over data actions only), CQL (α = 0.1), and behaviour cloning (the most frequent action).

Practice.

  • Start with behaviour cloning: with expert data it is hard to beat, and it is a sanity check for everything else.
  • Use IQL or CQL for mixed or suboptimal data; TD3+BC (TD3 plus a behaviour-cloning term) is a strong, simple baseline.
  • Evaluate offline before deploying: off-policy evaluation (importance sampling, fitted Q evaluation) and small, safe online trials.
  • Check coverage: a dataset from a narrow expert says little about recovering from mistakes.
  • Fine-tune online carefully: offline-trained critics can collapse at the start of online training (Cal-QL addresses this).
  • Tools: D4RL and Minari datasets; CORL and d3rlpy implementations.
def offline_q(data, reward_of, S, gamma, mode, Q0, term, iters=300):
    s, a, s2 = data.T; r = reward_of[s2]; cont = ~term[s2]
    seen = np.zeros((S, 4), bool); seen[s, a] = True
    n = np.zeros((S, 4)); np.add.at(n, (s, a), 1); Q = np.array(Q0, float)
    for _ in range(iters):
        nxt = np.where(seen, Q, -np.inf).max(1) if mode == "constrained" else Q.max(1)
        nxt = np.where(np.isfinite(nxt), nxt, 0.0)
        tot = np.zeros((S, 4)); np.add.at(tot, (s, a), r + gamma * cont * nxt[s2])
        Q = np.where(seen, tot / np.maximum(n, 1), Q0)
    return Q, seen
🐍 Exercise 1: naive and constrained offline Q-learning
Collect a dataset from a near-expert policy, then learn from it alone with naive fitted Q iteration, batch-constrained Q, CQL and behaviour cloning, and evaluate each policy exactly.
📝 Quick check (Level 2)
0/3 answered
  1. 1. With expert data, a strong first baseline is…
  2. 2. TD3+BC adds to TD3…
  3. 3. Standard offline RL datasets include…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why the max is dangerous. If Q̂(s′, a′) = Q(s′, a′) + e(a′) with errors that are not constrained by data, then max_a′ Q̂(s′, a′) ≥ Q̂(s′, a*) and, in expectation, exceeds max_a′ Q(s′, a′) (topic L4-5). Online, acting on an overestimated action produces data that corrects it. Offline, nothing ever corrects it, and bootstrapping spreads the error to every state that leads to s′.

Constraining to the data. If the target uses only actions with data at s′, every Q value in the backup is an average of observed outcomes, and the fixed point is the optimal value of the MDP restricted to supported actions. The learned policy can still combine (stitch) good pieces of different trajectories, which behaviour cloning cannot.

CQL’s penalty. The gradient of log Σ_a exp Q(s, a) − Q(s, a_data) with respect to Q(s, b) is softmax_b(Q(s, ·)) − 1[b = a_data]. Averaged over the data, actions the data rarely takes are pushed down in proportion to how strongly the current Q prefers them. Kumar et al. show that the resulting value of the learned policy is a lower bound on its true value, for a large enough α.

Coverage. Performance guarantees for offline RL depend on a concentrability coefficient: how much more often the target policy visits state–action pairs than the data does. Where the data has no coverage, no method can know the value.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Offline, an overestimated action is never…
  2. 2. Constraining the max to data actions lets Q-learning…
  3. 3. CQL's penalty gradient on an action is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do behaviour cloning, naive offline Q-learning, batch-constrained Q and CQL compare as the dataset’s quality and size change?

History.

  • D. Ernst, P. Geurts and L. Wehenkel (2005) introduced fitted Q iteration for batch RL; S. Lange, T. Gabel and M. Riedmiller (2012) surveyed batch RL.
  • S. Fujimoto, D. Meger and D. Precup (2019) identified extrapolation error and introduced BCQ.
  • A. Kumar, A. Zhou, G. Tucker and S. Levine (2020) introduced CQL.
  • I. Kostrikov, A. Nair and S. Levine (2022) introduced IQL; S. Fujimoto and S. Gu (2021) introduced TD3+BC.
  • J. Fu, A. Kumar, O. Nachum, G. Tucker and S. Levine (2020) released the D4RL benchmark.

Exercise 2.

  • Datasets of 20 or 200 episodes from three behaviour policies: expert (ε = 0.1 around the optimal policy), medium (ε = 0.5) and random.
  • Four datasets of each kind, each with its own random initial Q values; the true value of every learned policy computed exactly. Optimal start value 0.2513.
  • Also recorded for naive Q: its estimate of the start value minus the true value of its policy, and the number of visited states where its policy picks an action absent from the data.

Seed 0, mean true start value of the learned policy:

data episodes behaviour BC naive Q constrained Q CQL naive over-estimate
expert 20 0.121 0.220 −0.215 0.081 0.237 +0.620
expert 200 0.121 0.251 −0.108 0.232 0.251 +0.467
medium 20 −0.494 0.212 −0.204 0.163 0.251 +0.660
medium 200 −0.494 0.251 0.216 0.216 0.251 +0.058
random 20 −0.916 −0.865 −0.839 −0.817 −0.838 +1.204
random 200 −0.916 −0.837 0.207 0.207 0.206 +0.144
  • Naive offline Q-learning failed on narrow data: with expert data its policies were worth −0.215 and −0.108 (−0.302 and −0.345 on seed 1; −0.565 and −0.471 on seed 2), worse than the behaviour policy, while it believed they were worth 0.47 to 1.03 more. More expert data did not fix it, because the same actions stay unseen.
  • CQL was the most reliable: within 0.03 of the optimum in 11 of the 12 expert and medium settings over three seeds; the exception was 0.164, with 20 expert episodes on seed 2.
  • Behaviour cloning matched good data: near-optimal for expert and medium data (the most frequent action of an ε = 0.5 policy is still the optimal one), but useless for random data (−0.837 with 200 episodes).
  • Offline RL beat cloning on random data: with 200 random episodes the Q-learning methods reached about 0.21 on seeds 0 and 2, combining pieces of random trajectories into a near-optimal policy; on seed 1 they reached only −0.16 to −0.34, still far above cloning (−0.825).
  • Batch-constrained Q fixed the worst failures but lagged CQL when data was scarce (0.081 with 20 expert episodes), because restricting the policy to seen actions also excludes good actions seen too rarely to be valued well.
  • Exercise 1: from 456 near-expert transitions, naive Q’s policy was worth −0.306 while it estimated 0.561; batch-constrained Q 0.131; CQL 0.246; behaviour cloning 0.245.
🐍 Exercise 2 (research): how do behaviour cloning, naive offline Q-learning, batch-constrained Q and CQL compare as the dataset's quality and size change?
Compare behaviour cloning, naive offline Q-learning, batch-constrained Q and CQL on expert, medium and random datasets of two sizes, evaluating every learned policy exactly and recording how far naive Q overestimates.

Offline RL in practice

  • Sequence models: Decision Transformer treats offline RL as conditional sequence modelling of returns, states and actions.
  • Offline-to-online: pretrain offline, then fine-tune with interaction (Cal-QL, IQL fine-tuning).
  • Off-policy evaluation: importance sampling and doubly robust estimators score a policy from logs before any deployment.
  • Language models: preference datasets and DPO are a form of offline policy optimisation (topic F7-3).

References

  1. D. Ernst, P. Geurts and L. Wehenkel (2005). “Tree-based batch mode reinforcement learning.” JMLR.
  2. S. Fujimoto, D. Meger and D. Precup (2019). “Off-policy deep reinforcement learning without exploration.” ICML.
  3. A. Kumar, A. Zhou, G. Tucker and S. Levine (2020). “Conservative Q-learning for offline reinforcement learning.” NeurIPS.
  4. I. Kostrikov, A. Nair and S. Levine (2022). “Offline reinforcement learning with implicit Q-learning.” ICLR.
  5. S. Levine, A. Kumar, G. Tucker and J. Fu (2020). “Offline reinforcement learning: tutorial, review, and perspectives on open problems.” arXiv:2005.01643.