Policy Gradients & REINFORCE

Do more of what led to reward, less of what did not. That is the whole idea.

🔴 advanced5 min readrl
Most reinforcement learning methods score actions and then pick the best one. Policy gradient methods skip that and directly adjust the policy, the rule for choosing actions, to make good outcomes more likely. REINFORCE, the simplest version, plays a whole episode, then raises the probability of every action it took in proportion to how much reward followed. It is easy to understand and needs no model of the world, but its updates are very noisy, so it learns slowly without extra tricks.

The Policy Objective Function J(θ)J(\theta)

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

The goal is to maximize total expected discounted return over trajectories τ=(s0,a0,r0,s1,a1,r1,… )\tau = (s_0, a_0, r_0, s_1, a_1, r_1, \dots):

J(θ)=Eτ∼πθ[R(τ)]=∫P(τ;θ)R(τ)dτJ(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}[R(\tau)] = \int P(\tau; \theta) R(\tau) d\tau

Where trajectory probability P(τ;θ)=P(s0)∏t=0TP(st+1∣st,at)πθ(at∣st)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:

∇θJ(θ)=∇θ∫P(τ;θ)R(τ)dτ=∫∇θP(τ;θ)R(τ)dτ\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 (∇θP(τ;θ)=P(τ;θ)∇θln⁡P(τ;θ)\nabla_\theta P(\tau; \theta) = P(\tau; \theta) \nabla_\theta \ln P(\tau; \theta)):

∇θJ(θ)=∫P(τ;θ)∇θln⁡P(τ;θ)R(τ)dτ=Eτ∼πθ[∇θln⁡P(τ;θ)R(τ)]\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(τ;θ)=ln⁡P(s0)+∑t=0Tln⁡P(st+1∣st,at)+∑t=0Tln⁡πθ(at∣st)\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(st+1∣st,at)P(s_{t+1} \mid s_t, a_t) completely!

∇θJ(θ)=Eπθ[∑t=0T∇θln⁡πθ(at∣st)⋅Gt]\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)

python
# 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