Key ideas
- Value-based methods learn how good states or state–action pairs are and derive the policy from that. The action-value is the expected return after taking in and then following . Once the optimal is known, acting greedily, , is optimal and needs no model of the environment3.
- Bellman equation: a recursive definition: the value of a state is the expected immediate reward plus the discounted value of the next state3.
- Value iteration (dynamic programming) needs the model and and repeats the Bellman optimality backup for every state; it converges to whatever the initial values3. One sweep costs products, e.g. on an grid.
- Monte Carlo vs TD. Monte Carlo waits for the end of the episode and moves toward the actual return . TD(0) updates after a single step toward , using its own current estimate of the next state (bootstrapping)1.
- Q-learning is an off-policy, value-based TD method that trains the Q-function; in the tabular case the Q-table has one cell per state–action pair and acts as the agent's memory2. Its hyperparameters are the learning rate , the discount and the exploration rate .
- ε-greedy: with probability take a random action (explore), otherwise the action with the highest Q-value (exploit); starts high, e.g. 1.0, and is decayed as the table improves2.
- Off-policy vs on-policy. Q-learning acts ε-greedily but its target uses the greedy : two different policies. SARSA is on-policy: its target uses the action the ε-greedy policy actually picks2. On the cliff-walking grid Q-learning therefore learns the optimal path right along the edge (and, while it still explores, sometimes falls off), whereas SARSA learns a longer, safer path that allows for its own exploratory slips7.
- Because the target takes a max, Q-learning can learn even from a random behaviour policy; poor coverage of the state space, however, gives poor estimates everywhere, so the exploration policy matters4.
- Deep Q-learning. Tabular methods only work for small spaces: an Atari frame of values in 0–255 can take different values, far too many for a table, so a network replaces it8. In DQN, frames are reduced to grayscale, and four frames are stacked because one frame shows no motion (temporal limitation)5.
- DQN alternates a sampling phase (act, store transitions in a replay memory) and a training phase (learn from a random minibatch). Experience replay reuses each transition many times, breaks the correlation between consecutive samples and reduces catastrophic forgetting. A fixed Q-target network, copied from the online network every steps, keeps the target from moving at every update. Double DQN lets the online network choose the next action and the target network evaluate it, which reduces the overestimation caused by taking a max over noisy values6.
Formulas
- The bracket is the TD error; is the TD target. If is terminal, the target is just 4.
- SARSA target: 2.
- DQN target ; Double DQN 6.
Worked example
, reward , , , and .
- Q-learning: target , TD error , new .
- SARSA, if the ε-greedy policy actually picked the second action in : target , new value .
- Double DQN with online values and target-network values at : the online net picks the first action, the target net scores it 3, so ; plain DQN would use and get .
In code
import numpy as np
Q, goal = np.zeros((5, 2)), 4 # chain 0-1-2-3-4, actions 0 = left, 1 = right
alpha, gamma, eps = 0.5, 0.9, 0.3
rng = np.random.default_rng(0)
for episode in range(200):
s = 0
while s != goal: # entering state 4 gives reward 1 and ends
a = int(rng.integers(2)) if rng.random() < eps else int(Q[s].argmax()) # eps-greedy
s2 = max(s - 1, 0) if a == 0 else s + 1
r = 1.0 if s2 == goal else 0.0
target = r + gamma * Q[s2].max() * (s2 != goal) # no bootstrap from a terminal state
Q[s, a] += alpha * (target - Q[s, a]) # move toward the TD target
s = s2
print(Q[:goal].round(3).tolist()) # [[0.656, 0.729], [0.656, 0.81], [0.729, 0.9], [0.81, 1.0]]
The "right" column is and the "left" one is times the value of the state reached (state 0 stays put), i.e. to three decimals: each update moves an entry halfway to its TD target, and the reward spreads backwards from the goal one state at a time.
Common pitfalls
- Q-learning is the algorithm; the Q-function is the action-value function it trains; the Q-table is where the values are stored. Each cell holds a state–action value, not a state value2.
- Monte Carlo cannot update before the episode ends; TD can, because it bootstraps1.
- Value iteration needs the transition model; Q-learning is model-free and learns from sampled transitions3,4.
- Forgetting the terminal case (bootstrapping from a terminal state) corrupts the values4.
- With all Q-values 0, a first-action tie-break and , the agent may repeat the same move forever on a sparse-reward map and never learn; with only states one step from a reward get nonzero values.
- Classic Q-learning fails on Atari because the state space is gigantic, not because the actions are continuous (Atari has a few discrete actions) or because games are fast8.
- One frame is not a Markov state; stacking four frames adds the motion information5.
- Taking a max over noisy estimates is biased upward (overestimation); Double DQN decouples selection from evaluation, the replay buffer and target network address correlation and moving targets6.
References
- Hugging Face Deep Reinforcement Learning Course — Unit 2, Monte Carlo vs Temporal Difference Learning
- Hugging Face Deep Reinforcement Learning Course — Unit 2, Introducing Q-Learning: What is Q-Learning?; The Q-Learning algorithm; Off-policy vs On-policy
- Dive into Deep Learning — Sec. 17.2 Value Iteration (17.2.2–17.2.6, 17.2.9 Summary)
- Dive into Deep Learning — Sec. 17.3 Q-Learning (17.3.1–17.3.3, 17.3.6 Summary)
- Hugging Face Deep Reinforcement Learning Course — Unit 3, The Deep Q-Network (DQN) — Preprocessing the input and temporal limitation
- Hugging Face Deep Reinforcement Learning Course — Unit 3, The Deep Q-Learning Algorithm: Experience Replay; Fixed Q-Target; Double DQN
- Sutton & Barto (2018), Reinforcement Learning: An Introduction, 2nd ed. — Sec. 6.5 Q-learning: Off-policy TD Control, Example 6.6 Cliff Walking
- Hugging Face Deep Reinforcement Learning Course — Unit 3, From Q-Learning to Deep Q-Learning