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.

a=argmaxaQ(s,a)a = \arg\max_{a'}Q(s,a')

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 θ\boldsymbol\theta and moves θ\boldsymbol\theta in a direction that increases the probability of actions that produced good returns.

πθ(as)\pi_\theta(a\mid s)

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.

πθ(as)=exphθ(s,a)bexphθ(s,b)\pi_\theta(a\mid s) = \frac{ \exp h_\theta(s,a) }{ \sum_b\exp h_\theta(s,b) }

hθ(s,a)h_\theta(s,a) 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.

AtN(μθ(St),σθ(St)2)A_t \sim \mathcal N \left( \mu_\theta(S_t), \sigma_\theta(S_t)^2 \right)

The policy can sample a continuous action directly, without listing the Q-values of every possible action or calculating an argmax\arg\max.

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.

J(θ)=Eτπθ[G0]J(\theta) = \mathbb E_{\tau\sim\pi_\theta} \left[ G_0 \right]

τ\tau is a trajectory generated by the policy.

τ=(S0,A0,R1,S1,A1,)\tau = (S_0,A_0,R_1,S_1,A_1,\ldots)

We want the direction in which J(θ)J(\theta) increases:

θJ(θ).\nabla_\theta J(\theta).

Make Actions That Produced Good Results More Likely

REINFORCE uses the following update.

θθ+αGtθlogπθ(AtSt)\theta \leftarrow \theta + \alpha G_t \nabla_\theta \log\pi_\theta(A_t\mid S_t)

We can read the equation as a sentence.

  • If Gt>0G_t>0, increase the log-probability of the selected action AtA_t.
  • If Gt<0G_t<0, decrease that action's log-probability.
  • The larger Gt|G_t| is, the larger the update becomes.
Increase the probability of actions that produced good returns and decrease the probability of actions that produced poor returns.

Suppose the only actions are LEFT and RIGHT, and currently

πθ(RIGHTs)=0.6.\pi_\theta(\text{RIGHT}\mid s)=0.6.

If we sample RIGHT and obtain a good return, we move in a direction that increases logπθ(RIGHTs)\log\pi_\theta(\text{RIGHT}\mid s). Because the probabilities must sum to 11, 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 pθ(x)p_\theta(x).

J(θ)=xpθ(x)R(x)J(\theta) = \sum_x p_\theta(x)R(x)

Differentiating gives

θJ(θ)=xθpθ(x)R(x).\nabla_\theta J(\theta) = \sum_x \nabla_\theta p_\theta(x)R(x).

We use the identity

θpθ(x)=pθ(x)θlogpθ(x),\nabla_\theta p_\theta(x) = p_\theta(x) \nabla_\theta\log p_\theta(x),

which follows from

θlogpθ(x)=θpθ(x)pθ(x).\nabla_\theta\log p_\theta(x) = \frac{ \nabla_\theta p_\theta(x) }{ p_\theta(x) }.

Substituting it produces

θJ(θ)=xpθ(x)θlogpθ(x)R(x),\nabla_\theta J(\theta) = \sum_x p_\theta(x) \nabla_\theta\log p_\theta(x) R(x),

or, as an expectation,

θJ(θ)=Expθ[R(x)θlogpθ(x)].\nabla_\theta J(\theta) = \mathbb E_{x\sim p_\theta} \left[ R(x)\nabla_\theta\log p_\theta(x) \right].

We no longer need to sum over every possible outcome. A sample xx from the current policy and its observed R(x)R(x) are enough to estimate the gradient.

The log-derivative trick lets us estimate the gradient of an expectation from a sample drawn from the probability distribution and its return.

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.

Gtenvironment feedback×θlogπθ(AtSt)policy gradient\underbrace{G_t}_{\text{environment feedback}} \times \underbrace{ \nabla_\theta\log\pi_\theta(A_t\mid S_t) }_{\text{policy gradient}}

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

θJ(θ)Eπθ[Qπθ(St,At)θlogπθ(AtSt)].\nabla_\theta J(\theta) \propto \mathbb E_{\pi_\theta} \left[ Q^{\pi_\theta}(S_t,A_t) \nabla_\theta \log\pi_\theta(A_t\mid S_t) \right].

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 GtG_t for the unknown Qπ(St,At)Q^\pi(S_t,A_t).

Qπ(St,At)GtQ^\pi(S_t,A_t) \approx G_t

Why Use GtG_t Instead of the Entire Episode Return?

An action at time tt cannot affect rewards that were received before it. We should therefore evaluate AtA_t using only the return from that point onward.

Gt=Rt+1+γRt+2+γ2Rt+3+G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2R_{t+3} +\cdots

Including earlier rewards adds noise unrelated to the action. Respecting this causal relationship reduces gradient variance.

The Complete REINFORCE Procedure

  1. Generate one episode using the current policy πθ\pi_\theta.
  2. Calculate return GtG_t at every time tt.
  3. Accumulate Gtθlogπθ(AtSt)G_t\nabla_\theta\log\pi_\theta(A_t\mid S_t).
  4. Update the parameters after the episode ends.
θθ+αt=0T1Gtθlogπθ(AtSt)\theta \leftarrow \theta + \alpha \sum_{t=0}^{T-1} G_t \nabla_\theta\log\pi_\theta(A_t\mid S_t)

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, GtG_t can change because of environment randomness and subsequent actions. One rollout may produce +10+10 and the next 3-3.

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.

  1. A policy πθ(as)\pi_\theta(a\mid s) directly represents a probability distribution over actions.
  2. The log-derivative trick lets us estimate a gradient from samples without differentiating the environment.
  3. REINFORCE uses Gtlogπθ(AtSt)G_t\nabla\log\pi_\theta(A_t\mid S_t).
  4. The sign of GtG_t determines whether to increase or decrease a probability; its magnitude determines the update strength.
  5. The method is simple, but the Monte Carlo return gives it high variance.

References