Evaluation lab · San Francisco · London · Zürich
Reinforced Labs Request a briefing
Reinforcement learning

Q-learning explained: the update rule, worked examples and DQN

Q-learning learns the value of every action in every state from experience alone, then acts greedily on those values. We work the update by hand, let you watch an agent learn, and report seeded results: why a constant step size stalls short of optimal, why Q-learning hugs the cliff that SARSA avoids, and how double Q-learning removes the maximisation bias.

Part of Reinforcement learning. New to the topic? Start with What is reinforcement learning? A guide from gridworlds to language models.

Q-learning is a model-free reinforcement learning algorithm that learns the value of taking each action in each state, called the Q-value, directly from experience. After every step it nudges its estimate toward the reward it just received plus the discounted value of the best action in the next state. Repeated enough, with every state-action pair tried often enough, those estimates converge to the optimal action values, and acting greedily on them is an optimal policy.

It needs no model of the environment, it can learn the optimal policy while behaving quite differently, and with a neural network in place of the table it became the deep Q-network (DQN). This guide works an update by hand, lets you watch an agent learn, and reports seeded simulations of step sizes, exploration, Q-learning against SARSA, and the maximisation bias behind double Q-learning.

A worked example: two updates by hand#

We use the reference gridworld shared by this series of articles: a 5 by 5 grid with a goal worth +1 in the top-right corner, a pit worth −1 just left of the square below it, three walls, and a start in the bottom-left. Moves slip: the agent goes where it intended with probability 0.8 and slides to one of the two perpendicular directions with probability 0.1 each. Bumping into a wall or the edge leaves it in place. Rewards arrive on entering a square, the discount is , and the goal and pit end the episode. The Markov decision process article builds this world in detail.

The Q-table starts at zero. Suppose the agent stands directly below the goal, at row 1, column 4, chooses up, and enters the goal. With learning rate , the update is

There is no future term because the goal is terminal. Later the agent is one square further down, at row 2, column 4, chooses up and lands below the goal with reward 0. Now the target uses the value just learned:

This is the whole mechanism. Value seeps backwards from rewards one transition at a time, discounted by and damped by . Nothing told the agent where the goal is or how the slip works; it only saw transitions.

Where does it end up? If the estimates converge, the value of up below the goal must satisfy its own consistency condition. Up succeeds with probability 0.8 (reward +1), slips right into the edge with probability 0.1 (stay put, then continue with value ), and slips left into the pit with probability 0.1 (reward −1). With the value of that square under optimal play,

Up is the best action there, so , and solving gives . Value iteration on the full model gives the same 0.769, and it gives 0.293 for the start square. Q-learning should approach those numbers without ever seeing the model. The rest of this article is about when and how fast it does.

What is Q-learning, formally?#

An agent in a Markov decision process picks actions to maximise the expected return . The optimal action-value function is the best expected return achievable after taking in . It satisfies the Bellman optimality equation

where the expectation is over the reward and next state produced by the environment, and is the discount. In words: the value of an action is what you get now plus the discounted value of the best thing you can do next. If you knew , the optimal policy would be to pick in each state, with no lookahead and no model.

If you know the transition probabilities you can solve this equation by sweeping it to a fixed point, which is value iteration. Q-learning solves the same equation when you do not know them, by replacing the expectation with single samples.

The Q-learning update rule#

Here and are the current state and action, and the observed reward and next state, the learning rate (step size), and the discount. When is terminal the max term is dropped. The bracket is the temporal-difference (TD) error : how much better or worse this transition turned out than the current estimate predicted. The update moves the estimate a fraction of the way toward the target.

The target is a noisy but unbiased sample of the Bellman optimality backup applied to the current estimate, so averaging many samples with a suitable step size recovers the expectation. It also bootstraps: it borrows the agent's own estimate of the next state's value instead of waiting for the episode to end, as a Monte Carlo method would. That is what lets value propagate a step at a time, and it is also the source of the instabilities discussed under DQN.

The algorithm, step by step#

  1. Initialise for all states and actions, usually to zero; terminal states stay at zero.
  2. At the start of each episode, observe the initial state .
  3. Choose an action with an exploratory behaviour policy, typically ε-greedy: a uniformly random action with probability , otherwise with ties broken at random.
  4. Take the action, observe and .
  5. Update with the rule above, using the max over actions in (or no future term if is terminal).
  6. Set and repeat from step 3 until the episode ends; then start a new episode.
  7. When finished, act greedily: .

The ε-greedy rule is the same exploration device as in multi-armed bandits; a bandit is the one-state special case of this problem.

Why Q-learning is off-policy#

The target uses , the value of the greedy action in , whatever action the agent actually takes next. So the values being learned are those of the greedy policy, while the data come from the exploratory ε-greedy policy. Learning about one policy from data generated by another is what off-policy means. It is why Q-learning can learn from random play, from logged data, or from a replay buffer of old experience, and it is the starting point for offline reinforcement learning, where the data were collected by someone else entirely.

Does Q-learning converge?#

Watkins sketched a convergence argument in his 1989 thesis [1], and Watkins and Dayan gave the proof in 1992 [2]: in a finite MDP, tabular Q-learning converges to with probability 1 provided every state-action pair is updated infinitely often and the step sizes for each pair satisfy the Robbins-Monro conditions

where is the step size used on the -th update of that pair. The first condition lets the estimate travel any distance; the second makes the noise die out. A schedule like satisfies both. A constant fails the second, so the estimates keep fluctuating forever around , which is often what you want in practice when the environment drifts, but it means "converged" is only approximately true. Note what the theorem does not ask for: it does not require the behaviour policy to be greedy, or even sensible. Any policy that keeps visiting every pair will do.

We checked this on the reference gridworld (simulation, 20 seeds). With a uniformly random behaviour policy and per pair, the largest error over all state-action pairs, , was 0.39 after 100 episodes, 0.15 after 1,000, 0.069 after 5,000 and 0.035 after 20,000. The agent never once acted on its estimates, yet it learned the optimal action values. That is off-policy learning in its purest form.

Watch a Q-learning agent learn#

The figure runs the update rule live: each square shows and the greedy action, and the curve below tracks the return per episode. Switch to cliff walking, a deterministic 4 by 12 world from Sutton and Barto [7], or to SARSA, the on-policy alternative below. Set to zero and the agent stops exploring; push toward one and the values jitter.

0.040.500.540.65+10.100.35−10.620.210.350.290.500.280.310.330.43S0.280.310.340.370.41Discounted return per episode, mean of last 20−101V*(start) = 0.29302000 episodes
0.10 0.10 5/frame

Figure 1. Tabular Q-learning (or SARSA) with ε-greedy exploration, using exactly the update rule in the text. Squares show and the greedy action; the dot is the agent. The curve is the mean return of the last 20 episodes; in the gridworld the dashed line is the optimal from value iteration, and the readout reports the largest gap between and . Every reset uses the same random seed, so a run is reproducible. The static version shows one run (the default seed) in the reference gridworld after 2,000 episodes of Q-learning with , ; Figure 2 shows the average over 50 seeds.

What the simulations show: step size and exploration#

Setup (simulation, seeded). We ran Q-learning on the reference gridworld for 2,000 episodes (capped at 200 steps each) with every combination of and , 50 seeds each. Every 25 episodes we took the greedy policy and computed its exact value from the start square by solving the Bellman equation for that policy, so the score has no evaluation noise. The optimum is 0.293.

00.10.20.30500100015002000Episodes of Q-learning (ε = 0.1)Value of greedy policy at startoptimal V*(start) = 0.293α = 0.05α = 0.1α = 0.3α = 0.5
Figure 2. Exact value of the greedy policy at the start square during learning, mean of 50 seeded runs, . All constant step sizes plateau well below the optimum of 0.293, and larger steps plateau lower. Simulation.
Learning rate αGreedy value at start after 2,000 episodes (ε = 0.1)Online discounted return, last 200 episodesLargest error
0.050.212 ± 0.0200.1570.545 ± 0.026
0.10.197 ± 0.0250.1420.498 ± 0.031
0.30.150 ± 0.0290.1360.449 ± 0.028
0.50.148 ± 0.0290.1080.477 ± 0.036

Three results stand out, and none of them is what a quick tutorial would lead you to expect.

A constant step size leaves a gap that does not close. With , the greedy policy reached within 0.01 of optimal in only 6 of 50 runs after 2,000 episodes. The reason is not a lack of data: the states where the greedy action was wrong had been visited a median of 759 times. The reason is that the action gaps in this world are tiny. At the start square, going up is worth 0.2933 and going right 0.2906, a difference of 0.0027, while a constant- estimate keeps jittering by several hundredths because each update weights the most recent noisy, slip-affected transition by 10%. When the noise in is larger than the gap between actions, the argmax is a coin flip. Small gaps also mean small losses per state, but they compound along the route: the median greedy policy was worth 0.206.

Decaying the step size helps, slowly. With the per-pair schedule and (a separate set of 30 seeds), the mean greedy value at the start was 0.248 ± 0.014 after 2,000 episodes and 0.257 ± 0.013 after 10,000, against 0.201 ± 0.030 and 0.214 ± 0.035 for constant on the same seeds. The share of runs within 0.01 of optimal after 10,000 episodes rose from 13% to 33%. This is the Robbins-Monro condition at work: averaging shrinks the noise until small action gaps can be resolved. It is slow because a decaying step size also slows learning in the squares the greedy agent rarely reaches, and with there are several of those.

The table is only as good as the states you visit. The largest-error column stays near 0.5 for every setting, dominated by squares the greedy agent almost never reaches, whose Q-values stay near zero. More exploration () shrank it to about 0.44 but cut the online return collected while learning from 0.142 to 0.089 at : the bandit trade-off between exploring and exploiting, spread over 21 states.

Q-learning vs SARSA#

SARSA, introduced by Rummery and Niranjan as "modified connectionist Q-learning" [3] and named for the quintuple it uses, differs from Q-learning in one term:

where is the action the agent will actually take next, drawn from its ε-greedy policy. SARSA therefore learns the value of the policy it is following, exploration included. It is on-policy. Q-learning learns the value of the greedy policy while following another. When exploration is costly, those two values differ, and the classic illustration is the cliff.

Setup (simulation, seeded). Cliff walking [7]: a 4 by 12 grid, start at the bottom left, goal at the bottom right, and the ten squares between them are a cliff. Every step costs −1; stepping into the cliff costs −100 and sends the agent back to the start. No slip, no discount. Both methods used and for 500 episodes, 100 seeds each.

cliff: −100, back to startSGQ-learning: 13 steps on the edge (100 of 100 runs)SARSA: 17 steps on the top row (82 of 100)
Figure 3. Greedy paths learned on cliff walking. Q-learning finds the shortest route, one row above the cliff. SARSA, which accounts for its own random steps, keeps to the top row. Simulation, 100 seeds per method.
−120−90−60−3000100200300400500EpisodeReturn per episode (mean of 100 runs)best possible: −13Q-learning (learns the edge path)SARSA (learns the safe path)
Figure 4. Return per episode while learning, mean of 100 seeded runs, 10-episode moving average. The method that learns the better policy collects less reward during learning, because its exploratory steps sometimes fall off the edge it hugs. Simulation.

Every one of the 100 Q-learning runs learned the optimal 13-step path along the edge, worth −13. But while learning with , it averaged −49.6 (95% interval ±0.7) per episode over episodes 101 to 500, because one random step in ten from the edge row sometimes lands in the cliff. SARSA averaged −27.5 (±0.7): its values include the risk of its own exploration, so it prefers the 17-step route along the top row, which it learned in 82 of 100 runs (one more found a 19-step variant). In the other 17 runs the purely greedy SARSA policy circled without reaching the goal; SARSA's values describe the exploring policy, and acting fully greedily on them is not guaranteed to work. If is decayed toward zero, both methods converge to the optimal path.

Q-learningSARSA
Target, actually taken
Learns the value ofThe greedy policy (off-policy)The behaviour policy, exploration included (on-policy)
Cliff walking, learned pathShortest, along the edgeSafer, away from the edge
Return while exploringLower when exploration is dangerousHigher
Can learn from logged or replayed dataYesOnly data from its current policy
Use whenYou will deploy the greedy policy and exploration is cheap or simulatedThe agent must keep exploring in the real system

The cliff is also a small safety lesson: a method can be optimal by its objective and still behave badly in the regime it actually runs in. Safe reinforcement learning handles this with explicit constraints.

Maximisation bias and double Q-learning#

The max in the target has a statistical side effect. If the estimates are noisy but unbiased, the maximum of them is biased upward: , because the max preferentially picks whichever estimate happened to be too high. Q-learning then bootstraps on that inflated value and passes the bias backwards. Van Hasselt showed that this overestimation can be large in stochastic environments and proposed double Q-learning [4]: keep two tables, and , and on each step update one of them at random, choosing the action with one table and evaluating it with the other:

Because the noise in is independent of the noise that made choose that action, the estimate is no longer biased upward (it can be slightly biased downward). The agent acts ε-greedily on .

Setup (simulation, seeded). The example from Sutton and Barto [7, Example 6.7]: from state A, right ends the episode with reward 0; left moves to state B with reward 0, where any of 10 actions ends the episode with a reward drawn from a normal distribution with mean −0.1 and standard deviation 1. Going left is worse in expectation, so with an optimal agent goes left only 5% of the time, through exploration. We ran 10,000 seeded runs of 300 episodes each with .

02550751001100200300Episode% of episodes choosing left at Aoptimal under ε = 0.1: 5%Q-learningDouble Q-learning
Figure 5. Fraction of runs choosing left at A in each episode. The max over ten noisy estimates in B looks attractive to Q-learning; double Q-learning is barely fooled. Simulation, 10,000 runs.

Q-learning went left in 93.9% of runs at its worst, around episode 21, and was still at 11.8% after 300 episodes. Double Q-learning never exceeded 50.3% (that peak is the coin flip of the first episodes, before anything is learned) and finished at 6.8%, close to the 5% floor. Large action spaces and noisy rewards make the bias worse, which is the regime of deep RL.

From Q-tables to deep Q-networks (DQN)#

A table needs one entry per state-action pair. For Atari frames or robot sensors that is hopeless, so the table is replaced by a function approximator , usually a neural network that outputs one value per action. The natural move is to turn the update into regression toward the TD target, minimising

where are the network weights being trained, is a replay buffer of past transitions, and is a periodically copied set of target-network weights. Done naively, this is unstable: function approximation, bootstrapping and off-policy data together form what Sutton and Barto call the deadly triad [7, Section 11.3], under which value estimates can diverge. The deep Q-network of Mnih and colleagues [5, 6] made it work with two devices.

Experience replay. Transitions go into a large buffer and training samples random minibatches from it, which breaks the correlation between consecutive frames and reuses each transition many times. The idea goes back to Lin [8], and it works only because Q-learning is off-policy: the buffer holds data from many older policies.

A target network. The TD target is computed with a frozen copy of the network, , refreshed every few thousand steps, so updates stop moving the target they regress toward.

The 2013 version learned seven Atari games from raw pixels with one architecture and set of hyperparameters [5]. The 2015 version, with the target network, was evaluated on 49 games and reached a level comparable to a professional human games tester across them [6]. Van Hasselt, Guez and Silver then showed that DQN overestimates values substantially on several games and carried double Q-learning across [9]: choose the next action with the online network and evaluate it with the target network,

where is the regression target. It is a one-line change that reduced overestimation and improved scores, and it is now the default in most DQN implementations.

Q-learning needs a max over actions, so it suits discrete action spaces. For continuous control and for language models, most systems use policy gradient and actor-critic methods, which keep a learned value function as the critic; that is the route to reinforcement learning for LLMs.

Q-learning in Python#

A complete tabular agent for the reference gridworld in plain numpy:

import numpy as np

LAYOUT = ["....G", ".#.X.", ".#...", "...#.", "S...."]
MOVES = [(-1, 0), (0, 1), (1, 0), (0, -1)]          # up, right, down, left
GAMMA, SLIP = 0.9, 0.2

def step(s, a, rng):
    """Slip to a perpendicular direction with prob SLIP; reward on entering s'."""
    u = rng.random()
    d = a if u < 1 - SLIP else (a + 1) % 4 if u < 1 - SLIP / 2 else (a + 3) % 4
    r, c = s[0] + MOVES[d][0], s[1] + MOVES[d][1]
    if not (0 <= r < 5 and 0 <= c < 5) or LAYOUT[r][c] == "#":
        r, c = s                                       # bumped: stay put
    cell = LAYOUT[r][c]
    return (r, c), {"G": 1.0, "X": -1.0}.get(cell, 0.0), cell in "GX"

def q_learning(episodes=2000, alpha=0.1, eps=0.1, seed=0):
    rng = np.random.default_rng(seed)
    Q = np.zeros((5, 5, 4))
    for _ in range(episodes):
        s = (4, 0)
        for _ in range(200):
            if rng.random() < eps:                     # explore
                a = int(rng.integers(4))
            else:                                      # exploit, random tie-break
                q = Q[s]
                a = int(rng.choice(np.flatnonzero(q == q.max())))
            s2, r, done = step(s, a, rng)
            target = r if done else r + GAMMA * Q[s2].max()
            Q[s][a] += alpha * (target - Q[s][a])      # the Q-learning update
            s = s2
            if done:
                break
    return Q

Q = q_learning()
print(np.round(Q.max(axis=2), 2))                     # learned state values
print(np.array(list("↑→↓←"))[Q.argmax(axis=2)])        # greedy policy

With seed 0 it learns the right-hand route, leaves the rarely visited top row at zero, and reports 0.89 below the goal, above the true 0.769: constant- noise after a run of lucky visits. A per-pair step size makes it settle.

Choosing α, ε and γ in practice#

SettingTypical starting pointWhat goes wrong if it is off
Learning rate α (tabular)0.1 to 0.5 in deterministic worlds; a decaying per-pair schedule such as in stochastic onesToo large: estimates jitter and the greedy policy flips between near-equal actions (Figure 2). Too small: value propagates slowly
Exploration εStart high (0.3 to 1) and decay toward 0.01 to 0.05Too low: whole regions never get updated. Too high: poor return during learning and, for SARSA, an overly cautious policy
Discount γSet by the task horizon; 0.9 to 0.99Too low: the agent ignores delayed rewards. Close to 1: slower and noisier learning, larger overestimation
Initial valuesZero, or optimistic values to drive early explorationOptimism with a decaying α can take long to wash out
Max over noisy valuesDouble Q-learning (tabular) or double DQNSystematic overestimation, preference for noisy actions (Figure 5)

Report the greedy policy's performance separately from the exploring agent's online return, over many seeds with intervals; the guide to evaluating RL agents covers how.

Failure modes#

Unvisited states. Convergence needs every pair visited infinitely often. In practice much of the table may be visited a handful of times, and a greedy policy that slips into those squares behaves arbitrarily. Tiny action gaps and overestimation, covered above, are the other two tabular traps.

Divergence with function approximation. Off-policy bootstrapping with a function approximator can diverge. Replay, target networks and conservative learning rates are guards, not guarantees.

The reward is the specification. Q-learning optimises exactly the reward you give it. If that reward has a loophole, a capable learner finds it; see reward hacking and reward shaping for how to change rewards without changing the optimal policy.

Frequently asked questions#

What is Q-learning in simple terms?

Q-learning is a way for an agent to learn, by trial and error, how good each action is in each situation. It keeps a score Q(s, a) for every state and action, and after each step moves that score toward the reward received plus the discounted score of the best next action. Acting on the highest scores then gives a good policy.

What does the Q in Q-learning stand for?

The Q refers to the action-value function Q(s, a), often read as the quality of taking action a in state s. It is the expected discounted return from taking that action and then acting optimally. The notation comes from Watkins's 1989 thesis, where the algorithm was introduced.

Why is Q-learning called off-policy?

Its update target uses the maximum Q-value in the next state, which is the value of the greedy policy, regardless of which action the agent actually takes next. So it learns about the greedy policy while behaving with an exploratory one. This is why it can learn from random play, replay buffers and logged data.

What is the difference between Q-learning and SARSA?

SARSA uses the Q-value of the next action the agent actually takes, so it learns the value of its exploring policy. Q-learning uses the best next action, so it learns the greedy policy. On cliff walking, in our seeded runs, Q-learning learned the shortest path along the edge but averaged -49.6 per episode while exploring; SARSA learned a safer path and averaged -27.5.

Does Q-learning always converge?

Tabular Q-learning converges to the optimal Q-values with probability 1 if every state-action pair is updated infinitely often and the step sizes decay so that their sum diverges while the sum of their squares stays finite. With a constant learning rate it only hovers near the optimum, and with neural networks there is no general guarantee.

What is a deep Q-network (DQN)?

A DQN replaces the Q-table with a neural network that outputs a value for each action, trained to regress onto the Q-learning target. Two additions make it stable: experience replay, which trains on random minibatches of stored transitions, and a target network, a periodically copied set of weights used to compute the targets.

Why does Q-learning overestimate values?

The target takes a maximum over noisy estimates, and the maximum of noisy unbiased estimates is biased upward. Bootstrapping then spreads that optimism backwards. Double Q-learning fixes this by selecting the best action with one estimate and evaluating it with another; double DQN does the same with the online and target networks.

How do I choose alpha and epsilon for Q-learning?

Start with epsilon high and decay it to a small value such as 0.01 to 0.05, so that every state gets explored early. Use a learning rate around 0.1 to 0.5 for deterministic problems and a decaying per-pair schedule for stochastic ones. Always compare settings over many seeds and evaluate the greedy policy separately.

Sources#

  1. C. J. C. H. Watkins. Learning from Delayed Rewards (PhD thesis, University of Cambridge), 1989. cs.rhul.ac.uk
  2. C. J. C. H. Watkins, P. Dayan. Q-learning, 1992. doi.org
  3. G. A. Rummery, M. Niranjan. On-line Q-learning using connectionist systems (Technical Report CUED/F-INFENG/TR 166), 1994. mi.eng.cam.ac.uk
  4. H. van Hasselt. Double Q-learning, 2010. papers.nips.cc
  5. V. Mnih, K. Kavukcuoglu, D. Silver, A. Graves, I. Antonoglou, D. Wierstra, M. Riedmiller. Playing Atari with Deep Reinforcement Learning, 2013. arxiv.org
  6. V. Mnih, K. Kavukcuoglu, D. Silver, A. A. Rusu, J. Veness, M. G. Bellemare, A. Graves, M. Riedmiller, A. K. Fidjeland, G. Ostrovski, et al.. Human-level control through deep reinforcement learning, 2015. doi.org
  7. R. S. Sutton, A. G. Barto. Reinforcement Learning: An Introduction (2nd ed.), 2018. incompleteideas.net
  8. L.-J. Lin. Self-improving reactive agents based on reinforcement learning, planning and teaching, 1992. doi.org
  9. H. van Hasselt, A. Guez, D. Silver. Deep Reinforcement Learning with Double Q-learning, 2015. arxiv.org

Know the number before you ship.

A 45-minute working session with an evaluation engineer. Bring one use case; leave with a threat model, a draft eval plan with sample sizes, and an honest read on where you stand.