Q-learning
An agent in a five-cell corridor learns which way to walk, one number at a time. Seven steps through the Q-table, the update rule with real numbers, discounting and exploration.
In 30 seconds
Q-learning teaches a program, the agent, to act by trial and error. For every situation and every possible move it keeps one number, the Q-value: the total reward it expects if it makes that move and plays well afterwards.
After each move it nudges that number toward what it just saw: the reward received, plus the best value available from where it landed, with later rewards counted a little less.
Mostly it takes the move with the highest number, and now and then a random one, so that it keeps discovering.
Q-learning, step by step
In this corridor a move always has the same result. Q-learning also handles moves with random outcomes; the values then settle on averages. Real DQNs add tricks such as replaying stored experience.
- Step 1: An agent, moves and rewards. An agent stands in a corridor of five cells. In each free cell (its state) it chooses an action, a step left or right. Reaching the goal pays +10, falling in the pit costs 10, any other step pays 0. Nobody tells it which way is good. It must find out by trying, over many runs called episodes.
- Step 2: The Q-value. The agent keeps a table with one number for each state and action: the Q-value. Q(s2, →) answers the question: if I step right from s2 and act as well as I can afterwards, how much reward will I collect in total, with later reward counted slightly less? Nothing is known yet, so all six start at zero.
- Step 3: The update rule. After every step the agent corrects one number. The target is what the step suggests the value should be: the reward r it just got, plus γ times the best Q-value in the state s′ it landed in. The estimate moves a fraction α of the way from its old value toward that target. Here α = 0.5 and γ = 0.9.
- Step 4: One update, with numbers. In the first episode the agent wanders from s2 to s3 and then steps right into the goal: reward 10. The episode is over, so nothing more can follow and the target is just 10. The old value was 0, so Q(s3, →) moves halfway, to 5. The earlier step from s2 taught nothing, because every number it could see was still zero.
- Step 5: The discount factor. Next episode, the agent steps right from s2 into s3. The reward is 0, but s3 is now known to be promising: its best Q-value is 5. The discount factor γ = 0.9 counts that future value at 90%, giving a target of 4.5, and Q(s2, →) moves halfway to 2.25. Step by step, value spreads backwards from the goal.
- Step 6: Explore or exploit. How should the agent choose its moves while learning? Always taking the highest Q-value (exploiting) would lock in early luck and leave other moves untested. Epsilon-greedy adds a little exploring: with probability ε, here 0.1, it picks an action at random; otherwise it takes the best-known one.
- Step 7: A learned table, and DQN. After many episodes the numbers stop changing: 10, 9 and 8.1 for stepping right, each 0.9 times the one after it. Taking the largest value in every cell is the learned behaviour. When there are far too many states for a table, a neural network estimates the Q-values instead: a deep Q-network (DQN).
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?