Skip to content
mlmentorship

Q-learning

Learn the action-value function Q(s, a) by Bellman backups. The foundation of value-based RL. DQN, Rainbow, and the original Atari breakthroughs.

Published · 6 min read ·Specialist ·Intermediate

Visual quick review

Visual first · depth when needed

Given one transition and the next state's action values, distinguish the action sampled by the behavior policy from the greedy action used to construct the Q-learning target.

Preparing the visual…

Summary

Q-learning (Watkins, 1989) is an off-policy temporal-difference algorithm that learns the optimal action-value function . The expected return from taking action in state and then acting optimally. By iterating the Bellman optimality update:

Q-learning is the canonical value-based RL algorithm. Combined with deep neural networks (DQN; Mnih et al., 2015), it produced the original deep RL breakthroughs on Atari and remains the foundation of value-based methods. Knowing Q-learning is the prerequisite for understanding: target networks, experience replay, double DQN, dueling networks, and the relationship to actor-critic methods.

The setup

A Markov decision process (MDP): states , actions , transition , reward , discount .

The optimal action-value function:

The optimal policy: .

Bellman optimality

satisfies the Bellman optimality equation:

Q-learning approximates this by sampling: take action , observe and , update toward the target .

Tabular Q-learning

For small finite state-action spaces, store as a table. Sample transitions , apply the update with learning rate . Guaranteed to converge to if every state-action pair is visited infinitely often and learning rates satisfy Robbins-Monro conditions.

Off-policy: the update uses regardless of which action is actually taken next. This decouples exploration policy (e.g., -greedy) from the learned greedy policy.

Learning objective: given one transition and the next state’s action values, distinguish the action sampled by the behavior policy from the greedy action used to construct the Q-learning target.

One off-policy backup

The trajectory can explore while the target looks ahead greedily.

Q-learning separates behavior from the greedy backup target A sampled transition moves from state s with action a and current value 4 to next state s prime with reward 2. At s prime, three possible next actions have Q-values: up 2, right 6, and down 1. The behavior policy samples down to keep exploring, but that sampled next action is not used in this update. The target policy selects the maximum value 6 from right. With discount one half, the target is 2 plus one half times 6, which equals 5. The temporal-difference error is 5 minus 4, or 1. With learning rate one quarter, the updated current Q-value is 4.25. 1 · OBSERVE ONE TRANSITION current pair (s, a) Q(s, a) = 4 reward r = 2 next state s′ consider every action 2 · SEPARATE BEHAVIOR FROM TARGET possible action a′ Q(s′, a′) ↑ up → right ↓ down 2 6 · MAX 1 behavior samples down; not used in this target 3 · MOVE THE CURRENT VALUE TOWARD THE TARGET target = r + γ max Q(s′, a′) = 2 + 0.5 × 6 = 5 TD error = target − current = 5 − 4 = 1 Q(s, a) ← 4 + 0.25 × 1 = 4.25
Read it this way: use the sampled transition to obtain s′ and r, then scan every action value at s′ for the maximum. Here exploration samples down, but the target uses right because its value is 6. That mismatch is intentional: behavior gathers data, while the greedy target defines what Q-learning learns.

Deep Q-Networks (DQN)

For large state spaces, replace the table with a neural network . Two essential tricks make this work:

  1. Experience replay: store transitions in a buffer, sample mini-batches uniformly. Breaks temporal correlation; stabilizes training; enables data reuse.
  2. Target network: maintain a separate frozen copy for the target . Update to every steps. Prevents the target from chasing itself.

The DQN loss:

Variants

  • Double DQN (van Hasselt 2015): use from online network, value from target network. Reduces overestimation bias.
  • Dueling DQN (Wang 2016): factor .
  • Prioritized replay (Schaul 2015): sample transitions with high TD error more often.
  • Rainbow (Hessel 2018): combines six improvements; canonical strong baseline.
  • Distributional RL (C51, IQN): predict the distribution of returns, not just the mean.

Limitations

  • Maximization bias: is biased upward when is noisy. Double DQN partially fixes.
  • Continuous action spaces: becomes a non-trivial optimization; use deterministic policy gradients (DDPG, TD3, SAC) instead.
  • Sample efficiency: deep Q-learning needs millions of environment steps; impractical for slow simulators.
  • Off-policy correction: off-policy data can be biased; DQN papers often need careful replay buffer management.

Q-learning vs. policy gradient

MethodQ-learningPolicy gradient
Learns
PolicyImplicit ()Explicit
On/off policyOff-policyUsually on-policy
Continuous actionsHard ()Natural
VarianceLowerHigher
Sample efficiencyHigher (data reuse)Lower

In practice for continuous control: SAC (combines Q-learning with stochastic policy). For discrete actions with small action space: DQN-family. For large discrete action spaces (LLMs): policy gradient or DPO-family.

Common pitfalls

  • Skipping the target network. Loss explodes; training diverges.
  • Skipping experience replay. Successive samples are highly correlated; gradient estimates are biased.
  • Confusing on-policy and off-policy. Q-learning is off-policy: you can learn from old data with a different policy.
  • Ignoring exploration. Greedy policy from a randomly-initialized is terrible; -greedy with decaying is the standard.