Reinforcement Learning

Policy Gradients & REINFORCE

Directly optimizing policy parameters θ via gradient ascent on expected trajectory returns.

🔴 advanced5 min readrl
Policy Gradient methods directly parameterize a policy π_θ(a|s) and update parameters θ using gradient ascent on expected return J(θ). The Policy Gradient Theorem proves that ∇_θ J(θ) = E [ ∇_θ ln π_θ(a|s) · Q^π(s,a) ] without requiring knowledge of environment transition derivatives. The REINFORCE algorithm (Williams, 1992) uses Monte Carlo trajectory returns G_t as unbiased estimators of Q^π(s,a), but suffers from high variance.

The Policy Objective Function $J(\theta)$

We parameterize a policy $\pi_\theta(a \mid s)$ using neural network weights $\theta$.

The goal is to maximize total expected discounted return over trajectories $\tau = (s_0, a_0, r_0, s_1, a_1, r_1, \dots)$:

$$J(\theta) = \mathbb{E}{\tau \sim \pi\theta}[R(\tau)] = \int P(\tau; \theta) R(\tau) d\tau$$

Where trajectory probability $P(\tau; \theta) = P(s_0) \prod_{t=0}^T P(s_{t+1} \mid s_t, a_t) \pi_\theta(a_t \mid s_t)$.

The Log-Derivative Trick & Policy Gradient Theorem

Taking the gradient with respect to $\theta$:

$$\nabla_\theta J(\theta) = \nabla_\theta \int P(\tau; \theta) R(\tau) d\tau = \int \nabla_\theta P(\tau; \theta) R(\tau) d\tau$$

Applying Log-Derivative Trick ($\nabla_\theta P(\tau; \theta) = P(\tau; \theta) \nabla_\theta \ln P(\tau; \theta)$):

$$\nabla_\theta J(\theta) = \int P(\tau; \theta) \nabla_\theta \ln P(\tau; \theta) R(\tau) d\tau = \mathbb{E}{\tau \sim \pi\theta} \left[ \nabla_\theta \ln P(\tau; \theta) R(\tau) \right]$$

Notice that $\ln P(\tau; \theta) = \ln P(s_0) + \sum_{t=0}^T \ln P(s_{t+1} \mid s_t, a_t) + \sum_{t=0}^T \ln \pi_\theta(a_t \mid s_t)$.

Taking derivatives eliminates unknown environment terms $P(s_{t+1} \mid s_t, a_t)$ completely!

$$\mathbf{\nabla_\theta J(\theta) = \mathbb{E}{\pi\theta} \left[ \sum_{t=0}^T \nabla_\theta \ln \pi_\theta(a_t \mid s_t) \cdot G_t \right]}$$

                           Intuition of Policy Gradient Step
                             θ ← θ + α · ∇_θ ln π_θ(a_t | s_t) · G_t
  If Trajectory Return G_t > 0 ──► INCREASE probability of taking action a_t in state s_t!
  If Trajectory Return G_t < 0 ──► DECREASE probability of taking action a_t in state s_t!

The REINFORCE Algorithm (Monte Carlo Policy Gradient)

# REINFORCE Update Loop
for episode in range(num_episodes):
    trajectory = generate_trajectory(policy_net)  # Collect full episode
    for t in range(len(trajectory)):
        G_t = sum(gamma**k * r for k, r in enumerate(rewards[t:]))  # Monte Carlo Return
        loss = -log_prob(actions[t]) * G_t                          # Policy Gradient Loss
        loss.backward()
    optimizer.step()

Say this out loud

"Policy Gradient methods optimize policy parameters θ directly via gradient ascent on expected return J(θ). The Policy Gradient Theorem uses the log-derivative trick to eliminate unknown environment transition dynamics, deriving ∇_θ J(θ) = E [ ∇_θ ln π_θ(a|s) · G_t ]. REINFORCE uses Monte Carlo episode returns G_t as unbiased estimators, but suffers from high variance."

Follow-ups to expect

Check yourself

Question 1 of 3

Why is the Policy Gradient Theorem mathematically remarkable regarding environment transition dynamics P(s'|s,a)?

More in Reinforcement Learning

See all →
Value-Based vs Policy-Based Methods5 minMulti-Armed Bandits4 minProximal Policy Optimization5 min