Generalization in RL
Agents that overfit their training levels, and how procedural generation exposed it.
An agent that masters 200 levels of a video game can stumble on the 201st. For years RL agents were tested on the very environments they trained in, which rewards memorization. Procedurally generated benchmarks exposed how badly agents overfit, and the theory of contextual MDPs and epistemic POMDPs explains why generalization in RL is harder than in supervised learning. This lesson covers the evidence, the formalism and the techniques, from data augmentation to decoupled value networks, and what each does and does not show.
What you’ll learn
- Explain why evaluation on the training environments measures memorization, and describe the experiments that exposed overfitting in deep RL.
- Formalize generalization with contextual MDPs, and define the generalization gap and zero-shot policy transfer.
- Explain the epistemic POMDP: why a policy that is optimal on every training level can be the wrong one to deploy, and why good test-time behavior may need memory or randomness.
- Compare techniques for better generalization (data augmentation, regularization, architecture, decoupled value networks, level curricula) by what their evidence shows.
Training on the test set
Supervised learning has always held out a test set. RL long did not: on the Arcade Learning Environment an agent trains and is evaluated on the same game from the same start, and in a deterministic game it can score well by memorizing what to do rather than learning how to play. Two studies made the problem impossible to ignore.
| Study | Setup | Finding |
|---|---|---|
| Zhang, Vinyals, Munos & Bengio (2018) | gridworld mazes; agents trained on 10 to 10,000 levels | training reward reached the optimum in every case while test performance varied widely; agents memorized many training levels even when the rewards were assigned at random; sticky actions and random starts did not necessarily prevent or reveal the overfitting |
| Cobbe, Klimov, Hesse, Kim & Schulman (2019) | CoinRun, a procedurally generated platform game; training sets of many sizes | agents overfit to surprisingly large training sets: substantially below 4,000 levels and noticeably even at 16,000; a deeper network (IMPALA-CNN) generalized better at every size; L2 regularization, dropout, data augmentation and batch normalization helped, and added stochasticity (ε-greedy actions, a larger entropy bonus) helped most |
Zhang et al. found that sticky actions and random starts do not necessarily prevent or reveal overfitting to the training mazes: overfitted agents still scored well on the training levels with them. Why not?
- Because the injected noise was too small to matter
- Because random starts make training harder, which hides overfitting
- Because they change how each training maze is played, not which mazes are seen: the agent still trains and is tested on the same mazes and can memorize their layouts
- Because they were applied only at test time
Show the answer
C. Because they change how each training maze is played, not which mazes are seen: the agent still trains and is tested on the same mazes and can memorize their layouts
Noise in actions or start states makes a memorized open-loop sequence fail, but a policy that recognizes each training maze and knows its solution is not disturbed by it. Only evaluation on mazes never seen in training separates memorization from generalization.
Contextual MDPs and the generalization gap
Procgen (Cobbe, Hesse, Hilton & Schulman, 2020) makes the gap measurable at scale: 16 games whose levels come from a generator, a recommended training set of 200 levels in easy mode, and tests on the full distribution of levels. As reported by Raileanu and Fergus (2021), PPO trained for 25 million steps on 200 easy levels per game averages these returns over 10 seeds:
| Game | Training levels | Unseen levels |
|---|---|---|
| BigFish | 9.2 | 3.7 |
| Dodgeball | 5.3 | 1.6 |
| Heist | 7.1 | 2.5 |
| Maze | 9.1 | 5.5 |
| StarPilot | 29.0 | 24.9 |
| CoinRun | 9.4 | 8.6 |
Think first Why can 200 levels be enough to master the training levels but not to play new ones?
A large network can tie its decisions to incidental features that happen to identify each training level, such as a background, a layout detail or a start configuration, because on those levels they predict the right action perfectly. On new levels the features change and the decisions fail. Song et al. (2020) call this observational overfitting. More levels, augmentations that randomize the irrelevant features, and architectures or objectives that discourage level-specific features all attack it.
Why it is hard: the epistemic POMDP
Ghosh, Rahme, Kumar, Zhang, Adams and Levine (2021) identified a deeper reason. Learning from a finite set of training contexts leaves the agent uncertain about which context it faces at test time, even if every training MDP is fully observed. The test problem is then partially observed, with the unknown context as the hidden variable: a POMDP created by the limits of the training data, which they call the epistemic POMDP. Its optimal policy is in general not Markov: it must adapt to what it observes. Among memoryless policies, the best may have to be stochastic.
Make it concrete: each step the agent opens one door; the right door pays 1 and ends the episode, a wrong one pays 0 and leaves it in the room; . A policy is worth , where is the step at which it opens the right door. On a new level each door is the exit with probability ½. The cue-reading policy is deterministic and memoryless there, and it is stuck forever when its fixed door is wrong:
A memoryless policy that picks a door uniformly at random succeeds at each step with probability ½, so is geometric:
A policy with one bit of memory tries the left door, then the right. An oracle that knows the context scores 1, and so does the cue reader on its training levels, where the cues still work:
Ghosh et al.'s algorithm, LEEP (Linked Ensembles for the Epistemic POMDP), trains an ensemble of four PPO policies on different bootstrap samples of the training levels and links them with a penalty that keeps each close to the others. On Procgen with 200 training levels it outperformed PPO on Maze, Heist and Dodgeball and matched it on BigFish.
Why can a policy that is optimal on every training level be a poor policy to deploy?
- Because it was trained with too small a learning rate
- Because at test time the agent does not know which context it is in; good behavior must hedge or gather information, which optimizing each training level separately never rewards
- Because optimal policies are deterministic, and determinism is always bad
- Because the test levels have different reward functions
Show the answer
B. Because at test time the agent does not know which context it is in; good behavior must hedge or gather information, which optimizing each training level separately never rewards
On training levels the agent can identify the context, often from incidental cues, and act as if it knew everything. On new levels the same cues are uninformative, and the right behavior is that of an agent uncertain about its environment: try, observe, adapt, or randomize.
What helps, and what the evidence shows
| Technique | Idea | Evidence, as reported |
|---|---|---|
| Data augmentation: RAD (Laskin et al., 2020) | train on randomly cropped or otherwise transformed image observations, so that level-specific pixels stop being reliable cues | on Procgen's BigFish, random crops raised PPO's test return by 55.8% (relative; 3 runs); with a suitable augmentation for each game, RAD trained on 100 levels beat PPO trained on 200 on BigFish and StarPilot, with little gain on Jumper |
| Augmentation for value learning: DrQ (Kostrikov, Yarats & Fergus, 2021) | augment observations and average the value targets over augmented views | developed for sample efficiency in control from pixels; it regularizes the critic |
| Choosing augmentations: UCB-DrAC (Raileanu et al., 2021) | select an augmentation per game automatically with a bandit, and regularize the policy and value to be invariant to it | designed and evaluated on Procgen |
| Regularization and noise (Cobbe et al., 2019; Igl et al., 2019) | L2, dropout, batch normalization, entropy and action noise; an information bottleneck with noise injected only where it does not corrupt the learning signal | on CoinRun each regularizer helped, and added stochasticity helped most (Cobbe et al., 2019) |
| Architecture (Cobbe et al., 2019) | a deeper residual network (IMPALA-CNN) instead of the original DQN network | better test performance at every training-set size on CoinRun |
| Decoupling policy and value: IDAAC (Raileanu & Fergus, 2021); PPG (Cobbe, Hilton, Klimov & Schulman, 2021) | the value function needs level-specific information, such as how far through the level the agent is, that the policy should ignore; separate networks, and for IDAAC an adversarial loss that removes such information from the policy's features | IDAAC's authors reported a new state of the art on Procgen; PPG trains policy and value in separate phases |
| Level curricula: PLR (Jiang, Grefenstette & Rocktäschel, 2021) | replay more often the training levels with the most learning potential, estimated from their TD errors | improved sample efficiency and test returns on Procgen in its authors' experiments |
Key takeaways
- Evaluating on the training environments measures memorization: agents reach optimal training return on mazes and platform games while failing on new levels, even with thousands of training levels.
- Contextual MDPs formalize generalization: train on a finite set of contexts, test on fresh draws from the same distribution, and report the gap between training and test return.
- Limited training contexts make the test problem an epistemic POMDP, whose best policies adapt or randomize; maximizing return on each training level is the wrong objective.
- Augmentation, regularization, better architectures, decoupled value networks and level curricula all narrow the gap on Procgen, mostly as reported by their own authors on a few benchmarks.
- Generalizing well on held-out levels does not guarantee the intended goal: capabilities can transfer while the goal learned is a proxy that training could not distinguish.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A paper reports that its agent reaches near-optimal return on its 500 training levels. What else is needed to claim that it generalizes?
- Nothing: near-optimal return implies generalization
- A longer training run on the same levels
- Its return on levels held out from training, drawn from the same generator, with the gap to the training return reported
- A larger network
Show the answer
C. Its return on levels held out from training, drawn from the same generator, with the gap to the training return reported
Training return measures fit, and deep networks can fit training levels by memorizing them. Only held-out contexts measure generalization; stronger claims also need shifted distributions, such as new generators, dynamics or goals.
In a contextual MDP, what does zero-shot policy transfer require?
- Good performance on contexts not seen during training, with no further learning on them
- Learning a new policy for each test context from scratch
- Observing the context variable directly
- Training on every possible context
Show the answer
A. Good performance on contexts not seen during training, with no further learning on them
The policy is fixed after training on and judged by . Settings that allow adaptation at test time, such as fine-tuning or meta-RL, are different problems.
In the epistemic POMDP, why can the best memoryless policy be stochastic?
- Because stochastic policies explore better during training
- Because the rewards are random
- Because the training levels are chosen at random
- Because identical observations can hide different contexts that need different actions; a deterministic memoryless policy repeats the same choice forever, while randomizing avoids getting stuck
Show the answer
D. Because identical observations can hide different contexts that need different actions; a deterministic memoryless policy repeats the same choice forever, while randomizing avoids getting stuck
Without memory, a policy must act the same way every time it sees the same observation. If that observation is consistent with several contexts, any fixed choice fails in some of them, possibly forever; a randomized choice eventually succeeds in all. With memory, the agent can instead try the options in turn.
Why does IDAAC give the policy and the value function separate networks?
- To halve the computation per update
- Because the value function benefits from level-specific information, such as how far through the level the agent is, that the policy should ignore; a shared representation pushes the policy to depend on it and overfit
- Because a shared network cannot output both actions and values
- Because the policy needs more parameters than the value function
Show the answer
B. Because the value function benefits from level-specific information, such as how far through the level the agent is, that the policy should ignore; a shared representation pushes the policy to depend on it and overfit
Predicting the return well requires knowing details of the particular level, while choosing actions often does not. When both heads share features, the value loss pulls level-identifying information into the features the policy uses, which helps on training levels and hurts on new ones. IDAAC separates the networks and adds an adversarial loss that strips such information from the policy's features.
Further reading
- C. Zhang, O. Vinyals, R. Munos & S. Bengio, "A study on overfitting in deep reinforcement learning," arXiv:1804.06893 (2018).
- K. Cobbe, O. Klimov, C. Hesse, T. Kim & J. Schulman, "Quantifying generalization in reinforcement learning," ICML, PMLR 97, 1282–1289 (2019).
- K. Cobbe, C. Hesse, J. Hilton & J. Schulman, "Leveraging procedural generation to benchmark reinforcement learning," ICML, PMLR 119, 2048–2056 (2020).
- R. Kirk, A. Zhang, E. Grefenstette & T. Rocktäschel, "A survey of zero-shot generalisation in deep reinforcement learning," Journal of Artificial Intelligence Research 76, 201–264 (2023).
- D. Ghosh, J. Rahme, A. Kumar, A. Zhang, R. P. Adams & S. Levine, "Why generalization in RL is difficult: Epistemic POMDPs and implicit partial observability," Advances in Neural Information Processing Systems 34, 25502–25515 (2021).