Machine learningLoading progress…

Q-learning

Distinguish sequential decision-making, temporal prediction, recommendation and causal estimation.

Learning values from experience instead of from a model

Value iteration needed the rules of the world. Q-learning works without them. It stores a table Q[state][action]: the return the agent expects if it takes that action now and then behaves well. It fills the table in by trying things and nudging each entry toward what actually happened.

target = r + gamma * max over a' of Q[next state][a']      (the move is not terminal)
target = r                                                 (the move ended the episode)

Q[s][a] = Q[s][a] + alpha * (target - Q[s][a])

Worked example: current Q is 2, the reward is 1, gamma is 0.9 and the best next-state value is 4. The target is 1 + 0.9 × 4 = 4.6. With alpha = 0.5 the new Q is 2 + 0.5 × (4.6 − 2) = 3.3. The estimate moved half-way toward the target.

Predict first. The move ends the episode (terminal). Current Q is 0, reward is 1, alpha is 0.5. What is the new Q?

Watch the table fill in

In a small grid world every reward starts at zero, so the first updates change nothing. The first real learning happens when the agent stumbles into the goal or the pit. That information then spreads backward one step per visit, because each update uses the next state's current value.

00000000+10000-100000000S00000000
alpha (learning rate)0.5epsilon (exploration)0.3gamma (discount)0.9
Press Step. Each step picks a move (random with probability epsilon, otherwise the best Q, ties at random), then updates one table entry.
steps 0 · episodes 0 · reached goal 0x · fell in pit 0x
green arrow = single best move; dot = tie or nothing learned yet
Step through single updates or run whole episodes. The arithmetic of the latest update is written out, and the greedy arrows appear as values form.

Two details matter. The agent must still explore: with epsilon-greedy it sometimes acts at random, otherwise it can repeat a mediocre route forever. And the target uses the best next action, not the one the agent will actually take, which is why Q-learning is called off-policy.

Chapter context

These topics change the structure of the learning problem. Their first lesson is to identify what a prediction or action is allowed to depend on and how feedback is produced. Each section is a bridge to a larger specialist subject.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

Gt=∑k≥0γkrt+k+1,Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)−Q(s,a)].G_t=\sum_{k\ge0}\gamma^k r_{t+k+1},\quad Q(s,a)\leftarrow Q(s,a)+\alpha[r+\gamma\max_{a'}Q(s',a')-Q(s,a)].

For a terminal transition the continuation term is zero. A policy-gradient estimator uses a term proportional to ∇log⁡πθ(a∣s)\nabla\log\pi_\theta(a\mid s) times a return or advantage estimate.

Worked example

For a nonterminal transition, current Q=2, reward=1, discount=0.9 and best next Q=4 give target 1+0.9×4=4.6. With alpha=0.5, updated Q is 2+0.5×(4.6−2)=3.3. Reusing the same target for a terminal transition would be incorrect.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
q, reward, discount, next_q, rate = 2, 1, .9, 4, .5
target = reward + discount*next_q
assert isclose(target, 4.6)
assert isclose(q+rate*(target-q), 3.3)
terminal_target = reward
assert q+rate*(terminal_target-q) == 1.5

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Prediction is not intervention. Randomly splitting overlapping time windows can leak future information. An environment reward is a modeling choice and may omit important real-world consequences.