MediumReinforcement Learning

Policy Gradient Loss

Reinforcement Learning

Medium

Problem

The REINFORCE algorithm uses the policy gradient theorem to directly optimize a stochastic policy. The loss is constructed so that gradient descent increases the probability of actions that led to higher-than-average returns and decreases the probability of actions that led to lower-than-average returns.

Given the log-probabilities of the actions taken, the rewards at each timestep, and a discount factor gamma, compute the policy gradient loss with a mean-return baseline.

Algorithm

  1. Compute discounted returns backward:

G_{T-1} = r_{T-1}

G_t = r_t + \gamma G_{t+1}

  1. Subtract the mean return as a baseline to reduce variance:

\bar{G} = \frac{1}{T} \sum_{t=0}^{T-1} G_t

A_t = G_t - \bar{G}

  1. Compute the loss (negative because we want gradient ascent on expected return):

L = -\frac{1}{T} \sum_{t=0}^{T-1} \log \pi(a_t | s_t) \cdot A_t

Return the policy-gradient loss as a float.

Theory

In reinforcement learning:

The goal: learn a policy \pi(a|s) that maximizes cumulative reward.


What Is a Policy?

A policy maps states to actions. Two types:

Deterministic policy:

a = \pi(s) Given state s, always take action a.

Stochastic policy:

\pi(a|s) = P(a|s) Given state s, sample action a from a probability distribution.

Policy gradient methods use stochastic policies parameterized by neural networks.


The Objective

We want to maximize expected cumulative reward:

J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}[R(\tau)]

Where:

The challenge: how do we take gradients through sampling actions?


The Policy Gradient Theorem

The gradient of the objective is:

\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}\left[\sum_{t=0}^{T} \nabla_\theta \log \pi_\theta(a_t|s_t) \cdot R(\tau)\right]

This is remarkable: we can estimate the gradient by:

  1. Sampling trajectories using the current policy
  2. Computing log probabilities of the actions taken
  3. Weighting by the total reward

No need to differentiate through the environment!


REINFORCE Algorithm

The simplest policy gradient method (Williams, 1992):

For each episode:

  1. Run the policy, collect trajectory: s_0, a_0, r_0, s_1, a_1, r_1, ...
  2. Compute return: R = \sum_t r_t
  3. Compute loss: L = -\sum_t \log \pi_\theta(a_t|s_t) \cdot R
  4. Update: \theta \leftarrow \theta - \alpha \nabla_\theta L

The loss is the negative of the policy gradient objective (we minimize loss, which maximizes reward).


Understanding the Loss

L = -\sum_t \log \pi_\theta(a_t|s_t) \cdot R

Breaking this down:

\log \pi_\theta(a_t|s_t): How likely was this action under our policy?

R: How good was the outcome?

The product: If R > 0 (good outcome), increase probability of actions taken. If R < 0 (bad outcome), decrease probability of actions taken.

The negative sign: Because we minimize loss but want to maximize reward.


The Credit Assignment Problem

Basic REINFORCE uses total episode reward for all actions:

L = -\sum_t \log \pi_\theta(a_t|s_t) \cdot R_{\text{total}}

Problem: early actions get credit/blame for late rewards, even if they were unrelated.

Solution: reward-to-go

Use only future rewards for each action:

L = -\sum_t \log \pi_\theta(a_t|s_t) \cdot R_t

Where R_t = \sum_{t'=t}^{T} r_{t'} is the sum of rewards from time t onward.

This makes sense: action at time t can only affect future rewards, not past.


Variance Reduction: Baselines

Policy gradients have high variance. A common fix is subtracting a baseline:

L = -\sum_t \log \pi_\theta(a_t|s_t) \cdot (R_t - b(s_t))

Where b(s_t) is a baseline (any function of state, not action).

Why it works:

Common baselines:


Advantage Function

The advantage measures how much better an action is compared to average:

A(s, a) = Q(s, a) - V(s)

Where:

Using advantage as the weight:

L = -\sum_t \log \pi_\theta(a_t|s_t) \cdot A(s_t, a_t)

This is the foundation of Actor-Critic methods and PPO.


Numerical Example

Episode with 3 timesteps:

Timestep 0: state = s0, action = a0, log_prob = -0.5, reward = 1 Timestep 1: state = s1, action = a1, log_prob = -1.0, reward = 0 Timestep 2: state = s2, action = a2, log_prob = -0.3, reward = 10

Reward-to-go:

Loss (no baseline):

L = -[(-0.5)(11) + (-1.0)(10) + (-0.3)(10)]

L = -[5.5 + 10 + 3] = -18.5

Since all returns are positive, all actions get reinforced.


The Gradient

\nabla_\theta L = -\sum_t \nabla_\theta \log \pi_\theta(a_t|s_t) \cdot R_t

For a neural network policy with softmax output:

The gradient:


Entropy Regularization

Adding entropy bonus to encourage exploration:

L = -\sum_t [\log \pi_\theta(a_t|s_t) \cdot A_t + \beta H(\pi(\cdot|s_t))]

Where H is entropy and \beta is a small coefficient (e.g., 0.01).

Why entropy helps:


Common Issues and Solutions

High variance:

Sample inefficiency:

Unstable updates:


Where Policy Gradient Is Used

Modern methods like PPO, SAC, and TD3 all build on the policy gradient foundation with various improvements for stability and sample efficiency.

Examples

Example 1

Input
log_probs = [-1, -2, -0.5], rewards = [1, 1, 1], gamma = 1
Output
0.166667
Explanation
Returns [3, 2, 1] give centered advantages [1, 0, -1].

Example 2

Input
log_probs = [-1, -0.5], rewards = [0, 10], gamma = 0
Output
-1.25

Hints

  1. Build discounted returns from right to left before computing their mean.
  2. Multiply each log probability by its centered return and negate the average.

Requirements

Constraints

Starter Code

def policy_gradient_loss(log_probs: list, rewards: list, gamma: float) -> float:
    """
    Returns the REINFORCE loss with a mean-return baseline.
    """
    # Write code here
    pass

Test Cases

CaseMatches
cleanpublic
gamma0public