Memory in Deep RL
Frame stacks, recurrent and transformer agents: learning to remember what matters.
DQN played Atari from its last four frames, and that stack is already a memory architecture: a fixed window that assumes nothing older matters. When the information an agent needs lies hundreds or thousands of steps back, it needs a learned memory instead: a recurrent network, a transformer, a state-space model. This lesson looks at what each one buys, the replay tricks that make recurrent value learning work, what the memory benchmarks do and do not show, and a theory of what a learned memory should compute.
What you’ll learn
- Explain frame stacking and recurrent networks as ways to build an agent state, and what the DRQN experiments showed about them.
- Describe why replaying sequences is hard for recurrent agents, and R2D2's stored-state and burn-in remedies.
- Summarize the evidence on transformers, recurrent networks and other memories in RL benchmarks, separating memory from credit assignment and stating the limits of each study.
- State and prove a bound on the loss from planning on an approximate information state, and connect it to auxiliary prediction losses.
From frame stacks to recurrent agents
A deep RL agent in a partially observable world has to build its own agent state, , usually without a model from which to compute beliefs. The simplest construction is a window: DQN fed its network the last four preprocessed Atari frames (frame stacking; Mnih et al., 2015), enough to recover the velocities that a single frame hides. A window of observations is a Markov state only if the environment is -th order Markov in its observations; a key picked up 500 steps earlier lies outside any practical window.
A recurrent network replaces the window by a learned summary, , trained end to end by the agent's own losses with backpropagation through time; the policy acting on it is a recurrent policy. Nothing forces to be a sufficient statistic: it keeps whatever helps reduce the losses it is trained on.
| Memory | What it can keep | Cost per step | Known difficulties |
|---|---|---|---|
| Frame stack of observations | Exactly the last observations | Constant, with an input times larger | Nothing older than steps |
| Recurrent network (LSTM, GRU) | A learned summary of the whole past | Constant | Long backpropagation through time, often truncated; stale hidden states in replay |
| Transformer over a context window | Any of the last steps, retrieved by attention | Grows with | Unstable to train as an RL agent without changes; nothing beyond the window |
| Linear attention and state-space models | A fixed-size state updated linearly | Constant | Mixed results in RL so far |
Replaying sequences: R2D2
Value-based agents learn from a replay buffer of old transitions (experience replay). A recurrent network, though, needs a hidden state to process a stored observation, and the state it had when the observation was recorded was computed with older parameters. Kapturowski et al. (2019) studied the resulting representational drift and recurrent state staleness in distributed prioritized replay, and compared ways to start a replayed sequence:
- zero start: begin from a zero state, which the network never sees while acting;
- stored state: save the recurrent state with each sequence and start from it, which is stale because the parameters have changed since;
- burn-in: use an initial stretch of each sequence only to unroll the network and refresh its state, and train only on the rest.
Why does R2D2 burn in the first part of each replayed sequence instead of training on all of it?
- To save computation on long sequences
- Because the stored recurrent state was computed by older parameters; unrolling the current network over a prefix produces a fresher state from which to train on the rest
- To reduce the variance of the rewards in the sequence
- Because the first steps of every episode are uninformative
Show the answer
B. Because the stored recurrent state was computed by older parameters; unrolling the current network over a prefix produces a fresher state from which to train on the rest
Training from a stale or zero state teaches the network to work from states it will never produce while acting. A burn-in prefix lets the current parameters rebuild the hidden state, at the cost of not learning from those steps.
Transformers, benchmarks and what they measure
Transformers. Attention can retrieve any observation in the context window directly, without squeezing the past through a recurrent bottleneck. Plain transformers proved hard to optimize as RL agents. Parisotto et al. (2020) moved layer normalization to the inputs of each submodule and replaced residual connections by learned gates; their Gated Transformer-XL (GTrXL) surpassed LSTMs on memory-heavy environments and set the state of the art on the DMLab-30 suite, exceeding an external-memory agent, while matching or exceeding an LSTM on more reactive tasks.
Memory is not credit assignment. Ni, Ma, Eysenbach and Bacon (2023) separated two lengths: how far back an agent must remember an observation, and how far back a reward must be credited to an action. In simple configurable tasks, transformers solved memory problems that required observations from 1,500 steps earlier, but they did not improve long-term credit assignment. "Long-horizon" tasks usually mix the two, so success or failure on one says little about the other.
In task A an agent sees a cue at step 1 and must choose a door at step 1,000, rewarded at once. In task B it must choose a door at step 1 and is rewarded only at step 1,000. In Ni et al.'s terms, which is which?
- Both are memory problems of length 1,000
- A is a credit-assignment problem and B a memory problem
- Both are credit-assignment problems
- A needs long memory with short credit assignment; B needs long credit assignment with short memory
Show the answer
D. A needs long memory with short credit assignment; B needs long credit assignment with short memory
In A the reward follows the choice immediately, but the choice depends on a cue 1,000 steps old: memory. In B nothing has to be remembered at the choice, but the reward arrives 1,000 steps later and must be credited back to it: credit assignment. Transformers help with the first and, in Ni et al.'s experiments, not with the second.
| Memory model in POPGym | Mean max-mean episodic reward (scale −1 to 1) |
|---|---|
| GRU | 0.349 |
| LSTM | 0.255 |
| Elman RNN | 0.249 |
| Frame stacking | 0.190 |
| FART (linear attention) | 0.138 |
| Fast weight programmer (linear attention) | 0.112 |
| MLP, no memory | 0.067 |
| S4D (state-space model) | −0.180 |
Other studies point the same way. The Memory Maze (Pasukonis, Lillicrap & Hafner, 2022) asks agents to collect objects in randomized 3D mazes and remember where they are, with an online benchmark, an offline dataset and a probing evaluation of what a model's internal state retains; agents trained with truncated backpropagation through time succeed on small mazes and fall short of human players on large ones. Ni, Eysenbach and Salakhutdinov (2022) found that recurrent model-free agents with careful architecture and hyperparameter choices, among them separate recurrent encoders for the actor and the critic, beat specialized POMDP methods in sample efficiency and final performance on 18 of 21 environments drawn from six earlier papers. The common lesson is methodological: a memory benchmark must be checked against memoryless baselines, and a "long-horizon" result must say which length was long.
What should a memory compute? Approximate information states
With a known model the answer is exact: the belief is an information state, predicting rewards and its own next value as well as the whole history (The Markov Property & Agent State). A learned memory can only approximate one. Subramanian, Sinha, Seraj and Mahajan (2022) made "approximate" precise: an agent state that predicts the expected reward within and the distribution of its own next state within supports a dynamic program whose solution is near-optimal. The version below uses total variation, , for which with .
Treat histories as states. The optimal value over histories is the fixed point of the history Bellman operator , a -contraction. Let and compare, action by action, the backup of with the model's backup that defines :
Maximizing both sides over actions keeps the gap, and the model's maximum is . So has Bellman residual at most , and the residual bound of Error Bounds & Stopping Rules gives the first claim:
For the greedy policy , the same comparison with its action alone gives , hence , and the triangle inequality bounds the loss:
The bound turns "what should memory compute?" into two prediction targets: the next reward, and the distribution of the agent's own next state. Subramanian et al. learn agent states with losses of exactly this kind, and many deep RL agents add similar auxiliary losses, predicting rewards, observations or their own future latent states, to the RL objective (Representation Learning for RL). Two caveats keep the theory honest: the bound is worst-case and degrades like , and it says nothing about how to find a good agent state from finite data, which is where the practical difficulty lies.
Key takeaways
- Frame stacks are fixed windows, Markov only for -th order environments; recurrent and transformer agents learn their agent states end to end, with no guarantee that the states are sufficient.
- Replaying sequences needs fresh hidden states: R2D2's stored states plus burn-in reduce staleness, and the resulting recurrent agent set records on Atari-57 and matched the best on DMLab-30.
- The evidence is setting-dependent: GTrXL beat LSTMs on DMLab memory tasks; transformers remember far back but do not fix credit assignment; in POPGym under PPO, GRUs beat linear-attention and state-space models while a memoryless MLP won the navigation tasks.
- An agent state that predicts rewards within and its own next state within in total variation supports planning with loss at most : prediction losses are a principled target for memory.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
What did the DRQN experiments with flickering screens show?
- Recurrent agents beat frame stacking on every Atari game
- With the same history length, recurrence is a viable alternative to frame stacking with no systematic advantage in learning to play, but it adapts better when the observation quality changes at evaluation
- Frame stacking cannot learn flickering games at all
- An LSTM agent needs no training on partial observations
Show the answer
B. With the same history length, recurrence is a viable alternative to frame stacking with no systematic advantage in learning to play, but it adapts better when the observation quality changes at evaluation
Hausknecht and Stone were explicit that recurrence did not dominate frame stacking when learning; its advantage appeared when an agent trained under one observation quality was evaluated under another.
In recurrent experience replay, why is a hidden state stored with a sequence "stale" when the sequence is replayed?
- Because the observations in the buffer have been compressed
- Because the rewards have changed since the sequence was recorded
- Because replay samples sequences in a random order
- Because it was computed by the network's parameters at recording time, which have changed since
Show the answer
D. Because it was computed by the network's parameters at recording time, which have changed since
The state summarizes the past as an older network saw it. The current network, started from that state, sees an input distribution it would not produce itself: R2D2 measures the resulting gap in Q-values and reduces it with burn-in.
In POPGym, a memoryless MLP scored highest on almost all navigation tasks. What is the most defensible conclusion?
- MLPs have more effective memory than recurrent networks
- Memory never helps in partially observable tasks
- Those navigation tasks, as built, can be solved well without memory, so they are weak tests of memory architectures
- The recurrent models were implemented incorrectly
Show the answer
C. Those navigation tasks, as built, can be solved well without memory, so they are weak tests of memory architectures
A memory benchmark on which a memoryless baseline wins does not test memory. On other POPGym tasks memory models, especially GRUs, clearly beat the MLP, which is why the authors recommend diverse tasks and memoryless baselines.
In the approximate-information-state bound, the value error is at most . What do and measure?
- How well the agent state predicts the expected next reward () and the distribution of its own next state ()
- The exploration rate and the learning rate
- The error of the belief and of the observation model
- The approximation errors of the actor and the critic
Show the answer
A. How well the agent state predicts the expected next reward () and the distribution of its own next state ()
They are the two defining properties of an information state, relaxed: predict rewards, and predict your own next state, as well as the history would. When both are exact, and planning on the agent state is optimal.
Further reading
- S. Kapturowski, G. Ostrovski, J. Quan, R. Munos & W. Dabney, "Recurrent experience replay in distributed reinforcement learning," ICLR (2019).
- E. Parisotto et al., "Stabilizing transformers for reinforcement learning," ICML (2020).
- T. Ni, B. Eysenbach & R. Salakhutdinov, "Recurrent model-free RL can be a strong baseline for many POMDPs," ICML (2022).
- S. Morad, R. Kortvelesy, M. Bettini, S. Liwicki & A. Prorok, "POPGym: benchmarking partially observable reinforcement learning," ICLR (2023).
- J. Subramanian, A. Sinha, R. Seraj & A. Mahajan, "Approximate information state for approximate planning and reinforcement learning in partially observed systems," Journal of Machine Learning Research 23(12), 1–83 (2022).