Introduction to RL
The formal foundation for everything that follows. Levine defines Markov chains, MDPs, and POMDPs, writes down the RL objective as an expectation under the trajectory distribution, and shows why expectations make even discontinuous rewards optimizable. Then the three-box anatomy shared by every RL algorithm, value functions and Q-functions, and a guided tour of the algorithm zoo — with the trade-offs that explain why so many algorithms exist at all.
From imitation to rewards
Imitation learning trained a policy by supervised learning on expert observation–action pairs. This lecture removes the expert. To do that we need a formalism that says what the policy should do without demonstration data — and that formalism starts with the vocabulary from lecture 2: the state , the observation , and the action . The state satisfies the Markov property — is independent of given — and this is “the main thing that distinguishes the state from the observation”: the observation is a stochastic function of the state that may or may not contain enough information to recover it. The course covers both the fully observed case, , and the partially observed case, ; Levine admits he will “get a little sloppy” and write where would also work, flagging the cases where the distinction matters.
In place of data, the objective is defined by a reward function — a scalar function of state and action that “tells us which states and actions are better”. Driving fast down the road: high reward. Collided with another car: low reward. The crucial subtlety is that RL is not about taking high-reward actions now; driving a little too fast is rewarding right up until the inevitable collision. Choosing actions now to receive high rewards later is the decision-making problem at the heart of RL — “by the time you’re about to hit someone, it’s too late.”
Markov chains, MDPs, and POMDPs
The formal objects are built up in three steps. A Markov chain — named for Andrei Markov, who pioneered the study of stochastic processes — has only states and transitions: a state space (discrete or continuous) and , the transition probability . It is called a transition operator because of a piece of linear algebra that will matter later: stack the state probabilities at time into a vector with entries , let , and the chain rule of probability becomes a matrix–vector product:
The objective: an expectation under the trajectory distribution
Running a policy in an MDP induces a distribution over trajectories . By the chain rule — using the Markov property to drop dependence on earlier states — it factorizes into objects we have already defined:
The RL objective, in its most basic finite-horizon form, is to find
Levine asks you to genuinely stop and internalize this line — the trajectory distribution, how shapes it, what the expected sum of rewards means — because everything in the next several lectures builds on it.
A useful rewriting: group into an augmented state. These augmented states form a Markov chain with transition operator — the MDP dynamics times the policy. By linearity of expectation the objective becomes a sum over per-time-step state–action marginals:
This looks like a useless rewriting; it is what makes the infinite horizon tractable.
One last observation closes the section: “reinforcement learning is really about optimizing expectations.”
The anatomy of an RL algorithm
Nearly every algorithm in this course shares one high-level skeleton, drawn as three boxes in a loop. The orange box generates samples: run the policy in the environment and collect trajectories — the “trial” in trial-and-error. The green box fits a model or estimates the return — anything that evaluates how the current policy is doing. The blue box improves the policy. Then repeat.
Two instantiations show the range. A policy gradient method makes the green box trivial — sum the rewards of each rollout, — and the blue box a gradient step , nudging good trajectories up and bad ones down. An “RL by backprop” method instead spends the green box training a whole neural network so that , then in the blue box backpropagates through and to train the policy.
Which boxes are expensive depends entirely on the setting. If samples come from a real robot, car, or power grid, the orange box runs at “1x real time, until we invent time travel”; the MuJoCo simulator from homework 1 runs up to 10,000x real time, making it trivially cheap. The green box spans a millisecond of summation to hours of model training; the blue box spans one gradient step to backprop-through-a-model. A Q-learning method spends nearly all its effort in the green box — its blue box is just an argmax.
Value functions and Q-functions
Expand the objective’s expectation with the chain rule and it becomes a nest: an expectation over , inside it one over , then plus an expectation over , over , and so on. Messy — unless you had a symbol for everything inside the second bracket. Call it , and the whole objective collapses to . And if you knew , improving the policy at the first step would be easy: test each action, put probability on the argmax.
In the anatomy, value functions and Q-functions live in the green box — they are objects that evaluate how good the current policy is — and get used in the blue box to improve it.
The algorithm zoo and its trade-offs
The course’s whirlwind tour sorts algorithms by what they estimate. Policy gradients directly differentiate and ascend. Value-based methods estimate the value function or Q-function of the optimal policy, often with no explicit policy at all — the policy is implicitly . Actor-critic methods are the hybrid: fit a value function or Q-function of the current policy, then use it to compute a better policy gradient. Model-based methods estimate the transition model, then use it in one of several ways: plan directly through it (Monte Carlo tree search for chess-like problems, trajectory optimization for continuous control), backpropagate gradients into the policy (simple in principle, needing “quite a few tricks” for numerical stability), learn a value function by dynamic programming, or generate synthetic data for a model-free learner. Named examples the course will cover: Q-learning and DQN, fitted value iteration, REINFORCE, natural policy gradient, TRPO/PPO, A3C, soft actor-critic, Dyna-style methods, guided policy search.
Why so many? Because the trade-offs cut differently per problem. Sample efficiency — how many environment samples until a good policy — hinges on one question above all: is the algorithm off-policy (can improve the policy using previously collected samples) or on-policy (must throw everything away each time the policy changes “even a little bit”)? The rough spectrum from fewer samples to more: model-based deep RL, off-policy Q-learning, actor-critic, on-policy policy gradients, evolutionary and gradient-free methods. But wall clock is not sample efficiency — with a cheap simulator, the right end of the spectrum is often computationally faster, which is exactly why you might choose a less efficient algorithm.
Stability and ease of use ask: does it converge, to what, and every time? Coming from supervised learning you might wonder why this is even a question — the answer is that RL is often not gradient descent. “Convergent algorithms are actually a rare luxury.” Q-learning is a fixed-point iteration whose convergence with neural networks is an open problem; value function fitting at best minimizes Bellman error — which is not the same as achieving reward — and at worst optimizes nothing and may diverge. Model-based RL converges as model fitting, but a better model is not guaranteed to mean a better policy. Policy gradient is the only one that performs true gradient ascent on the objective — and it is the least sample-efficient of the bunch.
Assumptions differ too: full observability (typical for value-fitting methods, mitigated with recurrence and memory), episodic resets (assumed by pure policy gradient methods, favored by the rest), and continuity or smoothness (common in model-based methods derived from optimal control). The lecture closes with worked examples spanning the zoo: Atari from pixels with convolutional Q-learning (Mnih et al. 2013), robotic visuomotor skills with model-based guided policy search, a humanoid learning to walk with a TRPO-family actor-critic, and the grasping robot from lecture 1 — also Q-learning, adapted to continuous actions.
Check yourself
Conceptual and computational exercises in the lecture’s spirit — work them on paper.
A two-state augmented Markov chain has transition operator
(column gives the probabilities of landing in each state from state ). Verify the chain is ergodic and aperiodic, find the stationary distribution , and compute the infinite-horizon average reward if assigns to state 1 and to state 2.
Show solution
Every entry of is positive, so every state reaches every other with nonzero probability in one step (ergodic) and self-transitions exist (aperiodic) — the stationary distribution exists. Solve with : the first row gives , so , i.e. . With the normalization, — an eigenvector of with eigenvalue 1, as the theorem requires. The average reward is . Note this is exactly : the finitely many early terms before the marginals converge to are washed out by the factor.
A robot attempts a grasp: reward if the grasp succeeds, if it fails — a discontinuous function of the gripper pose. Suppose the policy succeeds with probability for a scalar parameter . Show that the expected reward is smooth in , compute its derivative, and explain why this does not contradict the reward being non-differentiable.
Show solution
, which is infinitely differentiable, with everywhere — gradient ascent always pushes toward higher success probability. There is no contradiction because differentiation is with respect to the distribution’s parameters, not the outcome variable: the expectation integrates the discontinuous against a density (here a probability mass) that varies smoothly with . This is the lecture’s mountain-road example in miniature, and the reason RL can optimize sparse or binary rewards with smooth optimization methods.
In some state there are three actions with , , under a uniform policy . (a) Compute . (b) Which actions are “better than average”? (c) Write the improved policy from Idea 1 and argue, using the definition of , why it does at least as well as from .
Show solution
(a) . (b) Better than average means : only qualifies (); is exactly average and is worse. Idea 2 would increase . (c) , zero elsewhere. is the expected return from taking in and then following — so a single decision at switched to , with run afterwards, yields expected return , the return of following throughout. The first step of can only match or beat ‘s average, whatever is; making this argument at every state and time step (and iterating it) is the policy iteration idea developed later in the course.
For each algorithm, state what its green box and blue box do, whether it is on-policy or off-policy in its basic form, and one situation where you would prefer it: (a) a policy gradient method, (b) Q-learning, (c) model-based RL with planning.
Show solution
(a) Green box: sum rewards along sampled trajectories — trivially cheap. Blue box: gradient ascent step . On-policy — every step changes the policy, invalidating old samples. Prefer it when samples are cheap (fast simulator) and you want the only method that is true gradient ascent on the actual objective. (b) Green box: fit — nearly all the effort lives here. Blue box: just ; no explicit policy network needed. Off-policy — it can improve from previously collected samples. Prefer it with discrete actions and expensive samples. (c) Green box: fit by supervised learning — potentially a large training run per iteration. Blue box: plan through the model (e.g. Monte Carlo tree search, or trajectory optimization in continuous spaces) — possibly with no policy at all. Off-policy and typically the most sample-efficient end of the spectrum, but with the caveat that the model minimizes prediction error, and a better model is not guaranteed to yield a better policy. Prefer it when real-world samples are the binding cost and the dynamics are learnable (often assuming smoothness).