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.
Instead of one value per state, Q-learning tracks one value per state-action pair — , "how good is it to take action from state ?" 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.
After taking action from state , landing in with reward , the Q-learning update nudges toward a target built from the best action available next:
- — the learned value of taking action from state : how good that state-action pair looks so far.
- — the reward received for that step.
- — the state landed in after taking the action.
- — a candidate next action to take from that new state.
- — the discount factor applied to future value.
- — the learning rate: how much of the gap gets corrected on any one update.
- The parenthesized term is a TD error
It's a temporal-difference error — the gap between what 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.
- Alpha is an ordinary learning rate
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.
After all six scripted steps, already shows — 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.
With , , starting from an all-zero table:
- An update with no signal yet
From , taking "right" lands on with reward , and (nothing learned there yet). .
- The moment the goal is reached
Three steps later, from , "right" reaches the goal directly: reward , and the goal has no further actions, so . — a single update jumps straight to a strongly positive value.
- One step back, still catching up
was updated twice:
- First, taking "right" to with reward and (nothing learned there yet):
- Later, the same update runs again, but using which was still at that moment (the update at hadn't happened yet):
Further from the goal's value, not closer, purely because of the order the updates happened in.
Step forward until Q(2, right) turns positive — the first sign the goal's reward has entered the table.
Q-learning discovers a policy by discovering values first — once every has converged, the best policy is just "take whichever action has the highest 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.