Modern Robotics and Physical AILesson 5.6 · Offline RL
Glossary Ideas map RU
5.6 Part 5 · Reinforcement learning

Offline RL: how to learn from recordings without making new attempts

In lesson 5.3, SAC learned from old experience in its buffer, but it kept adding new drives to it all the time. On a real robot, every attempt costs time, and a failed one may also break something; on the other hand, there are usually plenty of recordings from operators and past policies, of varying quality. Reinforcement learning from such recordings alone, without a single new drive, is called offline RL. Imitation learning here copies whatever occurs most often in the logs, while the Q-learning from lesson 5.3 falls apart on the same logs: the Q-function overestimates the value of actions that aren't in the data, and the policy picks exactly those. In 2018, the authors of BCQ demonstrated this on tasks in the MuJoCo simulator; two years later, CQL penalized high estimates outside the data, and in 2021, IQL learned to never ask the Q-function about such actions at all. QT-Opt took the same path: 580,000 recorded grasp attempts on 7 robots gave 87% success on new objects, and 28,000 fine-tuning attempts raised it to 96%.

≈ 55 minutes3 labs and a quizmissions with a goal
THE LOGS OF THREE OPERATORSLook at the drives of a careful, a hasty and a random operator and find out which of them collects more reward on average
FOUR POLICIES, ONE SET OF RECORDINGSTrain four of Ada's policies in your browser on the same logs and compare what each one promised with what actually happened
A PROBE FOR THE Q-FUNCTIONPut Ada at any point on the route and see which action the Q-function considers best and how many such actions were in the logs
WHICH LOGS ARE NEEDEDTrain on the recordings of a single operator and find out when reinforcement learning beats imitation learning and when it doesn't

Start the lesson ↓

1 Logs

Learning from recordings when new attempts are expensive

Reinforcement learning in lessons 5.1–5.3 tries actions by itself: in lesson 5.2, PPO needed 400 drives to learn a single steering policy, and SAC in lesson 5.3 is more economical but still collects new experience at every step. On a real robot, every such attempt is paid for with the operator's time, wear and the risk of a collision. The authors of the offline RL survey (Levine et al., 2020) say as much: collecting data in the real world can be “expensive,” as in robotics, and “dangerous,” as in autonomous driving.

Ada is a home humanoid on a wheeled base; the course labs are built around her. More about her in lesson 0.1.

Recordings, on the other hand, are usually plentiful. Operators drove the robot, past versions of the policy worked for weeks, and all of it sits in the logs. Offline RL (offline reinforcement learning; older papers call it batch RL) trains a policy only on such a pre-collected set of “state, action, reward, next state” transitions. There are no new drives: the policy is run only once training is over. The people and past policies that collected the logs are together called the behavior policy πβ\pi_\beta: the logs record how they acted.

In this lesson's labs, Ada again drives from the hallway to the kitchen, as in lessons 1.1 and 5.2, and we watch the apartment from above. Now she chooses not only the steering but also the speed: slow, 40 cm/s, or fast, 80 cm/s. You can't turn sharply at speed: the steering is limited so that at 40 cm/s Ada turns at up to 1.4 rad/s, and at 80 cm/s only half as fast. In total she has 18 actions: 9 steering positions at each of the two speeds. Every 0.2 s she picks one of them, and the motors execute the steering with a small error.

Three operators recorded the logs. The careful one drives slowly and always down the middle of the passage. The hasty one follows the same line, but always fast. The random one presses whatever comes to hand in half of the decisions and the rest of the time steers Ada toward the kitchen, at a speed that keeps changing. Ada gets a reward for every decision: +1 for each meter of progress toward the kitchen, −0.5 for each second on the way, +2 for reaching the kitchen and −5 for a collision. So the best thing is to get there fast without hitting anything.

Where the experience comes from in the three approaches. Blue marks recorded drives, orange marks what is being trained. The top row shows on-policy PPO from lesson 5.2: Ada drives with the current policy, an update is made on each rollout, and after it the rollout is thrown away. The middle row shows off-policy SAC from lesson 5.3: every step goes into the buffer, training draws batches from the whole buffer, and Ada keeps driving and filling it. The bottom row shows offline RL: the operators' logs are collected in advance, there are no new drives, training draws batches from the same recordings over and over, and Ada sets off only after training. Click the diagram to pause it.

How is this different from lesson 1.1, where Ada also learned from operator recordings? There, the policy copied actions and didn't care whether a drive ended well. Here, every step has a reward, and we want a policy better than the one that collected the logs. The survey by Levine et al. names this as the main goal of offline RL: “to obtain a policy that is better than the best behavior in the dataset.” We could simply run the lab from lesson 5.3 on these logs, only without new drives. What would happen then?

2 Optimism

Naive Q-learning on logs: optimism outside the data

The simplest thing is to take Q-learning from lesson 5.3 and fill its buffer with the logs in advance. That is exactly how the survey by Levine et al. describes the simplest offline RL method: Q-learning without new drives, with its buffer pre-populated with the recordings. The Q-function Q(s,a)Q(s, a) from lesson 5.1 estimates the value of an action: the discounted return Ada will collect if she takes action aa in state ss and then acts in the best way. It learns the same way as in lesson 5.3: on every recorded transition, the estimate Q(s,a)Q(s, a) is pulled toward the target

y=r+γmax⁡a′Q(s′,a′).y = r + \gamma \max_{a'} Q(s', a').

Here rr is the reward for the recorded step, s′s' is the state after it, γ\gamma is the discount factor, and the maximum is taken over all 18 of Ada's actions in s′s'. And there's the problem. The Q-function itself learns only on the actions that are in the logs: each recorded transition adjusts the estimate of the action the operator took. The network extrapolates the estimates of all the other actions however it happens to. If the logs contain almost no full-lock turns at high speed, nothing checks the estimate of such an action: it may turn out lower than the true value or higher. The maximum picks the largest of the estimates, which means, most often, the one that is mistakenly inflated.

The authors of BCQ (Fujimoto, Meger and Precup, 2018) called this extrapolation error, “a phenomenon in which unseen state-action pairs are erroneously estimated to have unrealistic values.” Combined with the maximum, it produces the persistent overestimation from lesson 5.3, and the inflated target immediately trains the Q-function for the previous step. Online, such an error would correct itself: the policy would try the overestimated action and see what happens. Offline, that doesn't happen. In the words of the survey by Levine et al., “if the policy can produce out-of-distribution actions for which the learned Q-function erroneously produces excessively large values, it will learn to do so,” and “these errors accumulate over each iteration of training.”

This is the same distribution shift as in lesson 1.1, only now it's the actions that shift. There, the policy drifted into states the operator had never visited. Here, the Q-function learns on the operators' actions but evaluates the actions it picks itself.

In the BCQ experiments, a DDPG agent learned from a million transitions collected by a well-trained DDPG expert. The data was expert data, and still, as the authors write, “the agent quickly learns to take non-expert actions, under the guise of optimistic extrapolation,” and “the value estimates rapidly diverge.” Even when two agents learned from the same buffer, but one of them collected the data itself and the other only learned from it, the second one fell behind in every single trial. So good logs aren't enough. We need to stop trusting the Q-function where there is no data. How?

3 Three ideas

Three ways not to trust the Q-function outside the data: BCQ, CQL and IQL

The first idea is not to let the policy choose rare actions. BCQ (batch-constrained Q-learning, Fujimoto et al., 2018) first trains a model that, given a state, proposes actions similar to the recorded ones; in essence, this is imitation learning. Then, out of 10 such proposals, each slightly perturbed, it picks the one with the highest Q. The maximum in the training target is also taken only over them. The authors note that this gives a dial between two extremes: with a single proposal and no perturbations, the policy “resembles behavioral cloning,” and with infinitely many proposals and arbitrary perturbations, the method approaches ordinary Q-learning. In the labs, Ada has only 18 actions, so we use a discrete version of the same idea: the allowed actions are those that the imitation learning model considers at least 30% as likely as the most frequent one.

The second idea is to fix the Q-function itself. The Q-learning from section 2 minimizes the Bellman error, the difference between the estimate Q(s,a)Q(s, a) and the target yy, which comes from the Bellman equation of lesson 5.1. CQL (conservative Q-learning, Kumar et al., 2020) adds a penalty to this error that, on every recorded transition, pushes down all high estimates in that state and pushes up the estimate of the action recorded there:

LCQL=12(Q(s,a)−y)2+α(log⁡∑a′eQ(s,a′)−Q(s,a)).L_{\text{CQL}} = \tfrac12\bigl(Q(s,a) - y\bigr)^2 + \alpha\Bigl(\log \sum_{a'} e^{Q(s,a')} - Q(s,a)\Bigr).

The first term is the Bellman error on the recorded pair (s,a)(s, a). The second term contains log⁡∑a′eQ(s,a′)\log \sum_{a'} e^{Q(s,a')}, a smoothed maximum of the estimates over all actions: it is almost equal to the largest of them, and its gradient pulls exactly that one down the hardest. The penalty weight is set by α\alpha. The Bellman error holds the logged actions in place, while the penalty pushes down the estimates of actions that aren't in the logs. The authors proved that with a large enough α\alpha, the expected estimate of a policy is no higher than its true value, and they write that it takes “only 20 lines of code” on top of a standard SAC implementation. In their experiments, CQL often learned policies that “attain 2-5 times higher final return” than prior methods, “especially when learning from complex and multi-modal data distributions.”

The third idea is to never ask the Q-function about actions outside the logs at all. IQL (implicit Q-learning, Kostrikov, Nair and Levine, 2021) separately learns the state value V(s)V(s), and only from the actions in the logs. Instead of the maximum, it takes an expectile: a statistic that equals the mean at τ=0.5\tau = 0.5 and approaches the maximum as τ\tau gets closer to 1, but only over the values that occurred in the data. An asymmetric squared error is enough for this:

LV=∣τ−1(u<0)∣ u2,u=Q(s,a)−V(s),LQ=(r+γV(s′)−Q(s,a))2.\begin{gathered} L_V = \bigl|\tau - \mathbf 1(u < 0)\bigr|\, u^2, \quad u = Q(s,a) - V(s), \\ L_Q = \bigl(r + \gamma V(s') - Q(s,a)\bigr)^2. \end{gathered}

Here 1(u<0)\mathbf 1(u < 0) is one if the Q of the recorded action is below the current estimate V(s)V(s), and zero otherwise. At τ=0.7\tau = 0.7, underestimating costs 0.7 of the squared error and overestimating costs 0.3, so V(s)V(s) is pulled toward the best of the recorded actions. The target for the Q-function is r+γV(s′)r + \gamma V(s'), and it contains no maximum over actions that never occurred. The authors write that at τ=0.5\tau = 0.5 the method turns into SARSA, which evaluates the operators' behavior as it is, and at τ\tau close to 1 into Q-learning over the actions in the data.

IQL gets its policy through weighted imitation learning: each recorded action is copied with the weight eβ(Q(s,a)−V(s))e^{\beta (Q(s,a) - V(s))}. The difference Q−VQ - V is the advantage from lesson 5.1: how much better the action is than usual in this state. The larger it is, the more strongly the policy copies the action; β\beta sets how sharply. Actions worse than usual are hardly copied at all. In a similar way, π*0.6 in lesson 3.8 labeled its attempts as good or bad by their advantage and learned from them with ordinary supervised learning.

One of Ada's states. The horizontal axis is the steering, from full left to full right, and the vertical axis is the Q estimate of each action. The blue dots at the bottom are the actions from the logs in this state: there are many in the middle and almost none at the edges. The orange line is the Q-function: in the middle the data holds it in place, and at the edges it is filled in however it happens to be. First, the maximum (the ring) finds an inflated estimate at an edge, the target r+γmax⁡Qr + \gamma \max Q carries it over to neighboring states, and the peak grows from iteration to iteration. Then the diagram shows three fixes: BCQ grays out the rare actions and looks for the maximum only among the frequent ones, CQL pushes down the estimates where there is no data, and IQL draws the line V(s)V(s), an expectile over the estimates of the recorded actions only. The numbers on the diagram are illustrative. Click the diagram to pause it.

All three methods stick to the logs in different ways: BCQ restricts the choice, CQL changes the estimates, and IQL never leaves the data at all. Let's test them on the same recordings of the three operators.

🔬 Lab 1

Four policies on the same logs

The logs hold 100 drives: 20 by the careful operator, 40 by the hasty one and 40 by the random one, 4,647 decisions in total. Four policies train on these recordings in your browser: imitation learning (BC, behavior cloning from lesson 1.1), naive Q-learning, CQL and IQL. The conditions are the same for all of them: networks of the same size and 4,000 training steps on batches of 64 recorded transitions with the same Adam step size. The test is the same too: each policy makes 20 drives from the same starts and with the same steering noise. The BC and IQL policies choose the most probable of the 18 actions, and the methods with a Q-function choose the action with the highest estimate.

Logscareful operator
Logshasty operator

A top view of the apartment, 6 × 4 m: the hallway on the left, the living room in the middle, the kitchen on the right, furniture hatched. The dashed circle in the kitchen marks the end of the route, and Ada stands at the start in the hallway. The lines show the drives: where a line is thin, Ada drives slowly, and where it is thick, fast. A green check mark at the end of a drive means Ada made it, a red cross means she crashed, and a gray circle means the 20 seconds ran out.

Left
Right
On the chart

The average return per drive is the sum of rewards without a discount. Gray bars show the operators on their own recordings, colored bars show the policies on 20 test drives. The label next to a bar gives how many drives reached the kitchen and the average time of those that did.

Under the hood: the world, the operators, the methods and a check on ten seeds

The world. The apartment and the observation come from lessons 1.1 and 5.2: Ada sees 9 rangefinder beams in a ±120° sector, up to 2.5 m long, the direction to the kitchen, given by the cosine and sine of the angle, and the distance to it. That's 12 numbers in total; the network has 13 inputs, and the last one is always zero. A decision is held for 0.2 s (four simulation steps of 0.05 s), and a drive lasts up to 20 s, that is, up to 100 decisions. The steering has 9 positions from full left to full right, and the speed is either 40 or 80 cm/s. The steering limit is set by a lateral acceleration of 56 cm/s²: at 40 cm/s that's 1.4 rad/s, and at 80 cm/s, 0.7 rad/s. Gaussian noise of 0.3 rad/s is added to the steering. Ada starts in the hallway with a spread of 8 cm and 0.12 rad.

Reward per decision. r=Δd−0.5 Δt+2⋅[kitchen]−5⋅[collision]r = \Delta d - 0.5\,\Delta t + 2\cdot[\text{kitchen}] - 5\cdot[\text{collision}], where Δd\Delta d is the progress along the center line of the passage in meters and Δt=0.2\Delta t = 0.2 s. The discount is γ=0.97\gamma = 0.97 per decision.

The operators. The careful and the hasty operators steer Ada toward a route point 42 cm ahead, like the teacher from lesson 1.1, each drive with its own small offset from the middle; the careful one always drives at 40 cm/s, the hasty one at 80 cm/s. The random operator presses any of the 18 actions with probability 0.5 and otherwise steers Ada toward the kitchen, changing the speed with probability 0.15 at every decision.

The methods. All networks have sizes 13 → 24 → 24 → 18 and leaky ReLU activations, and the IQL value network has a single output. The networks train for 4,000 steps on batches of 64 transitions, Adam uses a step size of 0.003, and the target networks track the trained ones with τ=0.02\tau = 0.02. BC trains with cross-entropy on the recorded actions. Naive Q-learning uses the target r+γmax⁡a′Qtarg(s′,a′)r + \gamma \max_{a'} Q_{\text{targ}}(s', a'). CQL takes the same target and adds the penalty with α=1\alpha = 1. IQL takes the expectile τ=0.7\tau = 0.7 and the weights eβAe^{\beta A} with β=10\beta = 10, capped at 100, as in the paper. BCQ trains a BC model and a Q-function and picks both the action and the maximum in the target only among actions whose BC probability is at least 0.3 of the most probable one. In the paper, IQL takes the smaller of two Q-functions and trains the policy after the Q-function; we have one Q-function, and all networks train at the same time.

The same in every browser. The exponential, logarithm, sine and arctangent in the engine are built from additions, multiplications and divisions, as in lessons 5.2 and 5.3: the built-in functions of different browsers differ in the last digit, and training blows such a difference up. That's why everyone gets the same numbers.

Ten seeds. The lab uses a single seed: it sets both the logs and the initial weights. Across seeds 1–10, naive Q-learning never makes it on any of them, and its promise at the start ranges from 39 to 1,051. IQL makes it in 16–20 drives out of 20, BC in 14–19, CQL in 8–20, BCQ in 12–17. In average return, IQL beats the best operator on 10 seeds out of 10 and BC on 10. CQL with α=1\alpha = 1 beats BC in return on only 3 seeds out of 10: it sticks to the logs but plays it too safe.

The careful and the hasty operators collect almost the same on average, but reach that result in different ways:

Operators: results from their own logs
Metric Careful Hasty
Average undiscounted return +0.51 +0.45
Successful drive time 15 s 9 s
Crashes None: always makes it 18 drives out of 40

All of the hasty operator's crashes happen in the kitchen, where the turn can't be made at that speed. All 40 of their drives reach the kitchen, so the logs for that section contain many fast actions. In the hallway and the living room, the logged drives were more often slow.

BC copies the most frequent logged action at every point. It therefore drives slowly where it could go fast, and fast where the hasty operator crashed. IQL chooses the speed differently: it takes the fast option in about 60% of decisions in the hallway and living room, but in 9% in the kitchen. It uses the hasty operator's fast sections and slows down where the crashes happened.

Policies on 20 evaluation drives
Policy Successful drives Outcome
BC 14 of 20 All six crashes are in the kitchen; average undiscounted return −0.08
Naive Q-learning 0 of 20 Never makes it
CQL 17 of 20 Makes it, though slowly
IQL 18 of 20 In 10.9 s; average undiscounted return +1.98, more than any operator

Successful drives do not yet mean that the Q-function estimates return accurately. Let's compare its promise at the start with the actual discounted return from the same drives:

Predicted and actual return: averages over starts
Policy Q promise at the start Actual discounted return
Naive Q-learning 93.0 −3.92
CQL 11.2 −0.45

Both Q-functions promise more than the policies achieve. CQL overestimates less, and its policy now reaches the kitchen on most evaluation drives.

The numbers at the start are averaged over the drives. To understand where the promise of 93 comes from, we need to look inside the Q-function at a single point on the route: which actions it considers best and how many such actions were in the logs.

🔬 Lab 2

A probe for the Q-function: what it promises at a single point

The apartment plan shows the careful operator's route. Ada stands at one point on it; you can drag her along the route with the mouse or a finger. Next to the plan are all 18 actions at this point: nine steering positions at low speed and nine at high speed. The bars show how much better the Q-function considers each action than the one the operators most often took nearby. The diamonds show what would actually happen if Ada took this action and then drove like the careful operator. Below the axis, you can see how many times each action occurs in the logs near Ada. The Q-functions come from lab 1, and for other CQL penalty weights, training is run again.

The plan shows the apartment from above. The gray dashed line is the careful operator's route, and Ada stands on it. The 25 cm circle around her marks where we look for recorded decisions with a similar heading, and the blue dots are those decisions. On the chart, the actions are split into two groups by speed, “slow” and “fast,” and in each group the steering runs from full left to full right. The bars show the Q-function's estimate minus the estimate of the most frequent logged action nearby (it is labeled “most often”). The blue diamonds show the same difference in reality. The blue bars below the axis show how many times the action was recorded nearby. The ring marks the action Ada will choose. Gray hatching covers the actions that BCQ forbids.

Q-function
Under the hood: what the probe computes and how the results change with the penalty weight

Decisions nearby. All recorded decisions where Ada was within 25 cm of the probe point and her heading differed by at most 0.35 rad count as nearby. The most frequent of them is the reference: the bar of action aa equals Q(s,a)−Q(s,afreq)Q(s,a) - Q(s,a_{\text{freq}}).

In reality. From the probe point, Ada takes action aa and then drives like the careful operator, four times with different steering noise; the diamond shows the average discounted return minus the same for afreqa_{\text{freq}}. This is a lower bound: after the first step, Ada drives slowly rather than in the best way. On the other hand, it doesn't depend on any Q-function.

The penalty weight. For each α\alpha, CQL is trained from scratch on the same logs, with the same networks and the same number of steps as in lab 1, and is tested on the same 20 drives. The results are saved, so no value needs training twice.

Ten seeds. In front of the doorway, the naive Q-function picks an action that occurs in fewer than 5% of the nearby decisions on 10 seeds out of 10. With CQL at α = 0.03, Ada never makes it on any seed; at α = 0.1 she makes it in 0 to 14 drives out of 20, at α = 0.3 in 8 to 18, and at α = 1 in 8 to 20. At α = 10, CQL picks the most frequent logged action at the probe point on 6 seeds out of 10.

In front of the doorway to the living room, the naive Q-function considers a full-left turn at low speed the best and rates it 12.5 above what the operators most often did here. Only 5 such decisions out of 375 are recorded nearby, and the random operator made them. In reality, this action is no better at all: the difference is less than a hundredth. The naive Ada picks it again and again and crashes while still in the hallway. The CQL penalty pushes such estimates down: at α = 0.03 the peak remains and Ada never makes it, at α = 0.1 she makes it in 5 drives out of 20, at α = 0.3 already in 15, and at α = 1 and above, CQL picks the most frequent logged action at this point, like BC. BCQ arrives at the same result by another route: it simply doesn't consider rare actions.

So offline RL relies on what is in the logs. So how far it can surpass the operators depends on the logs too. What should the set of recordings be like?

🔬 Lab 3

Which logs offline RL needs

Here BC and IQL train not on the mixture but on the recordings of a single operator: 60 drives of the careful one, 60 of the hasty one or 100 of the random one. The networks, the number of training steps and the test stay the same as in lab 1. The mixture from lab 1 is also on the chart for comparison. Pick a set, and its drives appear on the plan; then place your bet and train both policies.

Logs

A top view of the apartment. The lines show the drives from the selected set of logs: where a line is thin, Ada drives slowly, and where it is thick, fast. A check mark means Ada made it, a cross means she crashed, and a circle means time ran out.

The bars show the average return per drive on each set: gray is the operator on their own recordings (for the mixture, the best of the three), blue is BC, orange is IQL; both policies make 20 test drives. The label under the bars gives how many drives made it and their average time.

Under the hood: the sets of logs and what the papers say

The sets. The drives are collected by the same operators and with the same randomness as in lab 1, only in different numbers. The random operator crashes often, so the drives are short. That's why this set has more of them, 100, so that the number of recorded decisions is comparable to the other sets.

In the papers. In Table 1 of the CQL paper, imitation learning and CQL are almost equal on expert data: 109.0 and 109.9 on hopper-expert, on a scale where 0 is a random policy and 100 is the expert. On a mix of random and expert data, hopper-random-expert, BC gets 10.1 and CQL 110.5. The BCQ authors saw the same: on clean expert data, “behavioral cloning unsurprisingly performs the best,” while on noisy demonstrations BCQ clearly beat both the demonstrator and BC. The survey by Levine et al. explains why: “Broader data distributions (i.e., ones where πβ(a|s) has higher entropy) are generally easier to learn from, since there are fewer out-of-distribution actions.”

Ten seeds. On the careful operator's logs, IQL drives like the operator on 10 seeds out of 10: the time difference is under 0.5 s, and IQL makes no fast decisions. On the hasty operator's logs, it always drives fast, on 10 seeds out of 10. On the random operator's logs, IQL makes it 5 to 20 times out of 20 and BC 8 to 20 times; IQL collects more than BC on 8 seeds out of 10. A good policy often comes out of a single random operator's logs, but not always.

On the recordings of the careful operator alone, IQL drives just like that operator and like BC: it makes it in 20 drives out of 20 in 15.1 seconds. There are no fast actions in the logs, and the survey by Levine et al. warns plainly: “we cannot expect offline RL to discover actions that are better than any action illustrated in the data.” On the hasty operator's recordings, both policies always drive fast and crash, almost always in the kitchen: there are no slow actions in the logs, so there's nothing to learn braking from. But from the logs of the random operator, who reached the kitchen in 17 drives out of 100, IQL got the best policy of the lesson: it makes it in 20 drives out of 20 in 8.2 seconds and collects +4.02. BC on the same logs also always makes it, because the random operator's most frequent action is to head for the kitchen, but it takes 12.3 seconds.

Offline RL is useful when the logs contain different actions and show what they lead to: both successes and mistakes. The logs of a single careful operator give a good policy to BC as well, but they don't let you surpass that operator.

4 Robots

Offline RL on real robots: QT-Opt and AWAC

The survey by Levine et al. explains why robots need recordings in terms of scale: the ImageNet challenge “stipulates a training set of 1.5 million images for open-world object recognition,” and a robot that acts in a similar open world will hardly get by with less experience. Collecting that experience anew for every new policy is expensive. The first large-scale example was QT-Opt (Kalashnikov et al., Google, 2018): a robot grasped unfamiliar objects from a bin using a camera image.

QT-Opt has no separate policy network. A Q-function with 1.2 million parameters takes an image and an action, and the action is chosen by search. The cross-entropy method, the same one the planner used in lesson 0.3, takes 64 random candidates, keeps the 6 best by Q and repeats this twice. The action is chosen this way both in training, when the target is computed, and on the robot. This is the same maximum over actions as in section 2, only approximate. The reward is simple: 1 for a lifted object, 0 for a failure and −0.05 for every step. The data was collected over four months on 7 robots: it added up to 580,000 grasp attempts, about 800 hours of robot time. Each new experiment reused the data of the previous ones.

MethodTaskDataResult
QT-Opt, logs only (2018)grasping unfamiliar objects, 7 robots580,000 recorded attempts87% success
QT-Opt, logs and fine-tuning (2018)the same580,000 recorded and 28,000 new attempts96% success
Prior method (Levine et al.)the same900,000 attempts78% success
AWAC (2020)a five-fingered hand rotates a pen in its fingers, simulation25 human demonstrations and 500 BC trajectoriessolved in 120,000 steps, about 20 min of interaction
IQL and CQL (2021)an ant in a maze, antmaze-medium-playlogs, then 1M fine-tuning stepsIQL: 72 → 95; CQL: 23 → 0

The numbers come from Table 1 of the QT-Opt paper, Section 5 of the AWAC paper and Table 2 of the IQL paper (there, scores are on the D4RL scale, where 0 is a random policy and 100 is the expert).

On recorded attempts alone, QT-Opt grasped unfamiliar objects 87% of the time. Another 28,000 attempts, made by the robot itself, raised success to 96%, while the prior method got 78% even with 900,000 attempts. The authors explain the gain this way: the new data lets the policy “quickly correct erroneous and over-optimistic extrapolations,” which is exactly what nothing can correct in the offline setting. The trained policy regrasped an object on its own if the grasp was turning out unstable, and when an object was pushed out of the gripper, it corrected itself and grasped another one.

Fine-tuning after the logs turned out to be a problem of its own. The authors of AWAC (advantage-weighted actor-critic, Nair et al., 2020) showed that SAC, even pre-trained with imitation learning, loses performance at the start of fine-tuning, while offline methods with hard constraints “struggle to improve with fine-tuning”: they are “often too conservative.” AWAC trains its policy the same way IQL does in this lesson, by imitation learning weighted by the advantage, but it takes the advantage from a Q-function that keeps learning on new steps. In simulation, a five-fingered hand learned to rotate a pen in its fingers to the target orientation in 120,000 steps, which the authors call “the equivalent of just 20 minutes of online interaction.” On real robots, AWAC opened a drawer with an arm with 7 degrees of freedom, picked up an object with a multi-fingered hand and rotated a valve with a three-fingered claw; in the authors' words, “AWAC trained offline achieves reasonable performance … and then steadily improves from online interaction.” IQL was also fine-tuned after the logs in its paper: in the maze where the ant has to stitch together pieces of unsuccessful trajectories, its score rose from 72 to 95, while CQL's fell from 23 to 0 after fine-tuning.

5 Today

Logs as a start, not a replacement for experience

The papers in this lesson converge on one recipe: first squeeze everything out of the accumulated recordings, then fine-tune on a small number of new attempts. The logs give the policy a good start without a single risky step, and the new attempts fix what can't be checked from the logs. That's exactly what QT-Opt did: on the recordings it got 87% success, and after 28,000 fine-tuning attempts, 96%.

Robots are accumulating more and more recordings. In lesson 4.6, the data flywheel fed the attempts of deployed robots, failures included, back into training, and π*0.6 from lesson 3.8 learned from the robots' own attempts across all iterations, labeling actions by their advantage. This is exactly the material for offline RL: many attempts of varying quality with known outcomes. Lab 3 suggests that what's most valuable in such logs is diversity and mistakes, not exemplary runs alone.

6 Check yourself

Eight common misconceptions

7 Takeaways

Lesson takeaways

  1. Offline RL trains a policy only on recorded transitions with rewards, without new attempts. It aims for a policy better than the one that collected the logs. In lab 1, IQL on the logs of three operators collected +1.98 per drive, and the best operator +0.51.
  2. Imitation learning copies the most frequent actions in the logs, good and bad alike. Ada's BC policy drove fast where the hasty operator drove more often, and all six of its crashes happened in the kitchen.
  3. Q-learning from lesson 5.3 falls apart without new drives: the maximum in the target picks actions that are almost absent from the logs, and their inflated estimates grow. The naive Q-function promised 93.0 at the start against −3.92 in reality, and in front of the doorway it considered best a turn recorded nearby only 5 times out of 375.
  4. There are three ways to stick to the data: BCQ chooses only among frequent actions, CQL pushes down the estimates of actions outside the logs, and IQL takes an expectile over the recorded actions only and copies them with advantage weights.
  5. Offline RL won't invent actions that aren't in the data. On the logs of the careful operator alone, IQL drove like that operator; on the logs of the hasty operator alone, it crashed like that operator. But the recordings of the random operator, who made it in 17 drives out of 100, produced the best policy of the lesson: 20 of 20 in 8.2 seconds.
  6. On real robots, the logs provide a start, and a few new attempts fix what the logs can't check: QT-Opt got 87% success on 580,000 recorded attempts and 96% after 28,000 new ones.

Open questions

How do you choose the penalty weight and when to stop training without running the robot?

In lab 2, we tested every α\alpha with drives, and that's exactly what offline RL can't do. The CQL authors chose the budget for the penalty from the predicted Q-values, without runs in the environment, and write that too small a budget inflated α\alpha and strongly underestimated Q, while too large a budget didn't stop the estimates from diverging. They also call the number of training steps an open problem: offline Q-learning lacks “a proper validation error metric.” And the survey by Levine et al. calls evaluating a policy without drives “an active area of research.”

When should you switch from logs to new attempts?

QT-Opt was fine-tuned on 28,000 attempts and raised success from 87 to 96%, while AWAC showed that after the logs many methods first lose performance or barely learn. After fine-tuning in the maze, IQL improved and CQL didn't. How many new attempts are needed, and how to keep what the logs gave at the start of fine-tuning, depends on the task and the method. The next lesson takes up this question.

Paper guide

I. Kostrikov, A. Nair, S. Levine, “Offline Reinforcement Learning with Implicit Q-Learning”, 2021.

Other papers in this lesson: the offline RL survey (Levine, Kumar, Tucker, Fu, 2020), BCQ (Fujimoto, Meger, Precup, 2018) on extrapolation error, CQL (Kumar et al., 2020) on the conservative Q-function, QT-Opt (Kalashnikov et al., 2018) on image-based grasping from 580,000 attempts, AWAC (Nair et al., 2020) on fine-tuning after the logs.

Resources

Offline RL squeezes more out of recordings than their average level: from 580,000 recorded grasps, QT-Opt learned to pick up an object in 87% of attempts. But what isn't in the recordings can only be checked with new attempts: another 28,000 grasps of its own raised it to 96%.

Lesson 5.7 · RL on real robots: how to learn right on the robot while a person stands by and corrects it

Was the lesson clear?
Found a mistake or inaccuracy? Report it on GitHub
Found this lesson useful? Say thanks — that is how the author knows the course is being read.