Two cells of a corridor look the same. The best policy that sees only the current cell is random: it reaches the goal in 1.8 steps on average. A critic that sees only the cell settles on a worse policy at 2.0 steps. Memory of one step gets 1.2.
Drag p, the chance of stepping right, and watch the average step count. Then switch to V(o) critic to see where a critic on observations stops, and to History to give the agent one step of memory. Then we build it piece by piece: why random wins, why critics and models fail, and how a history fixes them.
You need the policy gradient from Policy Gradients and the TD advantage from Actor-Critic. We build the rest here.
A supplement to UC Berkeley CS 185/285, Deep Reinforcement Learning, Sergey Levine. Sources: Lecture 14 (part 5, partial observability), Lecture 2, Lecture 5 and Lecture 24. The words, toys and numbers are our own.
Illustrative toy, our numbers. Cells A, B, C; the goal is B; each step costs 1. A and C give the same observation. The agent starts in A 80% of the time and in C 20%. The curve is exact; the walking agent samples episodes.
Chapter 0
See what the agent loses when it acts on what it sees, not on what is true
A self-driving car has a true situation: the position and speed of every car nearby. Its camera sees a slice of it. A truck can hide a cyclist. The policy acts on the camera image, not on the situation.
The state st is everything that decides what happens next. An observation ot is what the agent receives at step t, a partial view of the state. A POMDP (partially observed Markov decision process) is an MDP in which the agent sees only ot. The course lists driving, Minecraft, human-robot interaction and visual dialogue as examples.
States have the Markov property: the next state depends on the current state and action, and the earlier past adds nothing.
p of s-t-plus-one given s-one through s-t and a-t equals p of s-t-plus-one given s-t and a-t.
Predict the next state twice: once from the whole past, once from the current state only. For a true state the two predictions are the same.
In our corridor, step right from cell A and you reach goal cell B with chance 1, whatever came before. Replace s with o and this fails. From the observation alone the chance is 0.67; after one bump into a wall it is 1 or 0.
Our toy is a corridor of three cells, A, B and C. B holds the goal. A and C both show the agent the same thing: "end of the corridor, not the goal". Each step costs 1, and the episode ends when the agent enters B. The agent starts in A 80% of the time.
The faded agents show where the agent may be. Press R or L to step. The bars give the chance that stepping right reaches the goal.
Illustrative toy. "From ot alone" is the share of end-cell visits that are in A, under the rule p = 0.67 of Chapter 1. That is 1.2 visits to A and 0.6 to C.
One bump settles it. Step right and still see "end": you were in C, since right from A reaches the goal. The observation alone never tells you this. The failure is any method that treats ot as a state. To measure it, predict from ot and from the history; if the predictions differ, ot is not Markov.
The course asks which methods handle partial observability. A method handles it if it finds the best policy in its class, here π(a | o).
Chapter 1
Find why a coin flip beats every fixed choice when two cells look alike
A memoryless policy π(a | o) picks its action from the current observation only. A and C give the same observation, so they get the same action rule. In our corridor that rule has one number: p, the chance of stepping right when the agent sees "end".
Try a fixed rule. With p = 1 the agent always steps right. From A that reaches the goal in one step. From C it walks into the wall forever. With p = 0 the failure moves to A. Every fixed rule loses some starts for good.
A random rule reaches the goal from both ends. If each try succeeds with chance p, the number of tries until the first success averages 1/p. From A a try succeeds when it steps right, so the agent needs 1/p steps on average. From C it needs 1/(1 − p).
T of p equals rho times one over p, plus one minus rho, times one over one minus p.
Work out the average steps from A, 1/p, and from C, 1/(1 − p). Weight each by how often the episode starts there, rho and 1 − rho. Add them. That is T.
With p = 0.9: from A, 1/0.9 = 1.11 steps; from C, 1/0.1 = 10 steps. T = 0.8 × 1.11 + 0.2 × 10 = 2.89 steps. The rare start in C costs the most.
Set the slope of T to zero, −ρ/p² + (1 − ρ)/(1 − p)² = 0, and solve for p.
p-star equals the square root of rho, divided by the square root of rho plus the square root of one minus rho.
Take the square root of each start chance. p-star is the share of the right-hand pull, √ρ, in the total.
With rho = 0.8: √0.8 = 0.894 and √0.2 = 0.447, so p* = 0.894 / 1.342 = 0.67. T(0.67) = 0.8 × 1.5 + 0.2 × 3 = 1.8 steps. The course's version has equal starts, rho = 0.5, which gives p* = 0.5.
Teal is the average steps from A, purple from C, warm the weighted average T. Drag p to 0 or 1, then change rho.
Illustrative toy. The bars are exact averages; bars above 10 steps are cut, and a rule that never arrives shows ∞.
The failure is a method that can only output fixed rules. The symptom is an agent that bumps a wall forever on some starts. Measure success per start cell, not only the average return. The fix is a random policy, or memory: Chapter 5.
The course names a second oddity: information-gathering actions. In a maze, the best move can be to look around first, because it pays only in what the agent learns.
Chapter 2
Check that the gradient never needed the Markov property, then train one on the corridor
The policy gradient takes the log of a trajectory's chance. That log splits into policy terms, log π(at | ot), and dynamics terms. The dynamics terms do not depend on the policy's parameters θ, whatever they depend on: hidden states, the whole past. So their gradient is zero. The course states it plainly: the Markov property is never used.
The gradient of J with respect to theta is about one over N times the sum over i of: the sum over t of the gradient of log pi-theta of a-i-t given o-i-t, times the sum over t of r-i-t, minus b.
For each episode, add the log-chance gradients of its actions. Multiply by how much better its return was than b. Average over episodes and step θ that way.
At p = 0.5 with b = −2. An episode from A steps right once: (0.5) × (−1 + 2) = 0.5. An episode from C steps right, then left: (0.5 − 0.5) × (−2 + 2) = 0. The average, 0.25, says raise p, which is right: the best p is 0.67.
The warm line is p after each update of 20 episodes. The green dashes mark the best memoryless p, 0.67. Press Train, then turn the baseline off and train again.
Illustrative toy. 300 updates of 20 sampled episodes, step size 0.15, starting at p = 0.18. Over 20 seeds the mean of the last 100 updates is 0.666 with the baseline and 0.659 without.
The course leaves one estimator as a pop quiz: reward-to-go, which weights each step's gradient only by the rewards from that step on. It is still valid. An action at step t cannot change earlier rewards, Markov or not. Our check, 200,000 episodes at p = 0.5: full return 0.586, reward-to-go 0.591, exact 0.600. The variance falls from 16.5 to 9.4.
The failure is noise. Even after 300 updates p wanders about ±0.02 around 0.67. Measure the spread of p over the last 100 updates. Fix it with a baseline or larger batches, but not with a learned critic on o: Chapter 3.
Chapter 3
Watch a value function that averages over hidden states push the policy to the wrong place
The actor-critic replaces the return with a one-step advantage from a learned value V̂. With states this is safe: every visit to a state has the same expected future, whatever came before. With observations it is not. V̂(o) averages over the hidden states behind o, and the past tells which one it is.
A-hat-t equals r-t plus gamma times V-hat of o-t-plus-one, minus V-hat of o-t.
Take the reward r-t, add the value of where you landed, and subtract the value of where you were. A positive A-hat makes the action more likely.
At the best memoryless p = 0.67, V̂(end) = −1.8. Reaching the goal from C gives  = −1 + 0 + 1.8 = 0.8. The true advantage there is +2.0, because from C the usual future is −3. The critic gives 0.8 of credit where 2.0 is due.
For each hidden cell and action, the solid bar is the true advantage and the pink bar is the critic's. Drag p, or press Train to run an actor-critic with V̂(o).
Illustrative toy. V̂(end) is the exact fixed point of TD learning on this policy: minus the average steps. Training runs 3,000 episodes from p = 0.5, critic step 0.05, actor step 0.02. Over 10 seeds it settles at p = 0.78; the exact resting point is 0.80.
Add up the critic's pushes and they cancel only where the agent visits A and C equally often: p = 0.8, at 2.0 steps. Policy gradient with real returns rests at 0.67, at 1.8 steps. More data does not close the gap.
Q-learning on Q(o, a) has the same flaw, plus one more. Its greedy policy is fixed, so it cannot be random. The course's verdict: value-based methods do not work without the Markov property.
| Method on observations | What it learns | In the corridor |
|---|---|---|
| Policy gradient, real returns | The best π(a | o) | p = 0.67, 1.8 steps |
| Actor-critic with V̂(o) | A biased advantage | p = 0.80, 2.0 steps |
| Q-learning on Q(o, a) | A fixed greedy rule | never arrives from one end |
The symptom is an actor-critic that settles worse than plain policy gradient. Measure it by comparing the critic's advantages with Monte Carlo returns on the same episodes. The fix is real returns with a constant baseline, or a critic on the history: Chapter 5.
Chapter 4
See a model that is right on average plan a strategy that fails half the time
Model-based RL learns the dynamics p̂(s′ | s, a) and plans with them. Swap states for observations and the model becomes p̂(o′ | o, a). The course shows why that is a bad idea with two doors.
You stand in a room with two doors. One is locked; you do not know which. The observations are Left (in front of the left door), Right, and Pass (through a door). A model trained on many episodes sees the left door open on half of the first tries.
p-hat of o-prime equals Pass, given o equals Left and a equals Open, equals one half.
Count the tries at the left door and the tries that got through. The model divides one by the other and uses that chance for every try.
The model says 5 tries succeed with chance 1 − 0.55 = 0.97. The truth: if the first try fails, the door is locked, and every later try has chance 0. Keep trying succeeds in 0.5 of episodes, not 0.97.
Each episode locks one door at random. The locked door turns red when the episode ends. Pick a model, then run one episode or a hundred.
Illustrative toy after the course's example. 8 steps per episode; trying a door or walking to the other door takes 1 step. The observation model's planner keeps trying, because walking costs a step and the other door has the same 0.5.
The model is right on average and wrong in every episode. After one failed try, the left door has chance 0, because it is locked or it is not. The planner cannot see that, so it plans "keep trying". A planner with history tries left once, then walks right: it always gets through, in 2 steps on average.
The symptom: the model predicts 99.6% success in 8 steps, and real runs get 50%. Measure the model's predicted success against real rollouts. The fix is a model whose state remembers: a history or a learned latent state, Chapter 5.
Chapter 5
Make the input Markov again with a history, a sequence model or a learned latent state
The course gives two fixes. Learn a Markov latent state with a model, or use the history itself as the state. Call the history ht everything the agent has seen and done so far.
h-t equals the list o-one, a-one, o-two, and so on up to a-t-minus-one, o-t.
Keep the whole list. The next history h-t-plus-one is h-t with a-t and o-t-plus-one added, so an older history tells nothing that h-t does not. That makes h-t Markov.
In the corridor, h = (end, R, end) means "in C" with chance 1, where o = end alone means "in A" with chance 0.67.
Value-based methods work again, because the history is a state.
Move Q of h-t and a-t toward r-t plus gamma times the max over a-prime of Q of h-t-plus-one and a-prime.
Pay the step's reward r-t, then add the best value from the history you land in. Move Q toward that target.
After h = (end, R, end) the agent is in C: Q(h, L) = −1 and Q(h, R) = −2. At the start, Q(end, R) = −1 + 0.2 × (−1) = −1.2, and Q(end, L) = −1.8. The greedy rule averages 1.2 steps, better than any memoryless rule.
How do you feed a history to a network? The course shows two designs. A frame stack feeds the last k observations to a feed-forward net; DQN on Atari stacked 4 frames. A sequence model (an RNN, LSTM or Transformer) reads the whole history. Is a short stack bad? "Sometimes."
The first cell shows a cue: teal means the reward is up, purple means down. The blue bracket is what the agent sees at the junction. Lengthen the corridor, then the window.
Illustrative toy, a memory corridor of our own. Success counts the right arm; without the cue the best choice is a coin, 50%.
A state-space model learns a latent state zt. It has dynamics p(zt+1 | zt, at), a decoder p(ot | zt) and an encoder q(zt | o1:t). Then Q(z, a) is safe. The course says this can work well, with a caveat. Prediction can be hard, and high reward may not need good prediction.
Meta-RL is also a POMDP. The hidden task z joins the state, (s, z), and the agent observes only s. A policy with memory is the simplest solver, which the course calls in-context learning.
The failure is a window shorter than the memory gap: success drops to 50%. Measure success against the gap length. The fix is a longer window or a sequence model.
Chapter 6
Keep the verdicts, the numbers, a checklist and a recurrent agent on one page
When the agent sees observations, not states, the best memoryless policy can be random. Policy gradients still find it, because they never used the Markov property. Critics on V̂(o), Q-learning on Q(o, a) and models p̂(o′ | o, a) all assume o is a state and fail. Use a history, through a sequence model, or learn a Markov latent state.
| Method | Verdict | Why | Chapter |
|---|---|---|---|
| Policy gradient, real returns | Works | Dynamics terms drop out; reward-to-go is still valid | 2 |
| Actor-critic with V̂(o) | Biased | V̂(o) averages over hidden states | 3 |
| Q-learning on Q(o, a) | Fails | Not Markov, and greedy rules cannot be random | 3 |
| Model p̂(o′ | o, a) | Fails | Treats each try as a fresh coin | 4 |
| Anything on a history ht | Works | ht is Markov | 5 |
| Anything on a learned zt | Works if z is Markov | Prediction can be hard | 5 |
| What | Number | Why it matters |
|---|---|---|
| Fixed rule, p = 1 | ∞ steps from C | Every fixed memoryless rule loses some starts |
| Best memoryless rule | p* = 0.67, T = 1.8 steps | Random is optimal (ρ = 0.8) |
| REINFORCE, last 100 updates | p = 0.666 on average | PG finds p* with no Markov property |
| Critic credit, C then left | 0.8 against a true 2.0 | V̂(o) is biased |
| Actor-critic resting point | p = 0.80, T = 2.0 steps | Worse than plain PG |
| Two doors, 8 steps | model 99.6%, real 50% | Observation models plan the wrong thing |
| One step of memory | T = 1.2 steps | History beats the best memoryless rule |
pythonimport torch, torch.nn as nn class RecurrentAC(nn.Module): # input per step: the observation and the previous action, one-hot def __init__(self, obs_dim, n_act, hid=64): super().__init__() self.gru = nn.GRU(obs_dim + n_act, hid, batch_first=True) self.pi = nn.Linear(hid, n_act) # policy head: logits of pi(a | h_t) self.v = nn.Linear(hid, 1) # critic head: V(h_t), not V(o_t) def forward(self, obs, prev_act, h0=None): x = torch.cat([obs, prev_act], dim=-1) # (B, T, obs_dim + n_act) z, hT = self.gru(x, h0) # z: (B, T, hid), one summary per step return self.pi(z), self.v(z).squeeze(-1), hT # advantage: A_t = r_t + gamma * V(h_{t+1}) - V(h_t); both values read the history # carry hT across steps of an episode; reset it at episode start
Now press Present or Teach and explain from memory: observations against states, why random wins, why policy gradients still work, the biased critic, the two doors and the history fix. Then go back to the hero and find the three numbers: 1.8, 2.0 and 1.2.
This lesson supplements UC Berkeley CS 185/285, Deep Reinforcement Learning, Sergey Levine, at rail.eecs.berkeley.edu/deeprlcourse. It draws on Lecture 14 (pages 40 to 51), Lecture 2 (pages 4 to 8), Lecture 5 (page 13) and Lecture 24 (page 53). The corridor with the 0.5 policy and the two doors are the course's examples. The 80/20 start, the trainers, the memory corridor and every number here are our own.