Chapter 12: Reinforcement Learning
Explore the paradigm of learning through interaction with an environment. Master the Markov Decision Process framework, value-based methods like Q-learning, policy gradient algorithms, and understand how deep learning enables RL to tackle complex problems from game playing to robotics.
- Topics
- 3
- Videos
- 3
- Min read
- 27
Chapter Overview
Reinforcement Learning (RL) represents a fundamentally different paradigm from supervised and unsupervised learning. Instead of learning from labeled examples or finding patterns in data, RL agents learn by interacting with an environment and receiving feedback in the form of rewards.
The key challenge in RL is the credit assignment problem: when an agent receives a reward, it must determine which of its past actions contributed to that reward. This is complicated by delayed rewards—a good action might not produce a reward until many steps later.
RL has achieved remarkable successes: defeating world champions at Go and Chess, learning to play Atari games from raw pixels, controlling robots, optimizing data center cooling, and training the RLHF component of modern language models like ChatGPT.
The fundamental framework for RL is the Markov Decision Process (MDP), which formalizes the agent-environment interaction loop. Two main approaches exist for solving MDPs: value-based methods (learning which states/actions are valuable) and policy-based methods (directly learning which actions to take).
This chapter covers:
- MDP Framework: The mathematical formalization of sequential decision-making with states, actions, rewards, and transitions
- Q-Learning: A value-based algorithm that learns optimal action-values through temporal difference updates
- Policy Gradients: Methods that directly parameterize and optimize the policy using gradient ascent
- Deep RL: Combining neural networks with RL for high-dimensional state spaces and complex policies
Chapter Roadmap
Click any topic to jump in
Markov Decision Process
The mathematical framework for sequential decision-making — states, actions, rewards, transitions, and discount factors.
Value-based and policy-based approaches
Q-Learning
Model-free value learning through temporal difference updates — epsilon-greedy exploration and the Bellman equation.
Policy Gradients
Directly optimizing the policy with REINFORCE — advantage estimation, actor-critic, and continuous action spaces.
Topics
3 topics in this chapter
The previous chapter, NLP Fundamentals, ended with models pretrained on fixed datasets: every training example arrived with its input and its target already written down. Many problems do not look like that. A robot vacuum, a game-playing program, or a system that sets prices must choose an action, see what happens, and choose again, and its choices change the data it sees next. Nobody labels the correct move. The only feedback is a reward, often delayed: a chess win arrives 40 moves after the decisive mistake. How do we even state such a problem precisely enough to solve it?
This chapter, Reinforcement Learning, studies agents that learn from interaction and reward. This topic builds the formal language every later method uses, the Markov Decision Process. It starts with the MDP components: states, actions, transition probabilities, rewards, and the discount factor. A policy is the agent's rule for choosing actions. A value function scores a state by the reward that policy expects to collect from it. The topic closes with the discount factor, which trades immediate reward against future reward.
Definition
A Markov Decision Process is a tuple : a set of states , a set of actions , transition probabilities , a reward function , and a discount factor . The Markov property requires that the next state and reward depend only on the current state and action, not on earlier history.
In this topic
- 1MDP Components
- 2Policy
- 3Value Function
- 4Discount Factor γ
MDP Components
An MDP has five parts. The state set describes every situation the agent can be in; the action set lists its choices. The transition model gives the probability of landing in state after taking action in state , so it captures randomness such as a wheel slipping. The reward is a number the environment returns after each step, and it is the only signal of what we want. The discount weights future rewards. The Markov property says the current state carries everything relevant, so a state that omits needed history, such as velocity, breaks the model.
An MDP is the tuple where satisfies the Markov property: . The state space can be finite () or continuous (). For finite MDPs, the transition model is a set of stochastic matrices , each row summing to 1. The total number of parameters to fully specify the MDP is .
Robot vacuum cleaner: identify the MDP components. States? Actions? What determines transitions?
Policy
A policy is the agent's behavior: for each state, a probability distribution over actions. A deterministic policy puts all its probability on one action per state. The goal of reinforcement learning is an optimal policy, written , that maximizes expected discounted return from every state at once. For finite MDPs a deterministic optimal policy always exists, but stochastic policies are still useful during learning, because they explore, and in partially observed or adversarial settings. Note that 'optimal' means maximizing the expected value; the plain MDP objective is risk-neutral, so a high-variance gamble with the best average is preferred.
A policy maps states to action distributions. The optimal policy satisfies for all and all . For finite MDPs, an optimal deterministic policy always exists — randomization is unnecessary. The policy improvement theorem guarantees that making a policy greedy with respect to its own value function produces a policy that is at least as good: where .
Gridworld: state A (near cliff), actions {safe_path, risky_shortcut}. Optimal policy if safe=+5, risky=+10 with 50% fall (-100)?
Value Function
The state-value function is the expected discounted return when the agent starts in state and follows policy forever after: the sum of over all future steps , averaged over the randomness in both actions and transitions. It turns a long sequence of future rewards into one number per state, which lets the agent compare situations. Values satisfy the Bellman equation: a state's value equals the expected immediate reward plus times the expected value of the next state. That recursion is what dynamic programming, temporal-difference learning, and Q-learning exploit. Values depend on the policy: a bad policy makes a good state look poor.
The Bellman equation is a system of linear equations in unknowns, solvable in by matrix inversion: . The Bellman optimality equation replaces the weighted sum over actions with a max: , which is nonlinear and requires iterative methods like value iteration.
γ=0.9. From state A, policy gives rewards: +10 (t=0), +10 (t=1), +10 (t=2), then 0 forever. V(A)?
Discount Factor γ
The discount factor , between 0 and 1, multiplies a reward received steps in the future by . It makes infinite sums finite, since returns are bounded by the maximum reward divided by , and it encodes a preference for sooner rewards. A rough effective horizon is steps: about 10 for 0.9 and 100 for 0.99. With the agent is myopic and maximizes only the next reward; as approaches 1 it becomes far-sighted but learning gets harder, because values become larger and noisier. The discount is part of the problem definition, though it is often tuned like a hyperparameter.
The discount factor ensures the infinite sum converges: . Economically, represents the agent's time preference — the present value of receiving reward at time is . The effective planning horizon is approximately steps: with , the agent considers the next ~100 steps; with , only ~10 steps matter.
Two paths: A gives +1 now, B gives +10 in 5 steps. Which is better for γ=0.5 vs γ=0.99?
Theory Exercise
Problem:
An agent in a 4-state MDP receives +1 reward every step until reaching a terminal state. Why must the discount factor γ be strictly less than 1 in a continuing (non-terminating) task, and what happens to the value function as γ → 1?
Hints:
- Write out the value function as an infinite geometric series
- Recall the convergence condition for a geometric series
- Think about the effective planning horizon 1/(1-γ)
Coding Exercise
Problem:
Implement iterative policy evaluation for a tiny 4-state chain MDP under a uniform-random policy, then report the converged state values for a given discount factor.
Hints:
- Loop the Bellman expectation backup V(s) ← Σ_a π(a|s) Σ_s' P(s'|s,a)[R + γV(s')] until values stop changing
- Represent transitions as a dict keyed by (state, action) → (next_state, reward)
- Stop when the largest value change (delta) falls below a small threshold
The previous topic, Markov Decision Process, defined states, actions, rewards, policies, and value functions, and the Bellman equation that ties them together. Solving that equation directly requires the transition probabilities, but a real agent rarely knows them. A robot does not have a table of how often its wheels slip, and a game agent cannot read the game's source code. All it can do is act, observe the reward and the next state, and remember. Can an agent learn to act optimally from those experience samples alone, while still behaving badly enough to explore?
Q-learning answers yes. This topic starts with the Q-function, the value of taking a specific action in a state and acting optimally afterwards, which makes choosing an action a simple maximum. The Bellman update moves each estimate toward a sampled target, one step of experience at a time. Epsilon-greedy exploration keeps trying alternative actions so better ones can be found. The topic closes with Deep Q-Networks, which replace the table with a neural network and learned Atari games from pixels.
Definition
Q-learning is a model-free, off-policy temporal-difference algorithm that estimates the optimal action-value function , the expected discounted return from taking action in state and acting optimally afterwards. After each transition it moves toward the target ; the greedy policy with respect to is optimal.
In this topic
- 1Q-Function
- 2Bellman Update
- 3ε-Greedy Exploration
- 4DQN (Deep Q-Network)
Q-Function
The action-value function scores a state-action pair: the expected discounted return from taking action in state and then following a given policy. The optimal version assumes optimal play afterwards and satisfies the formula above: the expected immediate reward plus times the best Q-value available in the next state . Q-values are more useful than state values for acting, because choosing the best action needs only , with no model of where each action leads. The price is size: a table needs one entry per state-action pair, which is why large problems approximate with a function.
The Q-function satisfies and for the optimal policy. Q-values encode both the immediate reward and the long-term consequences of an action, making them sufficient for decision-making without knowing the transition model. For states and actions, the Q-table has entries.
State s has actions {left, right}. Left→ reward 5, terminal. Right→ reward 2, then state s' with max Q(s',·)=10. γ=0.9. Q(s, left)? Q(s, right)?
Bellman Update
The Q-learning update turns the Bellman optimality equation into a sampling rule. After one transition, form the target ; the gap between target and current estimate is the temporal-difference error, and the learning rate moves the estimate that fraction of the way. Because the target uses the agent's own estimate of the next state, the method bootstraps: it learns from incomplete episodes without waiting for the final return. The max makes it off-policy: it learns the value of greedy behavior even while the agent explores. In tables it converges to if every pair is visited infinitely often and decays suitably.
The Q-learning update is a stochastic approximation of the Bellman optimality equation. The term is the temporal-difference error. Watkins and Dayan (1992) proved that tabular Q-learning converges to with probability 1 provided every state-action pair is visited infinitely often and the learning rates satisfy the Robbins-Monro conditions and . The behavior policy may be exploratory, which is what makes the method off-policy. With function approximation, such as a neural network, this guarantee no longer holds.
Q(s,a)=10, α=0.1, γ=0.9. Take action a, get r=5, reach s' where max Q(s',·)=20. New Q(s,a)?
ε-Greedy Exploration
An agent that always takes the action it currently believes is best may never discover a better one, because it never tries the alternatives. Epsilon-greedy is the simplest fix: with probability take the greedy action, and with probability pick an action uniformly at random, which can also be the greedy one. Typical schedules start near and decay to about 0.01 to 0.1 as estimates improve. Exploration has a cost while learning: on Sutton and Barto's cliff-walking grid, Q-learning learns the shortest path along the cliff edge, but its random moves occasionally fall off, so its online returns are worse than a more cautious method's.
Epsilon-greedy selects the greedy action with probability and each non-greedy action with probability . On the 4 by 12 cliff-walking grid with and , Q-learning learns the optimal 13-step path along the cliff edge but averages about -49 per episode while training, because random moves knock it off the cliff. SARSA, which learns the value of its own exploratory policy, prefers a safer 17-step path and averages about -36. Decaying toward 0 while keeping every action tried infinitely often, a GLIE schedule, recovers convergence to the optimal greedy policy.
Q(s, left)=5, Q(s, right)=10, Q(s, up)=3. ε=0.2, 4 actions total. P(taking right)? P(taking left)?
DQN (Deep Q-Network)
A Deep Q-Network replaces the Q-table with a neural network that maps a state, such as four stacked Atari frames, to one Q-value per action. Training it naively is unstable, for two reasons. Consecutive transitions are strongly correlated, and the target uses the same network being updated, so every step moves the target too. DQN fixes the first with experience replay: transitions go into a buffer of up to a million, and minibatches are sampled at random. It fixes the second with a target network, a frozen copy used for the targets and refreshed every 10,000 steps. With these, Mnih et al. (2015) reached human-level play on many Atari games.
DQN approximates with a neural network, using experience replay (uniform sampling from a buffer of tuples) to break temporal correlations and a target network updated every steps to stabilize the target . The loss is . Double DQN decouples action selection from evaluation: but evaluates with , reducing overestimation bias.
Why does DQN need a target network updated every 10K steps instead of using the same network?
Theory Exercise
Problem:
Q-learning is described as 'off-policy.' Explain what this means by contrasting its update target with SARSA's, and state why Q-learning can learn the optimal policy even while following an exploratory ε-greedy policy.
Hints:
- Write down the Q-learning target and the SARSA target side by side
- Note which action's Q-value appears in each target: the max action vs the actually-taken next action
- Connect 'off-policy' to learning about a policy different from the one generating behavior
Coding Exercise
Problem:
Implement tabular Q-learning with an ε-greedy policy on a simple 1D corridor environment (start at the left, reward at the right end) and print the learned greedy action for each state.
Hints:
- Maintain a Q-table of shape (n_states, n_actions) initialized to zeros
- Apply the update Q[s,a] += alpha*(r + gamma*max(Q[s2]) - Q[s,a]) after each step
- Use np.argmax(Q[s]) to read off the greedy action once training finishes
The previous topic, Q-Learning, learned the value of each action and acted greedily on those values. That approach struggles in two common situations. If actions are continuous, such as joint torques for a robot arm, taking a maximum over infinitely many actions is itself an optimization problem at every step. And a greedy policy is deterministic, while some tasks need a stochastic one, such as rock-paper-scissors or partially observed settings. Learning values is also indirect: we care about behavior, so why not learn the policy itself and push it toward higher reward?
Policy gradient methods do exactly that. This topic starts with policy parameterization: a neural network that outputs action probabilities, or the mean and spread of a continuous distribution. The REINFORCE algorithm estimates the gradient of expected return from sampled episodes and makes rewarded actions more likely. Baselines and the advantage function cut the high variance of that estimate. The topic closes with actor-critic methods, which learn a value function alongside the policy, the design behind A3C and PPO.
Definition
Policy gradient methods parameterize a stochastic policy directly and perform gradient ascent on the expected return . The policy gradient theorem gives , which can be estimated from sampled trajectories without a model of the environment, then followed uphill by stochastic gradient ascent.
In this topic
- 1Policy Parameterization
- 2REINFORCE Algorithm
- 3Baseline / Advantage
- 4Actor-Critic
Policy Parameterization
A parameterized policy is a function, usually a neural network with weights , that turns a state into a distribution over actions. For discrete actions the network outputs one logit per action and a softmax turns them into probabilities. For continuous actions it typically outputs the mean and standard deviation of a Gaussian, and the action is sampled from it. Sampling makes exploration built in, and the policy can narrow its spread as it grows confident. Bounded actions need care: sample then clip, or squash through tanh, as Soft Actor-Critic does. Changing changes behavior smoothly, which is what makes gradients possible.
A parameterized policy outputs action probabilities (discrete: softmax over logits) or distribution parameters (continuous: for Gaussian ). The policy gradient theorem states , which can be estimated from trajectories without knowing the environment dynamics — the key advantage over model-based methods.
Robot arm with continuous torque control [-1, 1]. How does policy network output actions vs discrete case?
REINFORCE Algorithm
REINFORCE, from Williams (1992), estimates the policy gradient from complete episodes. For each step, is the direction in parameter space that makes the taken action more likely; it is multiplied by , the discounted return from that step onward. Averaging over episodes gives an unbiased estimate of , so actions followed by high returns become more likely and actions followed by low returns less likely. No value function or model is needed. The weakness is variance: sums many random rewards, so estimates are noisy, and learning needs many episodes, which motivates baselines.
REINFORCE estimates where is the return from time . The estimator is unbiased, but each sums many random rewards, and every log-probability term in a long episode is weighted by those noisy returns, so the variance grows with the horizon and with the scale of the rewards. Subtracting a baseline leaves the expectation unchanged, since , while removing much of that variance: .
Episode: took action a with π(a|s)=0.3, got return G=100. How does gradient update affect π(a|s)?
Baseline / Advantage
Subtracting a baseline from the return leaves the policy gradient unbiased, because the expected value of times any function of the state alone is zero. A good baseline, however, cuts variance sharply. The natural choice is the state value , which turns the weight into the advantage : how much better this action was than the policy's average in that state. Positive advantages make an action more likely and negative ones less likely, so the update finally separates good actions from bad ones instead of pushing up whatever was sampled.
The advantage function measures how much better action is compared to the average. Using instead of as the policy gradient weight centers the signal: positive for better-than-average actions, negative for worse. The TD advantage estimate has lower variance than Monte Carlo returns () but introduces bias from the value function approximation — a bias-variance tradeoff controlled by GAE().
V(s)=50. Two actions: Q(s,a₁)=60, Q(s,a₂)=40. What are their advantages? Why is this better than raw Q?
Actor-Critic
Actor-critic methods learn two functions. The actor is the policy . The critic is a value function , trained by temporal-difference learning toward . Instead of waiting for the full return, the actor uses the critic's one-step TD error as its advantage estimate, so it can update every step and with far less variance than REINFORCE. The cost is bias, because the critic is only an estimate. Generalized advantage estimation blends multi-step returns to tune this trade-off. A3C, PPO, and SAC are all actor-critic designs.
Actor-critic maintains two networks: the actor and the critic . The critic is trained to minimize via TD learning, while the actor is updated with using the critic's advantage estimate. PPO constrains actor updates with a clipped objective: where .
Actor outputs π(a|s). Critic outputs V(s)=20. Agent gets r=5, next state has V(s')=25. γ=0.9. What's the TD error for critic? Advantage for actor?
Theory Exercise
Problem:
In REINFORCE the gradient estimate is ∇J ≈ ∇log π(a|s)·G_t. Show why subtracting a state-dependent baseline b(s) reduces variance without introducing bias.
Hints:
- Compute the expectation of ∇log π(a|s)·b(s) over actions for a fixed state
- Use the identity Σ_a π(a|s)∇log π(a|s) = ∇ Σ_a π(a|s) = ∇1 = 0
- Then argue that subtracting a correlated baseline lowers the variance of the estimator
Coding Exercise
Problem:
Implement the REINFORCE return computation: given a list of per-step rewards and a discount factor, compute the discounted return G_t at every timestep, then the variance-reducing centered (advantage-style) returns.
Hints:
- Compute returns backwards: G_t = r_t + γ·G_{t+1}, starting from the episode end
- Use a running accumulator and reverse iteration for an O(T) computation
- Center the returns by subtracting their mean (and optionally dividing by std) as a simple baseline