Q-Learning Update
In one sentence: Q-learning lets an agent improve its guess about how good an action is, a little at a time, using the reward it got on its latest step.
Read it out loud: “Q of s, a becomes Q of s, a plus alpha times a bracket. Inside the bracket: the reward r, plus gamma times the largest Q of s prime and a prime, minus Q of s, a. The arrow means is replaced by. Q is the agent's guess of how good an action is. The letter s is the state, the situation, and a is the action. The letter r is the reward, s prime is the next state, and the small prime mark means next. Alpha, said AL-fuh, is the learning rate, and gamma, said GAM-uh, is the discount factor.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Agent, state, action and reward
An agent is a program that learns by trying actions and watching what happens. Its state, written , is the situation it is in now, such as one square of a game board. An action, written , is a choice it can make, such as moving right. After each action it receives a reward, written : a number of points that says how good the result was.
- The Q table and the value estimate
The agent keeps a table with one number for every pair of a state and an action. The number is its estimate of the total reward it can expect from taking action in state and playing well afterward. The table starts with guesses, often all zeros, and learning means improving those numbers.
- The Greek letters alpha and gamma
The Greek letter , called alpha and said AL-fuh, is the learning rate. It is a number between 0 and 1 that sets how big a step each update takes. The Greek letter , called gamma and said GAM-uh, is the discount factor, also between 0 and 1. It sets how much future reward counts compared with reward now.
- The arrow and the brackets
The arrow means replace the value on the left with the value on the right. So means add 1 to and store the result in . Square brackets, , group a calculation. Inside the brackets is the gap between a target and the old estimate, and the whole bracket is multiplied by before it is added.
- The maximum and the prime mark
The maximum, written , is the largest value in a list. In , the small label says to look over every action , and the answer is the biggest among them. A prime mark, such as or , means the next one. So is the next state, the place the agent ends up after its action.
What each symbol means
| Symbol | Meaning |
|---|---|
| The agent’s current estimate of how good it is to take action in state . It is the total future reward the agent expects, measured in reward points, with no unit. | |
| The state, the situation the agent is in right now, such as one square of a maze. It is a label for a situation, so it has no unit. | |
| The action taken in state , such as moving right. It is a choice from a list of possible moves, so it has no unit. | |
| The reward received right after taking action , measured in reward points. It can be zero, positive or negative. | |
| The next state, where the agent ends up after taking the action. The prime mark means next. It is a label, so it has no unit. | |
| The best value available from the next state: the largest among all the actions the agent could take from there. It is measured in reward points, with no unit. | |
| One of the possible actions in the next state . The prime mark marks it as an action taken from the next state, and the maximum looks over all of them. | |
| The learning rate, a number between 0 and 1 with no unit. It sets how big a step each update takes. A value of 0 changes nothing, and a value of 1 replaces the old estimate completely. | |
| The discount factor, a number between 0 and 1 with no unit. It sets how much a future reward counts compared with a reward now. A value of 0 counts only the next reward. | |
| The replace arrow. The value on the left is set to the result on the right. It is not an equals sign, because the two sides are not equal before the update happens. |
The formula piece by piece
The reward received for taking action in state , in points. It comes from the world the agent acts in, and it is the only new real information in this update.
The best value the agent expects from the next state. It looks at every action available there and takes the largest estimate. This assumes the agent plays well from then on.
The discounted future. Multiplying the best next value by shrinks it, so the future counts for less than the reward now. A discount of 0.9 counts each future point as 90 percent of a point.
The target. It adds the reward we got to the discounted best future value. This is the agent’s new, better guess of how good the action was, based on one real step.
The TD error, the gap between the target and the old estimate. A positive gap means the old guess was too low, and a negative gap means it was too high.
The step. The gap is multiplied by the learning rate, so the update moves the old estimate only a fraction of the way toward the target.
The new estimate equals the old estimate, plus the learning rate times the gap between the target and the old estimate.
The same update written as a weighted mix. The old estimate keeps the share , and the new target gets the share . Both forms give the same number.
What the formula is doing, and why
The agent keeps a table of numbers, one for each pair of state and action. At the start the numbers are guesses. Each time the agent acts, it gets a reward and lands in a new state, and it uses that one step to improve the number for the action it took. Over many visits, the table comes closer to the true value of each action.
Why a target with the best next value? After one step we know two things. One is the reward we got, which is real. The other is the best the agent can expect from the next state, which is the term. Adding them gives a target that uses one real reward, so it carries more real information than the old guess. The assumes the agent will act well from then on, which is why the update describes the best play.
Why move only part of the way? The gap between the target and the guess is the TD error, short for temporal difference error. One step of experience is noisy, so moving all the way to the target can make the estimate jump around. The learning rate moves the estimate only a fraction of the way. A small changes things slowly and smooths out noise, while a large reacts quickly but can jump around.
Why discount with ? Gamma decides how far ahead the agent looks. With , only the immediate reward counts. As nears 1, rewards far in the future count almost as much as rewards now. A discount below 1 also keeps the total finite when rewards are bounded, even if the task never ends.
When it applies. A table works when there are few enough states and actions to store one number for each pair, such as a small grid. Big problems use a neural network instead, which keeps the same kind of target. The update uses the best action in the next state even if the agent takes a different action there, which is why Q-learning is called off-policy.
An everyday picture
Think of rating a move in a board game. You know what the move gained right away, and you know the best you can expect from the position it leads to. Each time you play, you move your old rating a bit toward the sum of those two. Over many games the ratings settle near the true value of each move.
Worked example: One update in a grid world
The agent stands in state and takes action . Its current estimate is points. The action gives reward point and leads to state , where the best action has value points. Use the learning rate and the discount factor .
- Start with the old estimate. The agent currently thinks this action in this square is worth 2.0 points. We will adjust that guess using the one step it has taken.
- Discount the best value that follows. Multiply the best next value by the discount factor: . Multiplying by 0.9 counts each future point as 90 percent of a point now, so the future is worth a little less than its face value.
- Form the target. Add the reward we got: . The target is the agent’s new best guess of this action’s value, using the real reward and the discounted future.
- Find the gap. The TD error is the target minus the old estimate: . The gap is positive, which means the old estimate was too low.
- Take a step of the learning rate. Multiply the gap by : . With the agent moves halfway toward the target, so it does not trust one step completely.
- Add the step to the old estimate. The new estimate is points. Written as the update rule, .
- Check the halfway idea. The point halfway between 2.0 and 4.6 is . The two ways of looking at the update give the same new estimate.
Answer: The new estimate is . It moved halfway from 2.0 toward the target of 4.6, because . The next time the agent visits this pair, the estimate will move a little closer to the target again.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Using the action the agent actually takes next instead of the best one. That is a different method, called SARSA, and it gives different values.
- Setting for a task that never ends. Future rewards then add up without limit, and the estimates can grow forever.
- Adding the reward to the value of the wrong pair. The reward belongs to the step from to , so it goes into the update for .
- Expecting one update to settle the value. Each update is a small nudge controlled by , and the values improve over many visits.
Where you will meet it
- Teaching agents to move through grids and mazes, a standard example in reinforcement learning courses and textbooks.
- Deep Q-networks, which keep the same kind of target but learn the Q values with a neural network instead of a table.
- Robot control and scheduling problems, where an agent learns which action pays off most over a long run.
- Game-playing agents in research, which estimate the value of each move from the reward and the best next move.
Questions people ask
What does the learning rate do?
It sets how big a step each update takes. With , nothing is learned. With , the old estimate is replaced by the target completely.
Why use the max over the next actions?
The estimate should describe the best play from here on. Using the largest next value gives that, so the table can learn the best policy even while the agent sometimes explores at random. A policy is the agent’s rule for choosing actions.
What happens when an episode ends?
An episode is one run from a start to an end, such as one game. When it ends, there is no next state to value, so the future term is set to zero. The update then uses the reward alone: .
Who first described Q-learning?
Chris Watkins introduced it in 1989, in his thesis Learning from Delayed Rewards.
Related formulas
- Discounted Return: The discounted return is the total reward an agent can expect from now on, where rewards that arrive later count for a little less.
- Softmax Temperature: Temperature is a dial that makes a model’s choices more focused when it is low and more varied when it is high.
- Gradient Descent: Gradient descent nudges a model’s settings a small step in the direction that makes the error smaller, then repeats that step again and again until the error stops falling.