Introduction to Reinforcement LearningPart 4 · Beyond value functions

Lecture 12Learning from people

When people provide the signal: imitation learning, behaviour cloning and DAgger, inverse RL and maximum entropy, learning from comparisons with the Bradley–Terry model, reinforcement learning from human feedback, and its limits.

By the end you should be able to explain why behaviour cloning drifts, derive the max-entropy IRL gradient, fit a Bradley–Terry model, and say what the KL term in RLHF protects against.

Key ideas, one per episode

  • Ep. 47Showing can be easier than explaining.
  • Ep. 48Match the expert, and commit to nothing else.
  • Ep. 49Comparisons are cheap; Bradley–Terry turns them into scores.
  • Ep. 50Learn a reward from people, then optimise it on a leash.
  • Ep. 51Learning from humans is hard, which makes it interesting.

Based on: Lecture 8: Imitation Learning and RLHF, Stanford CS234 (Spring 2024), by Emma Brunskill, with material from Tatsu Hashimoto’s CS224N lecture and Katerina Fragkiadaki’s lecture on maximum-entropy inverse RL. The slides follow that order of topics, in our own words, figures and examples; they are not official material of the original course.

The title slide of Lecture 12: Learning from people

56 slides, written to be read on your own, with worked examples, “try it” boxes, small simulations and check-yourself questions (answers at the end). PDF, 1.7 MB.

Watch

5 short episodes of ACML Explains for this lecture. Pick one from the list, and open the transcript to read along: each timestamp plays the episode from there. The narration is generated with a voice clone of Teddy, used with his permission.

Episode 47 · 5:18

Learning from demonstrations

Write a reward for driving well. Go on, I'll wait.

Showing can be easier than explaining.

TranscriptEpisode 47

Write a reward for driving

Module twelve, the last one: learning from humans. Let's start with a challenge. Write me a reward for driving well. Go on, I'll wait. Stay in the lane. Don't hit anything. Don't brake too hard. Be polite at roundabouts. Every rule you add, an agent will find a loophole in. But any decent driver can just show you. One drive, and you see more than ten pages of rules.

Imitation learning

That's imitation learning. It's useful when demonstrating the behaviour is easier than writing the reward, or writing the policy directly. Driving. Tying a surgical knot. Folding a shirt. Ask an expert for a formula, and you get a shrug. Ask them to do it, and you get a demonstration. So the input changes. We still have states and actions. Sometimes we know the dynamics, sometimes not. But there is no reward at all. Instead, we get demonstrations: sequences of states and actions, from an expert's policy, pi star.

Three questions

From there, three questions. First, behaviour cloning: can we learn the expert's policy directly, with supervised learning? Second, inverse reinforcement learning: can we recover the reward the expert was optimising? Third, apprenticeship learning: can we use that reward to find a good policy of our own?

Behaviour cloning

Behaviour cloning first, because it's the simplest. Every state the expert visited is an input. The action they took is the label. Train a classifier. Done. This is the moment in the course where the supervised learning people finally relax. And it doesn't need the dynamics model. You never ask what happens next. You only ask what the expert did here. Mathematically: take every state and action pair from the demonstrations, and minimise the usual classification loss. Nothing in it knows about time. It works surprisingly often. And it fails in a very specific, very sneaky way.

Compounding errors

The expert is good. So the expert almost never makes mistakes. Which means the data never shows how to recover from one. Imagine learning to drive only from videos of perfect drivers. You've never seen anyone fix a drift. So the first time you drift, you're on your own. The copy makes one small error, and lands in a state the expert never visited. There, it has no idea what to do, so it makes a bigger error. And another. In supervised learning, the test data comes from the same place as the training data. Here, the copy chooses its own test data, by driving. People call this compounding errors, or distribution shift. A tiny error rate per step can add up to a big mess over a long drive.

Our lane toy

We tried it. A road nine cells wide. Gusts of wind push the car left or right, fifteen percent of the time each. The expert always steers back toward the centre. In twenty demonstrations, it never left the middle three cells. Our copy follows a table built from those demonstrations. But five percent of the time it picks a random action. And where it has no data at all, it acts at random. Two thousand drives of two hundred steps each. The expert went off the road zero times. The copy, in thirty seven percent of the drives. And look where the copy spent its last moments: in cells the expert never visited, with nothing to copy.

DAgger

The fix has a nice name: DAgger. Dataset aggregation, Ross, Gordon and Bagnell, twenty eleven. Let the copy drive. Every state it visits, ask the expert: what would you have done here? Add those answers to the data, and train again. Now the data includes the copy's own mistakes, with the right correction attached. In our toy, after one round, the copy went off the road in two drives out of a thousand. Ross and colleagues also showed why it matters. For behaviour cloning, the cost of small mistakes can grow with the square of the horizon. With DAgger, roughly linearly. The price: the expert has to sit in the passenger seat, for every round. Experts are expensive, and they get bored.

Inverse RL

Inverse reinforcement learning takes the other road. Don't copy the actions. Figure out what the expert wanted. Why? A reward transfers. New car, new road, new weather: the actions change, but the goal of not dying stays the same. A classic idea is feature matching, from Abbeel and Ng, two thousand four. Describe each state with features: speed, distance to the car ahead, lane position. If your policy visits those features as often as the expert does, you earn about the same reward. For any reward built from those features, with bounded weights. But there's a catch. Infinitely many rewards share the same optimal policy. Give zero reward everywhere, and every policy is optimal. Which reward should we pick? And the third question, apprenticeship learning, is the easy part. Once you have a reward, any method from this course can find a policy for

Recap

it. So: imitation learning, for when showing is easier than explaining. Behaviour cloning is simple, but it drifts. DAgger fixes the drift, at the price of a patient expert. Inverse RL looks for the reward instead. Next time: of all the rewards that could explain the expert, pick the least surprised one. Maximum entropy.

Interactive lab

Bradley–Terry lab

Compare answers two at a time, the way people label data for RLHF, and watch a Bradley–Terry model turn your choices into a score for each answer. Add simulated comparisons and see how many it needs.

The Bradley–Terry model says P(i beats j) = σ(rᵢ − rⱼ). Fitting the scores r to comparisons is the reward-model step of RLHF. The six answers are made up for this lab.

Simple math

From “this one is better” to a score

People find it easier to compare two answers than to grade one. A Bradley–Terry model turns many comparisons into one score per answer.

The model
P(i ≻ j) = σ(rᵢ − rⱼ) = 1 / (1 + e^−(rᵢ − rⱼ))

Only differences of scores matter, so the scores are shown centred at 0.

The fit
maximise Σ log P(winner ≻ loser) − a small penalty on r²

A tie counts as half a win for each side. The small penalty keeps scores finite when one answer wins every time.

In RLHF
The fitted r is the reward model.

A policy is then trained to produce answers the reward model scores highly, usually with a KL term that keeps it close to where it started.

Your comparisons
0
Simulated
0
Top answer
–
Agreement
–

Which answer is more helpful?

“My robot keeps bumping into the same wall. Why?”

Fitted scores

One bar per answer, centred at 0.

Agreement: the share of comparisons (ties left out) in which the fitted model gives the chosen answer more than a 50% chance.

P(row beats column)

What the fitted model predicts for every pair.

Play

A game that goes with this lecture, and what to look for while you play.

Echo Heist

Teach a little robot. Steal back a city.

In this lecture: Echo Heist’s learners include an imitation one: a policy learned from examples rather than from reward. Compare it with the learners that train on reward.

Choose what your partner Echo senses, which actions it can take and what success means, then let a real learner train in your browser: tabular, planning, policy-gradient, deep-RL or imitation. When the door opens, the policy is frozen and Echo runs with what it learned.