Gradient LabStart the lesson

Lessons › Chapter 13: Reinforcement learning

Q-learning

Q-learning keeps a q-value for every state and action: “how much total reward do I expect if I do this here?” Purple bars are “go left”, teal bars “go right”. Everything starts at 0.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

What is each action worth?

Leo tries to learn how valuable each kind of reminder is in each situation.

Concept

A table of guesses

Q-learning keeps a q-value for every state and action: “how much total reward do I expect if I do this here?” Purple bars are “go left”, teal bars “go right”. Everything starts at 0.

Build

Stumble onto the reward

Wandering around, the agent eventually steps right from square 4 into the goal and gets +10. That Q-value jumps up.

Key idea

Value flows backwards

The update uses the best Q-value of the next square. So the square before also learns “going right leads somewhere good”, and so on back along the corridor, a little weaker with each step.

Build

Follow the biggest bars

After many episodes, just pick the action with the highest Q in every square, and the path to the goal appears. Some random moves along the way (exploration) make sure the agent doesn’t miss better routes.

Real life

In real life: learning video games from pixels

A well-known 2015 research system used a deep-network version of Q-learning to learn dozens of classic Atari games straight from the screen pixels and the score, reaching human-level play on many of them.

Quick check: What does a Q-value estimate?

Q(state, action) estimates the expected (discounted) return from taking that action there.

Pseudocode

Q[state][action] = 0 for everything
repeat many episodes:
    pick an action (usually the best known, sometimes random)
    reward, next = environment.step(action)
    target = reward + 0.9 × best Q[next]
    Q[state][action] += 0.1 × (target − Q[state][action])
# then simply follow the highest Q in each state

Key terms

q-value
The expected total future reward for taking a particular action in a particular state.
exploration
Trying actions that might not look best yet, to discover better options.