ML Notes
Learning optimal policies directly using gradient ascent on policy parameters.
Policy gradient methods directly optimize the policy instead of learning value functions.
Policy Representation
Parameterized Policy:
π_θ(a|s) = P(action=a | state=s; parameters θ)
Typically neural network outputs probabilities
Policy Gradient Theorem
∇_θ J(θ) = E[∇_θ log π_θ(a|s) * Q^π(s,a)]
Gradient of objective = expected policy gradient weighted by action value
REINFORCE Algorithm
Simplest policy gradient method:
Actor-Critic Methods
Combine policy gradient (actor) with value function (critic):
Actor: Outputs actions π_θ(a|s)
Critic: Estimates value V_φ(s)
Actor improved by critic feedback
Critic improved by TD error
A2C (Advantage Actor-Critic)
Actor loss: -log π_θ(a|s) * A(s,a)
A(s,a) = r + γV(s') - V(s) [Advantage = Q - V]
PPO (Proximal Policy Optimization)
More stable than REINFORCE, better than A2C:
Continuous Control
Policy outputs action distribution for continuous actions:
# Gaussian policy for continuous actions
mean = network_mean(state)
log_std = network_log_std(state) # Learned parameter
std = exp(log_std)
# Sample action from distribution
action = mean + std * random_normal()
# Log probability
log_prob = -0.5 * ((action - mean) / std) ** 2 - log_stdAdvantages of Policy Gradient
Advantages:
- Direct optimization of policy
- Works with continuous action spaces
- Theoretically grounded
- No need for max operation (stable)
- Better convergence properties than Q-learning
Disadvantages:
- High variance (need many samples)
- Slower convergence than value-based
- Often requires large batches
Comparison
| Method | Type | Best For | Convergence |
|---|---|---|---|
| Q-Learning | Value | Discrete actions | Fast but unstable |
| DQN | Value | High-dim discrete | Stable, slow |
| REINFORCE | Policy | Simple baseline | Very slow |
| A2C | Actor-Critic | Small/medium | Medium |
| PPO | Policy | General use | Fast, stable |
Summary
Policy gradient:
- Optimize policy directly
- Use gradient ascent on expected return
- Reduces variance with baseline (critic)
- Works for continuous actions
- PPO is practical choice
Practical Implementation Tips
When implementing policy gradient methods in practice, several details make the difference between working and failing algorithms:
Reward Scaling: Raw rewards can vary enormously across environments. Normalize rewards by subtracting the mean and dividing by the standard deviation of a rolling window. This stabilizes gradient magnitudes and prevents the policy from oscillating.
Entropy Regularization: Add an entropy bonus to the loss function to encourage exploration. Without this, the policy can collapse to a deterministic strategy too early, getting stuck in local optima. The entropy coefficient is typically annealed from a high value (more exploration) to a low value (more exploitation) during training.
Gradient Clipping: Policy gradient updates can occasionally be very large, destabilizing training. Clip gradients by global norm (typically 0.5-1.0) to prevent catastrophic parameter updates while still allowing learning from strong signals.
Learning Rate Scheduling: Start with a moderate learning rate (3e-4 is a common default for Adam optimizer with policy gradients) and reduce it as training progresses. Too high a learning rate causes policy oscillation; too low means painfully slow learning.
When to Use Which Algorithm
Choosing the right policy gradient variant depends on your specific problem:
- REINFORCE: Educational purposes or extremely simple environments. Too high variance for practical use without significant modifications.
- A2C (Advantage Actor-Critic): Good for environments where you can run many parallel instances. Synchronous updates make debugging easier.
- A3C: Like A2C but asynchronous. Historical importance but largely superseded by PPO.
- PPO (Proximal Policy Optimization): The go-to choice for most practical applications. Robust, relatively hyperparameter-insensitive, and works across diverse environments. OpenAI's default for most tasks.
- SAC (Soft Actor-Critic): Best for continuous control tasks where sample efficiency matters. Uses entropy maximization for robust exploration.
- TRPO (Trust Region Policy Optimization): Theoretically elegant with guaranteed monotonic improvement, but complex to implement. PPO achieves similar results more simply.
Debugging Policy Gradient Training
Policy gradient methods are notoriously difficult to debug. Key diagnostics to monitor:
- Policy entropy: Should decrease gradually. Sudden collapse indicates premature convergence.
- Value function loss: Should decrease over training. If not, the critic isn't learning properly.
- KL divergence between old and new policy: Should stay small (PPO clips this by design).
- Episode reward trend: Plot rolling average. Expect noisy but upward-trending curves.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Policy Gradient Methods — Machine Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, reinforcement, policy, gradient, methods
Related Machine Learning Topics