ai theory
Reinforcement Learning Basics (11) — What Changes When a Table Becomes a Function?
Junyoung Park · 2024-05-24 · 8 min
Introduction
So far, we have stored one value for every state.
When there are only a few dozen states, this is the most intuitive approach. But if a single camera image is a state, we cannot build a table containing every possible combination of pixels. The same problem arises when the state is continuous, as with the joint angles of a robot. The robot may never encounter the exact same pose twice.
The necessary shift is from “memorize the answer for each state” to “calculate the answer from the characteristics of the state.”
contains parameters shared across many states. This article begins with the simplest form of value function approximation, a linear function, and then works through how Monte Carlo and TD targets become learning targets for that function.
Where a Table Reaches Its Limit
A tabular method changes only the entry for the state that was visited.
may be a Monte Carlo return or a TD target. The important point is that the entry for is independent of every other state's entry.
This independence is convenient in a small problem, but it creates two difficulties.
- If there are too many states, we cannot store every entry.
- Knowledge learned in similar states cannot be shared between them.
For example, suppose the lane position in an autonomous-driving state changes from to . These states describe almost the same situation, but a table gives them entirely different addresses. Even if the agent has learned a great deal at , it sees as a new state.
Function approximation lets similar states share representations and parameters.
An Approximation, Not the Exact Value
Let be the true state value under policy . Our function approximates it.
The hat means “an estimate calculated with the current parameters.”
- : the input state
- : the parameters to learn
- : the value predicted by the function
- : the true expected return we want to know but cannot observe directly
The structure resembles supervised learning. We feed in , obtain a prediction, and update to reduce the difference from a target. The difference is that no one supplies a correct label in reinforcement learning. A return or bootstrap target constructed from experience acts as a temporary answer.
Features Describe a State with Numbers
Before we feed a state into a function, we need to represent it as a numerical vector.
For an elevator state, we might define features such as
- the normalized current floor,
- whether an upward call button is active,
- whether a downward call button is active, and
- whether the door is open.
Features resemble a list of questions that describe the state. A neural network also learns the features themselves. With a linear function, however, people often define the input features by hand.
A Linear Value Function
The simplest approximation is a weighted sum of the features.
For example, if
then
expresses the direction and degree to which its feature affects the value. If repeated experience shows that the value rises when the second feature grows, for example, increases.
Moving in a Direction That Reduces Error
Suppose we have a target . The squared error for one sample is
The is simply a convenience that cancels the leading when we differentiate.
Updating the parameters with gradient descent gives
It is easier to read the equation as three pieces.
- : the step size that determines how far to move
- : the error that says how wrong the current prediction is
- : the direction that says which parameter will change the prediction
For a linear function, the gradient is the feature vector itself.
The update therefore becomes even simpler.
Weights connected to features with larger values move farther.
Learning One State Changes Other States Too
In a table, updating leaves the value of unchanged. With function approximation, every state shares the same , so an update using also changes the predictions for and .
This is both the advantage and the risk of function approximation.
- When useful features are shared, the agent can learn several similar states from relatively little experience.
- When it generalizes incorrectly, an error in one state can damage predictions for unrelated states as well.
A table learns slowly, but an error in one entry does not spread to another. A function generalizes quickly, but every update has a wider range of influence.
Using a Monte Carlo Target
If the episode has ended and we can calculate the actual return , then we set
The update becomes
Because the target does not contain the current value function, this looks most like familiar supervised regression. The usual properties of Monte Carlo remain: we must wait until the episode ends, and the return has high variance.
Using a TD Target
TD(0) bootstraps from the estimate one step ahead.
The TD error is
and the update is
Here, the target is also constructed with the same parameters . During the update, however, we do not differentiate through the target side. We change only the prediction for the current state, . This is why the method is called semi-gradient TD.
For now, it is enough to understand the process as follows: “temporarily fix the next state's estimate as the answer, and move only the current prediction toward it.”
Function Approximation Is Not Always Stable
Training can become unstable when function approximation, bootstrapping, and off-policy learning appear together. These three elements are commonly called the deadly triad.
- Function approximation: several states share parameters.
- Bootstrapping: the target contains a current estimate.
- Off-policy: the policy that produced the experience differs from the policy being learned.
An overestimate can spread through shared parameters. If that incorrect estimate is then used to construct another target, the error may grow. DQN, the subject of the next article, uses experience replay and a target network to mitigate this problem.
What to Remember
Value function approximation calculates values with a function that has shared parameters instead of storing a separate value for every state. This allows the estimate to generalize to states it has never seen.
- approximates .
- A linear function is written as .
- We can read an update as the product of a step size, prediction error, and gradient.
- Monte Carlo uses as its target, while TD uses .
- Shared parameters create generalization, but they can also spread a bad update across many states.
The next article approximates with a neural network and introduces DQN, which produces action values directly from pixel inputs.