Continuous actions: DDPG and PPO
How to train agents with continuous actions using Deep Deterministic Policy Gradient (DDPG) and Proximal Policy Optimization (PPO): key principles, derivations, formulas, worked examples, common exam mistakes and quick checks.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Two modern problems in control and robotics require choosing continuous-valued commands (steering angle, motor torque, throttle). DDPG and PPO are practical algorithms engineers use to train neural-network policies for continuous-action tasks (robot arms, autonomous driving, continuous trading). They let you learn policies that output real-valued actions while handling stability (PPO) and sample efficiency (DDPG with replay).
Key ideas
Start from first principles. A reinforcement learning (RL) problem with continuous actions uses a Markov decision process (MDP) with a continuous action space A ⊆ R^n.
- Define policy πθ(a|s) as a probability density over continuous actions a given state s, parameterised by θ. For stochastic policies πθ(a|s) is a density; for deterministic policies μθ(s) it is a function that outputs an action deterministically.
- Define value functions: state-value Vπ(s) = Eπ[Σt γ^t r_t | s0 = s], action-value Qπ(s,a) = Eπ[Σt γ^t r_t | s0=s,a0=a]. These are expectations with the same units as the reward; γ ∈ [0,1) is the dimensionless discount factor.
Two algorithm families for continuous actions:
Deterministic actor-critic with replay and target networks: DDPG (Deep Deterministic Policy Gradient).
- Uses a deterministic actor μθ(s) (outputs action) and a critic Qφ(s,a) (estimates Q-value).
- Learns the critic by regression to a target y = r + γ (1 - d) Qφ'(s', μθ'(s')) (with d indicating episode termination). The target networks θ', φ' are slowly updated copies of the main networks for stability (soft updates).
- Uses the deterministic policy gradient theorem: the gradient of the expected return J(θ) for deterministic policies under assumptions of ergodicity and differentiability is ∇θ J(θ) = Es∼D[∇θ μθ(s) ∇a Qφ(s,a)|_{a=μθ(s)}].
- Uses experience replay buffer to de-correlate samples. Exploration during data collection is typically implemented by adding temporally correlated (Ornstein–Uhlenbeck) or Gaussian noise to the actor output (behavior policy).
- Assumptions: continuous action space, differentiable actor and critic, off-policy data allowed via replay.
Diagram to draw for an exam: two boxes labelled Actor μθ(s) and Critic Qφ(s,a); show state s into actor → action a; s and a into critic → Q-value; show target networks μθ' and Qφ' with arrow showing soft update θ' ← τθ + (1-τ)θ'. Draw replay buffer with arrows from agent to buffer and from buffer to both networks.
Stochastic policy methods with trust-region style updates: PPO (Proximal Policy Optimization).
- Uses a stochastic policy πθ(a|s) (commonly Gaussian for continuous actions). Learns by maximising a clipped surrogate objective to avoid large policy updates.
- Define probability ratio r(θ) = πθ(a|s) / πθ_old(a|s). Use advantage  (from GAE or simple estimation). PPO objective (clip version): L(θ) = E[min(r(θ) Â, clip(r(θ), 1-ε, 1+ε) Â)].
- Also add an entropy bonus to encourage exploration and a value-function loss for the critic when using actor-critic variant. Many implementations also monitor the KL divergence between πθ and πθ_old to early-stop or add a KL penalty.
- Assumptions: on-policy or near on-policy batches, differentiable stochastic policy, advantages estimated with low-variance methods (GAE) help.
Diagram to draw: policy πθ generating a and logπ; show computing ratio with old policy πθ_old; show clipping region [1-ε, 1+ε] and choosing min between clipped and unclipped surrogate; show gradient ascent on L.
Derivation universities ask for (summary): deterministic policy gradient theorem is obtained by writing J(θ)=Es~ρθ[s][Q^{μθ}(s, μθ(s))] where ρθ is state distribution under μθ, then taking gradient and using chain rule, ignoring derivative of ρθ under some assumptions or using performance-gradient identity. Final outcome is the actor gradient above.
Formulas
y = r + γ · (1 - d) · Qφ'(s', μθ'(s')) — target for critic in DDPG; r is the reward, γ is the dimensionless discount, d ∈ {0,1} indicates terminal (1 if terminal), and Q has the same units as reward; applies in one-step TD target.
L(φ) = 1/N Σ_i (Qφ(s_i, a_i) - y_i)^2 — critic MSE loss; φ critic params; applies when training critic on sampled mini-batch of size N.
∇θ J(θ) ≈ 1/N Σ_i ∇θ μθ(s_i) ∇a Qφ(s_i, a)|_{a=μθ(s_i)} — deterministic policy gradient used to update actor in DDPG; θ actor params.
θ' ← τ θ + (1 - τ) θ' and φ' ← τ φ + (1 - τ) φ' — soft target update (τ ∈ (0,1], typical τ ≈ 1e-3).
r(θ) = πθ(a|s) / πθ_old(a|s) — PPO probability ratio; densities for continuous actions.
L^{PPO}(θ) = E_s,a[ min(r(θ) Â, clip(r(θ), 1-ε, 1+ε) Â) ] — clipped surrogate objective; ε typical 0.1–0.2, Â advantage estimate (dimensionless).
L_{total} = -L^{PPO} + c_v L_{value} - c_e H[πθ] — full PPO loss to minimise (negative surrogate + value loss - entropy bonus). c_v, c_e scalar coefficients; H entropy (nats).
Notes on units: Returns, values (V, Q), and advantages have the same units as reward; γ is dimensionless. There are no SI units for θ, φ; reward units depend on the environment (e.g., metres, seconds, or a dimensionless score).
Worked examples
Derivation (exam-style) — Deterministic Policy Gradient, step-by-step model answer
- Start with objective J(θ) = E_{s∼ρθ}[R(s)] where R(s) denotes the expected return from s under μθ.
- Write J(θ) = ∫ ρθ(s) Qμθ(s, μθ(s)) ds.
- Differentiate: ∇θ J = ∫ [∇θ ρθ(s)] Q + ρθ(s) ∇θ Q(s, μθ(s)) ds.
- Under the policy gradient/ performance difference identity and ignoring the derivative of ρθ (or by a detailed derivation using the stationary discounted state distribution under ergodicity) the main term reduces to E_s[∇θ μθ(s) ∇a Q(s,a)|_{a=μθ(s)}].
- Final: ∇θ J(θ) = E_s[∇θ μθ(s) ∇a Q(s,a)|_{a=μθ(s)}].
- Comment: the critic provides ∇a Q(s,a) (backprop through critic), which is chain-ruled with ∇θ μθ(s) (backprop through actor). This yields the actor parameter update.
Small Python example (PPO clipped surrogate calculation for a single scalar action sample)
Program (runs with Python 3, requires only numpy):
import numpy as np
# Simple PPO surrogate clip calculation for a single sample
a = 1.0 # action taken
old_mu = 0.0 # previous policy mean
sigma = 1.0 # fixed std dev
mu = 0.1 # current policy mean (parameter)
adv = 1.0 # advantage estimate
eps = 0.2 # clipping epsilon
def logp(a, mu, sigma):
return -0.5 * ((a - mu) ** 2) / (sigma ** 2) - 0.5 * np.log(2 * np.pi * sigma ** 2)
old_logp = logp(a, old_mu, sigma)
logp_curr = logp(a, mu, sigma)
ratio = np.exp(logp_curr - old_logp)
unclipped = ratio * adv
clipped = np.clip(ratio, 1 - eps, 1 + eps) * adv
surrogate = min(unclipped, clipped)
loss = -surrogate # we minimise negative of surrogate
print('ratio =', ratio)
print('unclipped =', unclipped)
print('clipped =', clipped)
print('surrogate =', surrogate)
print('loss =', loss)
Sample input: (the code above uses its own constants; no interactive input required)
Exact output (approximate numerical values; printed by the program):
ratio = 1.099659707 unclipped = 1.099659707 clipped = 1.099659707 surrogate = 1.099659707 loss = -1.099659707
Explanation: with a=1.0, old_mu=0.0, mu=0.1 and sigma=1.0 we compute log-probability difference 0.095, ratio = exp(0.095) ≈ 1.09966. Clipping does not change ratio here.
(Reviewer note: numeric outputs given to 9 decimal places are approximations; verify on a machine.)
Common mistakes
- Using large learning rates or no replay for DDPG: leads to instability due to correlated updates.
- In PPO, applying the clip incorrectly: the min operation is on the surrogate terms, not on the ratio alone.
- Miscomputing advantage  (forgetting baseline or using high-variance Monte Carlo returns without bootstrapping/GAE).
- Not scaling action outputs to environment action bounds: actor outputs must be mapped (tanh or scaling) to action range.
For your exam
Question types you will see: definitions (DDPG, actor-critic, surrogate objective), derivations (deterministic policy gradient theorem, critic loss derivation), algorithm descriptions (pseudo-code for DDPG or PPO), numerical evaluation of a surrogate or target value, and diagram-drawing (actor, critic, replay, target networks).
Full-mark answers include:
- Clear definitions of policy types (stochastic vs deterministic), value functions and assumptions (differentiability, ergodicity).
- Correct formulas for critic target and actor gradient in DDPG, and correct PPO clip objective including description of ratio r(θ) and ε.
- Pseudocode or stepwise algorithm with hyperparameters: learning rates, τ for soft updates, replay buffer usage for DDPG; epochs per batch and clip ε for PPO.
- Short explanation of stability tricks: target networks and replay (DDPG), clipping and multiple epochs on sampled on-policy batches (PPO).
Quick check
- In DDPG why do we use a replay buffer?
- Write the DDPG actor gradient formula.
- What is the PPO clip range if ε = 0.15?
Answers: 1) To break correlation between consecutive samples and enable off-policy minibatch updates (stability and sample reuse). 2) ∇θ J(θ) ≈ E_s[∇θ μθ(s) ∇a Qφ(s,a)|_{a=μθ(s)}]. 3) [1 - 0.15, 1 + 0.15] = [0.85, 1.15].
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?