Learning from Demonstrations & Data
Imitation, inverse RL and offline RL: learning without trial and error.
A self-driving car cannot crash a thousand times to learn to steer, and a hospital cannot try random treatments on its patients. Yet records of skilled behavior and of past decisions are everywhere. This lesson shows four ways to learn from such records instead of from trial and error, and the trap that each one has to avoid.
What you’ll learn
- Explain why trial-and-error learning is often too costly, and name four ways to learn from existing demonstrations and data.
- Explain why copying an expert (behavior cloning) fails when small mistakes lead to unfamiliar situations, and how DAgger fixes it by asking the expert for corrections.
- Describe what inverse reinforcement learning infers, and why many different rewards explain the same behavior.
- Explain why offline RL must be pessimistic about actions its data never tried.
- Explain how off-policy evaluation scores a new policy on old data, and why long horizons make it hard.
Learning without trial and error
Reinforcement learning, as you have met it so far, learns by trying. An agent acts, sees what happens, and adjusts its policy. In a video game a failed try costs nothing. On a road, in an operating room, or with a robot arm next to a person, a bad try can be dangerous, slow or expensive, and an agent that has to explore may need millions of them.
Meanwhile, data about good behavior often exist already: hours of recorded human driving, logs of which items a recommender system showed and which ones people clicked, records of which treatment each patient received and how they fared. This topic asks how much an agent can learn from such data without collecting new trial-and-error experience of its own.
| Approach | Learns from | Produces | The trap |
|---|---|---|---|
| Imitation learning | Demonstrations by an expert | A policy that acts like the expert | Small mistakes lead to situations the expert never showed |
| Inverse RL | Demonstrations | The reward the expert seems to pursue | Many different rewards explain the same behavior |
| Offline RL | Logged experience with rewards, collected by any behavior policy | A policy better than the one that collected the data | Value estimates are fooled by actions the data never tried |
| Off-policy evaluation | Logged experience with rewards | An estimate of how well a new policy would do | The estimates get very noisy over long horizons |
A hospital has records of the treatments its doctors chose and of how the patients did. It wants a treatment policy that does better than the doctors' average, without experimenting on patients. Which family fits that goal?
- Behavior cloning
- Inverse RL
- Offline RL
- Off-policy evaluation
Show the answer
C. Offline RL
Offline RL uses logged states, actions and outcomes to learn a policy that improves on the behavior that produced the logs. Off-policy evaluation would then help to check a candidate policy before anyone uses it. (Real medical records add a difficulty you will meet at the end of the lesson: doctors may have acted on information that the records do not contain.)
Copying an expert: behavior cloning
The simplest idea treats imitation as supervised learning: record the expert's states and actions, then train a model to predict the expert's action from the state, exactly as you would train an image classifier. This is behavior cloning. In 1989 Dean Pomerleau's ALVINN steered a vehicle with a small neural network that mapped a coarse camera image to a steering direction; it was first trained on simulated road images, and within a few years by watching a person drive. In 2016 a team at NVIDIA trained a much larger network on about 72 hours of human driving and let it steer a car on public roads.
Imitation has a catch that ordinary supervised learning does not. An image classifier's mistakes do not change the next image it is shown. A driving policy's mistakes do: it steers a little wrong, and the next camera image shows the car slightly off-center, a situation the careful human driver almost never got into. The training data say little about how to get back, so the next mistake is more likely and the car drifts further. Errors compound. The mismatch between training on the expert's situations and being tested on your own is called covariate shift.
A cloned driving policy makes a mistake on 1% of the steps where it is still on the road, and one mistake puts it off the road for good. Over a 100-step drive, how many steps does it spend off the road, on average? (Treating every step as an independent test, as supervised learning would, suggests about 1.)
Show the answer
37
The chance that the car has left the road by step is , so the expected number of steps off the road is . A mistake early in the drive spoils every later step, so the damage grows much faster than the length of the drive: roughly with its square while mistakes are rare, until leaving the road becomes almost certain. The Fixed Point lessons prove that this quadratic growth is the worst case for behavior cloning.
Asking the expert for corrections: DAgger
If the trouble is that the training data never show the learner's own mistakes, collect data on those mistakes. DAgger (dataset aggregation; Ross, Gordon & Bagnell, 2011) repeats a simple loop:
- Let the current learned policy drive.
- Ask the expert what it would have done in every state that the learner visited.
- Add these labeled states to the training set, keeping all the earlier data, and retrain.
After a few rounds the training data cover the situations the learner really gets into, including how to recover from them.
What did the expert want? Inverse RL
A copied policy knows what the expert did, not why. Inverse reinforcement learning turns the problem around: instead of learning a policy from rewards, it infers from behavior the reward function that the expert seems to maximize. Ordinary RL or planning can then turn that reward into a policy, even in situations the expert never faced.
Ziebart and colleagues (2008) recorded over 100,000 miles of GPS traces from 25 taxi drivers in Pittsburgh and inferred how the drivers traded off features of the roads, such as speed limits, the number of lanes and turns. The learned reward predicted which routes drivers would take between new pairs of places, and could even guess a trip's destination from its first part.
The inverse problem has a famous weakness: it is ambiguous. A reward of zero everywhere makes every behavior optimal, so it "explains" any demonstration. Doubling all the rewards changes nothing either, and many genuinely different rewards produce exactly the same choices. Inverse RL methods therefore need an extra principle to pick one reward, such as preferring rewards under which the expert's behavior is clearly better than the alternatives.
An inverse RL method returns the reward "zero everywhere" for a set of driving demonstrations. What is wrong with this answer?
- It cannot be consistent with the demonstrations, because the expert clearly did something on purpose
- It shows that the expert was driving at random
- It is consistent with the demonstrations, but also with every other possible behavior, so it says nothing about the expert
- Nothing: zero is the true reward of most driving tasks
Show the answer
C. It is consistent with the demonstrations, but also with every other possible behavior, so it says nothing about the expert
Under the zero reward every policy is optimal, so the expert's behavior is "explained", but so would any other behavior be. A useful reward must make the expert's choices better than the alternatives, which is the kind of extra criterion that inverse RL algorithms add.
A close relative of inverse RL powers today's chatbots. Instead of demonstrating, people compare two responses and say which one is better; a reward model is fitted to many such comparisons and then optimized by RL. That is reinforcement learning from human feedback, and it inherits the same question: which reward did the comparisons really pin down?
Learning from logs: offline RL
Behavior cloning copies and inverse RL explains; offline RL tries to beat the data. Given logs of states, actions, rewards and next states collected by some behavior policy, it learns a better policy without acting at all during training. That is the setting of the hospital records, or of a recommender system that must improve using only its past logs.
The natural first attempt runs an off-policy method such as Q-learning on the logs: estimate how good each action is, then choose the action that looks best. But the estimates are trustworthy only where the data are. For an action the logs never tried, the estimate is pure extrapolation by the function approximator, and choosing the best-looking action amounts to searching for the estimate's largest error. The agent is drawn toward out-of-distribution actions that merely look good. An online agent would try such an action, see a poor result and correct itself; an offline agent never finds out.
The cure is pessimism: treat what the data cannot vouch for as doubtful. Conservative methods push down the estimated value of unseen actions (CQL), keep the policy close to actions that appear in the data, or never evaluate unseen actions at all (IQL).
Grading a new policy on old data
Before a new policy goes live, such as a new treatment rule or a new recommender, you want to know how well it will do. Trying it is exactly the risk you are avoiding, so off-policy evaluation estimates its value from logs that the old behavior policy collected.
The central trick is reweighting, or importance sampling. Suppose the old policy recommended a certain article 20% of the time and the new policy would recommend it 60% of the time. Each logged outcome of that recommendation should then count three times (), and outcomes the new policy would rarely produce count for less. Averaging the reweighted outcomes gives an unbiased estimate of the new policy's value.
When a task has many steps the weights multiply, one factor per decision. A trajectory in which each of ten choices was three times likelier under the new policy gets weight , while most other trajectories get weights near zero. The average is then dominated by a handful of trajectories and swings wildly from one dataset to the next: the curse of horizon.
Think first Why not simply deploy the new policy for a while and measure how it does?
Eventually you often do, in a careful A/B test. But a test exposes real users or patients to an untested policy, takes time, and can compare only a few candidates. Off-policy evaluation screens the candidates first. Its high-confidence versions go further: they return a lower bound on the new policy's value that holds with high probability, so that a policy is deployed only if even this pessimistic estimate beats the current one.
One enemy, four strategies
All four approaches fight the same enemy, distribution shift: something is learned from data gathered under one behavior and then used under another. Behavior cloning is trained on the expert's states and tested on its own. Inverse RL explains behavior where the expert went and is used to predict it elsewhere. Offline RL must judge actions the behavior policy rarely took. Off-policy evaluation scores a policy that would visit states the logs rarely contain.
The remedies are variations on three moves: collect data where the learner actually goes (DAgger), match whole distributions of behavior rather than single actions, and be pessimistic about whatever the data cannot vouch for.
In the Bootstrapping lessons you'll build each family properly. Imitation Learning develops behavior cloning and DAgger, and the tricks that make cloning work in practice. Inverse RL & Reward Learning characterizes the rewards that make a policy optimal, and learns rewards from demonstrations and from preferences. Offline RL shows why ordinary off-policy algorithms fail on fixed data and how conservative methods fix them. Off-Policy Evaluation derives importance-sampling, model-based and doubly robust estimators and their trade-offs. The Fixed Point lessons then prove that cloning's errors can grow with the square of the horizon, recast imitation as matching whole distributions of behavior (GAIL), derive maximum-entropy inverse RL, ask which rewards behavior can identify at all, develop the theory of pessimism, and break the curse of horizon.
Key takeaways
- When trial and error is costly or dangerous, agents can learn from demonstrations and logs instead: imitation learning, inverse RL, offline RL and off-policy evaluation.
- Behavior cloning is supervised learning on the expert's choices. Its mistakes lead to unfamiliar states, so errors compound over the horizon; DAgger fixes this by having the expert label the states the learner reaches.
- Inverse RL infers the reward behind behavior, which can transfer to new situations, but many different rewards explain the same behavior.
- Offline RL must be pessimistic about actions the data never tried, because value estimates extrapolate and the best-looking unseen action is usually an overestimate.
- Off-policy evaluation reweights logged outcomes by how likely the new policy is to produce them; the weights multiply over steps, so long horizons make the estimates very noisy.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A behavior-cloned driving policy predicts the expert's steering correctly on 99% of held-out frames from the expert's own drives, yet it leaves the road on most real drives. What is the main reason?
- 99% is too low: with 99.9% accuracy on the same kind of frames the problem would disappear
- Its small mistakes take it into situations that the expert's data rarely contain, where it makes bigger mistakes
- Neural networks cannot represent steering behavior
- The expert's demonstrations must have contained errors
Show the answer
B. Its small mistakes take it into situations that the expert's data rarely contain, where it makes bigger mistakes
Held-out frames from the expert's drives test the policy on the expert's situations, but on its own drives the policy creates new situations by its own mistakes. That covariate shift lets errors compound over the drive, which is why accuracy on expert frames is a poor predictor of driving performance.
What does DAgger ask of the expert that behavior cloning does not?
- More demonstrations of the expert's own driving
- A reward signal for every step
- The action the expert would take in the states that the learner itself reached
- To take over the controls permanently whenever the learner errs
Show the answer
C. The action the expert would take in the states that the learner itself reached
DAgger runs the learner, has the expert label the states the learner visited, and retrains on all the data gathered so far. The training data then match the situations the learner actually gets into.
Why can perfect demonstrations not pin down the expert's reward exactly?
- Because many rewards, including scaled copies of the true one and the zero reward, make the same behavior optimal
- Because demonstrations always contain noise
- Because inverse RL algorithms can only find approximate solutions
- Because experts do not have rewards
Show the answer
A. Because many rewards, including scaled copies of the true one and the zero reward, make the same behavior optimal
Behavior reveals only which choices the reward makes best, and many rewards agree on those. The ambiguity would remain with infinitely many perfect demonstrations; inverse RL resolves it with extra assumptions, such as a margin or a probabilistic model of the expert.
An offline RL agent fits value estimates to logged data and then picks the action with the highest estimate. That action never appears in the logs. Why is this choice risky?
- Actions missing from the logs always have low rewards
- The estimate there is an extrapolation, and picking the highest of many uncertain estimates tends to pick an overestimate
- The behavior policy forbade that action, so it cannot be taken
- Value estimates are always too pessimistic outside the data
Show the answer
B. The estimate there is an extrapolation, and picking the highest of many uncertain estimates tends to pick an overestimate
Without data, the estimate for that action is a guess by the function approximator, and maximizing over many guesses favors the ones that are too high. Offline RL methods are therefore pessimistic about such actions: they penalize them, stay near the data, or never evaluate them.
Why does importance sampling become much noisier when episodes are long?
- Longer episodes have larger rewards
- It becomes biased as the horizon grows
- The behavior policy changes over the course of the episode
- A trajectory's weight is a product of one ratio per step, so a few trajectories get enormous weights and most get tiny ones
Show the answer
D. A trajectory's weight is a product of one ratio per step, so a few trajectories get enormous weights and most get tiny ones
Each step multiplies the weight by the ratio of the new policy's probability to the old one's. Over many steps these products spread out enormously, so a handful of trajectories dominate the average and the estimate swings from one dataset to the next. The estimate stays unbiased, but its variance can grow exponentially with the horizon.
Go deeper
- Imitation Learning · Behavior cloning, compounding errors and DAgger.
- Inverse RL & Reward Learning · Inferring the reward that explains observed behavior.
- Offline RL · Learning from a fixed dataset: distribution shift and pessimism.
- Off-Policy Evaluation · Estimating a new policy’s value from old logs.
Further reading
- T. Osa, J. Pajarinen, G. Neumann, J. A. Bagnell, P. Abbeel & J. Peters, "An algorithmic perspective on imitation learning," Foundations and Trends in Robotics 7(1–2) (2018).
- S. Levine, A. Kumar, G. Tucker & J. Fu, "Offline reinforcement learning: Tutorial, review, and perspectives on open problems," arXiv:2005.01643 (2020).