- An RL agent learns by acting, observing rewards, and updating its estimates.
- Q-learning updates action values from sampled transitions without learning T or R explicitly.
- Exploration is required, but it costs reward and can make learning noisy.
This lesson follows Markov Decision Processes and completes the published Machine Learning curriculum. The MDP background is in the previous lesson.
1. Learn by interacting
An agent has never seen the reward. How can it learn which action is best without a teacher telling it the right answer?
Reinforcement learning replaces the dataset with an environment. The agent observes a state, chooses an action, receives a reward, and observes the next state. The data it learns from are generated by its own choices.
That feedback loop is the defining difficulty. A supervised model can revisit a fixed dataset; an RL agent may never see a useful state unless it explores. The reward can be delayed, sparse, noisy, or misspecified.
The animation shows a tabular agent in a small grid. Before reading the Q update, predict why the agent cannot simply always choose the action with the highest current Q value.
All Q values start at zero. If the agent always takes the current best action, why might it never find the goal?
2. Model-free vs model-based
A model-based method learns or is given the transition and reward model, then plans with it. A model-free method skips the model and learns values or a policy directly from experience.
Value-based methods estimate Q(s,a). Policy-based methods optimize a parameterized policy directly. Actor-critic methods combine a policy with a value estimate. The categories overlap in practice.
Q-learning is model-free and off-policy: it updates Q toward the best next action, even if the behavior policy — the policy choosing actions during learning — chose a different action. That lets it learn from exploratory data.
3. Q-learning
Suppose Q(s,a) = 0, the observed reward is 10, the best next Q value is 5, γ = 0.9, and α = 0.5. The target is 10 + 0.9 × 5 = 14.5, so the update moves the estimate to 0 + 0.5(14.5) = 7.25.
The Q value is the expected return after taking action a in state s and then acting well. Q-learning moves the current estimate toward a one-step target:
Here s is the current state, a is the current action, s' is the next state, r is the observed reward, α is the learning rate, and γ is the discount. The bracket is the temporal-difference error: target minus current estimate. The target uses the best next Q value, not the action actually taken.
With a finite table, enough exploration, and a suitable learning-rate schedule, tabular Q-learning converges to the optimal Q function. In practice, constant α and limited episodes leave noise.
4. Explore or exploit
An ε-greedy policy exploits the best current action most of the time and explores randomly with probability ε. With ε = 0.2, the agent explores on 20% of steps.
A greedy agent can get stuck in a local pattern because it never tries the action that would reveal a better path. A purely random agent explores but never exploits what it has learned.
Exploration can be decayed over time, made optimistic, or driven by uncertainty. Each choice changes the data distribution the agent sees.
Why is ε-greedy not a complete solution to exploration?
5. Worked example
A hallway has a reward 1000 at the far right. Moving right at position 9 gives 1000; moving right at position 8 gives 0 + 0.9 × 1000 = 900; position 7 gives 810. The value propagates backward one cell per visit.
Worked example: one-step Q targets
| state-action | reward | target r + γ max Q(next) | Q |
|---|---|---|---|
| (9, right) | 1000 | 1000 | 1000 |
| (8, right) | 0 | 0 + 0.9(1000) | 900 |
| (7, right) | 0 | 0 + 0.9(900) | 810 |
A greedy agent that has not visited the end sees no reason to go right. Exploration is what produces the first 1000; after that, the value can propagate backward.
The animation’s grid has a much smaller reward, but the same backup: reward plus discounted best next value. Its run reached 253 successful episodes out of 400 in this seeded simulation.
6. Function approximation
A table cannot represent a continuous state or a huge game. A function approximator Q_θ(s,a), with parameters θ, predicts values from features and is trained on sampled Bellman targets. This is fitted Q-learning (fit a regressor to sampled targets), or a deep Q-network (DQN) when the approximator is a neural network.
Function approximation introduces instability. The target depends on the same weights being updated; correlated trajectories can cause catastrophic forgetting; a small change in Q can change the argmax action. Experience replay (reuse past transitions) and target networks (freeze a copy of Q when computing targets) are standard stabilizers.
Policy-gradient methods instead differentiate the expected return with respect to policy parameters. REINFORCE increases the probability of actions that led to high return. Actor-critic methods reduce variance with a value estimate. If Q(s,left) = 7.3 and Q(s,right) = 8.1, the greedy action is right.
Here π(s) is the action chosen in state s. A greedy policy takes the highest-valued action; an ε-greedy policy keeps that choice most of the time and explores otherwise.
7. Where it fails
Reward design is hard. An agent optimizes the reward it is given, not the outcome you intended. A proxy can be gamed, and a sparse reward may never be discovered.
Exploration can be expensive or unsafe. Online learning in a real system needs constraints, simulators, or human oversight. Offline RL must handle distribution shift and rarely seen actions.
Q-learning assumes enough coverage of state-action pairs. In large or continuous spaces, generalization can be wrong exactly where the agent needs it. A high average reward is not a guarantee for every starting state.
This is the last published lesson. The curriculum now spans foundations, supervised learning, unsupervised learning, deep learning, transformers, and reinforcement learning.
8. Going deeper
DQN combines Q-learning with a neural network, experience replay, and a target network. Double Q-learning reduces overestimation bias; dueling architectures separate state value from action advantage; prioritized replay focuses on surprising transitions.
Policy gradients lead to trust-region policy optimization (TRPO), proximal policy optimization (PPO), and actor-critic methods. Model-based RL learns a dynamics model and plans or generates imagined experience. Bandits are the one-state special case where exploration is the whole problem.
Evaluation is still the hard part. A single training curve can hide seed variance, reward hacking, and failures on rare states. Report distributions, not one lucky run.
9. Practice
Why does Q-learning use max over next actions instead of the action the behavior policy took?
A reward of 1 is received every step forever, with γ = 0.9. What is the return from the start?
Why can a Q-table fail on a continuous state space?
10. Resources
The MIT chapter covers model-free methods, Q-learning, function approximation, policy gradients, and bandits. Sutton and Barto’s book is the standard reference for the full theory. Both links were opened and checked for this lesson.