Part XVI — Reinforcement Learning: Bandits, MDPs, Policy Gradients & PPO · Chapter 5

Q-learning & temporal-difference updates

Hook

Last chapter's value function came from a policy you were simply handed — "always move right" was given, not discovered. Q-learning is the algorithm that discovers a good policy on its own, from nothing but a table of zeros and a sequence of moves.

Intuition
S0
S1
S2
S3
step 1/6: at S0, took "right" → S1, reward -1

Instead of one value per state, Q-learning tracks one value per state-action pairQ(s,a)Q(s,a), "how good is it to take action aa from state ss?" Step through this fixed sequence of moves and watch individual entries in that table update, one at a time, from a starting table of all zeros.

Formalize

After taking action aa from state ss, landing in ss' with reward rr, the Q-learning update nudges Q(s,a)Q(s,a) toward a target built from the best action available next:

Q(s,a)Q(s,a)+α(r+γmaxaQ(s,a)Q(s,a))Q(s,a) \leftarrow Q(s,a) + \alpha\Big(r + \gamma \max_{a'} Q(s',a') - Q(s,a)\Big)
  • Q(s,a)Q(s,a) — the learned value of taking action aa from state ss: how good that state-action pair looks so far.
  • rr — the reward received for that step.
  • ss' — the state landed in after taking the action.
  • aa' — a candidate next action to take from that new state.
  • γ\gamma — the discount factor applied to future value.
  • α\alpha — the learning rate: how much of the gap gets corrected on any one update.
  1. The parenthesized term is a TD error

    It's a temporal-difference error — the gap between what Q(s,a)Q(s,a) currently predicts and a slightly better estimate built from one real step of experience plus the table's own best guess about what happens after.

  2. Alpha is an ordinary learning rate

    α\alpha controls how much of that gap gets corrected on any one update — exactly a learning rate, doing exactly what a learning rate does in every other optimizer in this course.

Play

After all six scripted steps, Q(2,right)Q(2,\text{right}) already shows 55 — a real signal that reaching the goal from state 2 is good. Everything one state further back is still negative: the reward hasn't had enough steps yet to propagate that far through the table. Run more steps, and it eventually would.

Worked example

With α=0.5\alpha=0.5, γ=0.9\gamma=0.9, starting from an all-zero table:

  1. An update with no signal yet

    From S0S_0, taking "right" lands on S1S_1 with reward 1-1, and maxaQ(S1,a)=0\max_{a'}Q(S_1,a')=0 (nothing learned there yet). Q(S0,right)0+0.5(1+0.9(0)0)=0.5Q(S_0,\text{right}) \leftarrow 0 + 0.5(-1+0.9(0)-0) = -0.5.

  2. The moment the goal is reached

    Three steps later, from S2S_2, "right" reaches the goal directly: reward +10+10, and the goal has no further actions, so maxaQ(S3,a)=0\max_{a'}Q(S_3,a')=0. Q(S2,right)0+0.5(10+0.9(0)0)=5Q(S_2,\text{right}) \leftarrow 0 + 0.5(10+0.9(0)-0) = 5 — a single update jumps straight to a strongly positive value.

  3. One step back, still catching up

    Q(S1,right)Q(S_1,\text{right}) was updated twice:

    1. First, taking "right" to S2S_2 with reward 1-1 and maxaQ(S2,a)=0\max_{a'}Q(S_2,a')=0 (nothing learned there yet): Q(S1,right)0+0.5(1+0.9(0)0)=0.5Q(S_1,\text{right}) \leftarrow 0 + 0.5(-1+0.9(0)-0) = -0.5
    2. Later, the same update runs again, but using maxaQ(S2,a)\max_{a'}Q(S_2,a') which was still 00 at that moment (the update at S2S_2 hadn't happened yet): Q(S1,right)0.5+0.5(1+0.9(0)(0.5))=0.75Q(S_1,\text{right}) \leftarrow -0.5 + 0.5(-1+0.9(0)-(-0.5)) = -0.75

    Further from the goal's value, not closer, purely because of the order the updates happened in.

Checkpoint

Step forward until Q(2, right) turns positive — the first sign the goal's reward has entered the table.

S0
S1
S2
S3
step 1/6
Step forward to try it
Summary
Q(s,a)Q(s,a)+α(r+γmaxaQ(s,a)Q(s,a))Q(s,a) \leftarrow Q(s,a) + \alpha\Big(r + \gamma \max_{a'} Q(s',a') - Q(s,a)\Big)

Q-learning discovers a policy by discovering values first — once every Q(s,a)Q(s,a) has converged, the best policy is just "take whichever action has the highest QQ from here." No policy was ever specified up front; it falls out of the table once enough experience has passed through the same update rule, over and over. The next chapter takes a completely different approach to the same problem: instead of learning values and deriving a policy from them, learn the policy directly.