01Why it matters
Learning without an answer key
Every model so far learned from examples with the right answer attached: this point is class A, this house sold for that much. Nobody can label every move of a game or every turn of a robot's wheel, though. What there is instead is a score that arrives later: a win, a crash, a reward.
Reinforcement learning learns from that score alone. The agent acts, sees what happens, and slowly works out which moves lead to reward, even when the reward comes many moves after the decision that earned it. This lesson builds the classic method, Q-learning, on a few small grids.
02Key parts
The pieces you will see
- state and action
- Where the agent is (a cell of the grid) and what it can do there (move up, right, down or left).
- reward
- A number the world hands back after each move: +10 for the goal, −1 per step, −100 for the cliff.
- discount γ
- A reward moves away counts times as much as one now. With , ten points four moves away are worth today.
- Q-value
- : the total discounted reward the agent expects if it makes move in state and plays as well as it can afterwards.
- policy
- How the agent chooses moves. The greedy policy picks the move with the largest ; the arrows on the grid show it.
- ε-greedy
- Mostly greedy, but with chance a random move instead, so the agent keeps trying things it doesn't yet rate.
03See it work
Nothing, nothing, then a reward
The agent starts with every Q-value at 0 and wanders. After every move it nudges one entry of its table towards a target: the reward it just got, plus the discounted value of the best move from where it landed.
Only the move that entered the goal learns anything in the first episode. On the next visit to the cell before it, the target includes that cell's new value, so the reward spreads one step further back each time, shrinking by per step. The wash fades with distance from the goal because of that discount.
04See it work
Hugging the cliff edge
Here every move costs 1 and the cliff costs 100. The fastest route runs right along the edge.
The target uses , the value of acting greedily next, whatever the agent actually does next. So Q-learning learns how good the edge is for an agent that never slips, even while its own ε-greedy wandering keeps slipping. That is called learning off-policy. A method that learns the value of the policy it really follows (SARSA) would pay for the slips and prefer a row further from the edge.
05Predict
Your turn: up, down, or unchanged?
Before each update, decide whether that Q-value will rise, fall or stay put. Compare the target, , with the value it has now. When the agent exploits, predict which move it makes from the Q-values listed.
Make 6 predictions: step forward and answer each question before the next step plays.
0 of 6 made06Try it
Near or far?
From the start, a +1 is two moves away and a +10 is four. Which is better depends entirely on : the +1 is worth and the +10 is worth . Work out both for and for , then run each and see which way the arrows from S end up pointing.
Then use the input box to try:
- The crossover: at which does the preference flip? (Solve .)
- No exploring: set
epsilon = 0on the near-or-far world. The first goal the agent stumbles onto is the one it keeps going back to, so the other one may never be found. - Too few episodes: with
episodes = 10, the arrows from S may still loop.
07Check yourself
Check yourself
Question 1 of 6
The agent moves from s to s′ and gets r = 0. Every Q-value at s′ is 0, and Q(s, a) is 0. What does the update do?
Your answer comes back in Review in a few days, so it sticks.
Question 2 of 6
With γ = 0.5, how much is a reward of 8 worth if it arrives three moves from now?
Question 3 of 6
Why does the agent sometimes make a random move instead of the greedy one?
Question 4 of 6
Value iteration and Q-learning both find Q*. What does Q-learning do without?
Question 5 of 6
A move gives r = 0 but lands next to the goal, where the best Q-value is 10. With γ = 0.9, what value does Q(s, a) head towards?
Question 6 of 6
An agent picks random moves most of the time. What does the Q-learning update learn about?
08Explain it back
Say it in your own words
How does Q-learning work out which moves are good when the reward only arrives at the end?
09Recap
What to remember
- The agent learns , the discounted reward to expect from a move, and acts greedily on it, with chance of exploring.
- Each update moves a fraction towards ; rewards spread backwards one step per visit.
- The fixed point is the Bellman equation, the same one dynamic programming solves when the rules are known.
- trades near rewards against far ones; ε trades using what is known against finding out more.
- Next: replace the table with a neural network and the same update learns Atari games (DQN).