Machine learning · 20 min

Reinforcement learning: learning from rewards

Watch an agent learn a gridworld from rewards alone: Q-values, the discount, and why exploring matters.

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 kk moves away counts γk\gamma^k times as much as one now. With γ=0.9\gamma = 0.9, ten points four moves away are worth 0.94⋅10≈6.60.9^4 \cdot 10 \approx 6.6 today.
Q-value
Q(s,a)Q(s, a): the total discounted reward the agent expects if it makes move aa in state ss and plays as well as it can afterwards.
policy
How the agent chooses moves. The greedy policy picks the move with the largest Q(s,a)Q(s, a); the arrows on the grid show it.
ε-greedy
Mostly greedy, but with chance ε\varepsilon 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.

Q(s,a)←Q(s,a)+α (r+γmax⁡a′Q(s′,a′)⏟target−Q(s,a))Q(s, a) \leftarrow Q(s, a) + \alpha\,\big(\underbrace{r + \gamma \max_{a'} Q(s', a')}_{\text{target}} - Q(s, a)\big)
In the first episode, every target is 0 + γ·0 until the agent stumbles onto the +10. Then watch the value creep back towards the start, one cell per visit.

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 γ\gamma 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 moves chart falls towards the best route of 9 moves, but stays noisy: random exploring moves still tip the agent into the cliff now and then.

The target uses max⁡a′Q(s′,a′)\max_{a'} Q(s', a'), 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, r+γmax⁡Q(s′,⋅)r + \gamma \max Q(s', \cdot), 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 made

06Try it

Near or far?

From the start, a +1 is two moves away and a +10 is four. Which is better depends entirely on γ\gamma: the +1 is worth γ⋅1\gamma \cdot 1 and the +10 is worth γ3⋅10\gamma^3 \cdot 10. Work out both for γ=0.9\gamma = 0.9 and for γ=0.25\gamma = 0.25, 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 γ\gamma does the preference flip? (Solve γ=10γ3\gamma = 10\gamma^3.)
  • No exploring: set epsilon = 0 on 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

  1. 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.

  2. Question 2 of 6

    With γ = 0.5, how much is a reward of 8 worth if it arrives three moves from now?

  3. Question 3 of 6

    Why does the agent sometimes make a random move instead of the greedy one?

  4. Question 4 of 6

    Value iteration and Q-learning both find Q*. What does Q-learning do without?

  5. 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?

  6. 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 Q(s,a)Q(s, a), the discounted reward to expect from a move, and acts greedily on it, with chance ε\varepsilon of exploring.
  • Each update moves Q(s,a)Q(s, a) a fraction α\alpha towards r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s', a'); 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.
  • γ\gamma 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).
Back to path