ai theory
Reinforcement Learning Basics (13) — What It Means to Differentiate a Policy Directly
Junyoung Park · 2024-06-07 · 6 min
Introduction
DQN predicts a Q-value for every action and selects the largest one.
The policy is therefore hidden behind the value function: learn the Q-values well, and the policy follows.
Policy Gradient reverses that order. It explicitly represents a policy parameterized by and moves in a direction that increases the probability of actions that produced good returns.
The first question is usually, “If the environment is not differentiable, how can we differentiate action probabilities?” This article introduces the log-derivative trick that answers that question and REINFORCE, the most basic Policy Gradient algorithm.
Why Represent the Policy Directly?
For discrete actions, we can use a Softmax policy.
is a score representing the preference for an action.
For continuous actions, we can use a Gaussian policy that outputs a mean and standard deviation.
The policy can sample a continuous action directly, without listing the Q-values of every possible action or calculating an .
Learning the policy directly has several advantages.
- It handles continuous actions naturally.
- It can represent problems that require stochastic policies.
- It changes action probabilities smoothly.
Its disadvantages are that the gradient can have higher variance than in value-based methods, and on-policy experience is difficult to reuse for long.
What Are We Maximizing?
Let the objective be the expected return of the policy.
is a trajectory generated by the policy.
We want the direction in which increases:
Make Actions That Produced Good Results More Likely
REINFORCE uses the following update.
We can read the equation as a sentence.
- If , increase the log-probability of the selected action .
- If , decrease that action's log-probability.
- The larger is, the larger the update becomes.
Suppose the only actions are LEFT and RIGHT, and currently
If we sample RIGHT and obtain a good return, we move in a direction that increases . Because the probabilities must sum to , increasing RIGHT's probability decreases LEFT's.
The important point is that we do not train only the action with the largest probability. We evaluate the action that was actually sampled according to its outcome.
Why Use the Logarithm?
Consider an expectation under a probability .
Differentiating gives
We use the identity
which follows from
Substituting it produces
or, as an expectation,
We no longer need to sum over every possible outcome. A sample from the current policy and its observed are enough to estimate the gradient.
Why the Environment Does Not Need to Be Differentiable
The environment's response after an action may be impossible to differentiate. Collisions, game rules, and human reactions are often discontinuous.
Policy Gradient does not backpropagate through the environment. It uses the return from the environment as a weight and differentiates only the log-probability of the differentiable policy.
The environment reports the outcome; the policy changes the probability of the choice that produced it.
The Policy Gradient Theorem
In an MDP that revisits states, the Policy Gradient Theorem gives
The key result is that we can calculate the gradient from trajectories sampled under the current policy without directly differentiating the state-visitation probabilities.
REINFORCE substitutes the observed return for the unknown .
Why Use Instead of the Entire Episode Return?
An action at time cannot affect rewards that were received before it. We should therefore evaluate using only the return from that point onward.
Including earlier rewards adds noise unrelated to the action. Respecting this causal relationship reduces gradient variance.
The Complete REINFORCE Procedure
- Generate one episode using the current policy .
- Calculate return at every time .
- Accumulate .
- Update the parameters after the episode ends.
The implementation is simple, but when episode returns fluctuate widely, so do the gradients.
The High Variance of REINFORCE
Even after choosing the same action in the same state, can change because of environment randomness and subsequent actions. One rollout may produce and the next .
REINFORCE multiplies by this return directly, so its updates have high variance. We may need to average many trajectories before a stable direction emerges.
The next article subtracts the value normally expected in the state as a baseline and introduces a Critic that learns that baseline. This is the starting point of Actor-Critic.
What to Remember
Policy Gradient directly updates policy parameters in a direction that increases the log-probability of actions that produced good returns.
- A policy directly represents a probability distribution over actions.
- The log-derivative trick lets us estimate a gradient from samples without differentiating the environment.
- REINFORCE uses .
- The sign of determines whether to increase or decrease a probability; its magnitude determines the update strength.
- The method is simple, but the Monte Carlo return gives it high variance.