Skip to content
MLiS Hub

Rare Trajectory Sampling with Reinforcement Learning

Minimising KL is maximising returns

In this article we show that minimising the KL divergence is equivalent to the standard policy gradient objective. The derivation proceeds in two steps: first computing ∇θL(θ)\nabla_\theta L(\theta) directly, then computing ∇θDKL\nabla_\theta D_\text{KL} and showing the two expressions coincide.

Let’s assume a standard RL loss function which sums the expected value of each trajectory at each time step: L(θ)=𝔼w∼pπθ[∑tγtRt] \begin{aligned} L(\theta) &= \mathbb{E}_{w \sim p_{\pi_{\theta}}}\left[\sum_t \gamma^t R_t\right] \end{aligned} This can be re-written using the explicit form of the expectation and using that the return GG is the sum of the reward, G(w)=∑tγtRtG(w) = \sum_{t} \gamma^t R_t. For the remainder of this article we set the discount factor γ=1\gamma = 1 so as to equally weight the reward at each time-step instead of favouring short term gains. L(θ)=∑wpπθ(w)G(w) L(\theta) = \sum_w p_{\pi_{\theta}}(w)\, G(w) To find the maximum value of the returns, differentiate with respect to the policy parameters, θ\theta. ∇θL(θ)=∇θ∑wG(w) pπθ(w) \nabla_\theta L(\theta) = \nabla_\theta \sum_w G(w)\ p_{\pi_\theta}(w) Moving the derivative operator inwards past terms that have no θ\theta dependence, ∇θL(θ)=∑wG(w)∇θpπθ(w) \nabla_\theta L(\theta) = \sum_w G(w)\, \nabla_\theta p_{\pi_\theta}(w) Next, we can use a calculus identity known as the log derivative trick, ∇θpπθ(w)=pπθ(w)∇θlog⁡pπθ(w) \nabla_\theta p_{\pi_\theta}(w) = p_{\pi_\theta}(w) \nabla_\theta \log p_{\pi_\theta}(w) to rewrite as… ∇θL(θ)=∑wG(w)pπθ(w)∇θlog⁡pπθ(w) \nabla_\theta L(\theta) = \sum_w G(w)\, p_{\pi_\theta}(w) \nabla_\theta \log p_{\pi_\theta}(w) We are aiming to find some θ\theta which approximates the doob dynamics such that pdoob≈pπθp_{doob} \approx p_{\pi_\theta} including an environment term, P(St+1,Rt|St,At)P(S_{t+1},R_t|S_t,A_t), log⁡pπθ(w)=log⁡d(s0)+∑tlog⁡πθ(at∣st)+∑tlog⁡P(st+1∣st,at) \log p_{\pi_\theta}(w) = \log d(s_0) + \sum_t \log \pi_\theta(a_t \mid s_t) + \sum_t \log P(s_{t+1} \mid s_t, a_t)

Differentiate with respect to θ\theta, ∇θlog⁡pπθ(w)=∑t∇θlog⁡πθ(at∣st) \nabla_\theta \log p_{\pi_\theta}(w) = \sum_t \nabla_\theta \log \pi_\theta(a_t \mid s_t)

Substitute back into the objective and convert back into expectation form, ∇θL(θ)=𝔼w∼pπθ[G(w)∑t∇θlog⁡πθ(at∣st)] \nabla_\theta L(\theta) = \mathbb{E}_{w \sim p_{\pi_\theta}}\left[ G(w)\, \sum_t \nabla_\theta \log \pi_\theta(a_t \mid s_t) \right]

We now derive the gradient of the KL divergence and will show it takes the same form.

Starting from the KL divergence between the controlled trajectory distribution and a tilted target distribution, DKL(pπθ‖p˜)=∑wpπθ(w)logpπθ(w)p˜(w) \begin{aligned} D_{\mathrm{KL}}(p_{\pi_\theta} \,\|\, \tilde{p}) &= \sum_w p_{\pi_\theta}(w)\, \log \frac{p_{\pi_\theta}(w)}{\tilde{p}(w)} \end{aligned}

Using the exponential reweighting form of the Doob dynamics,

DKL(pπθ‖p˜)=∑wpπθ(w)[log⁡pπθ(w)−log⁡p(w)−sO(w)+log⁡Z] \begin{aligned} D_{\mathrm{KL}}(p_{\pi_\theta} \,\|\, \tilde{p}) &= \sum_w p_{\pi_\theta}(w)\left[ \log p_{\pi_\theta}(w) - \log p(w) - s\, O(w) + \log Z \right] \end{aligned}

As ZZ has no dependence on ww, and hence not on θ\theta, we can move it outside the sum, DKL(pπθ‖p˜)=∑wpπθ(w)[log⁡pπθ(w)−log⁡p(w)−sO(w)]+logZ \begin{aligned} D_{\mathrm{KL}}(p_{\pi_\theta} \,\|\, \tilde{p}) &= \sum_w p_{\pi_\theta}(w)\left[ \log p_{\pi_\theta}(w) - \log p(w) - s\, O(w) \right] + \log Z \end{aligned}

Take the gradient with respect to θ\theta. Importantly, ∇θlogZ=0\nabla_\theta\, log Z = 0, as ZZ has no effect on the gradient. We also move the derivative operator inside the sum. ∇θDKL=∑w∇θpπθ(w)[log⁡pπθ(w)−log⁡p(w)−sO(w)] \begin{aligned} \nabla_\theta D_{\mathrm{KL}} &= \sum_w \nabla_\theta p_{\pi_\theta}(w)\left[ \log p_{\pi_\theta}(w) - \log p(w) - s\, O(w) \right] \end{aligned}

Apply product rule: ∇θDKL=∑w∇θpπθ(w)[log⁡pπθ(w)−log⁡p(w)−sO(w)]+pπθ(w)∇θlogpπθ(w) \begin{aligned} \nabla_\theta D_{\mathrm{KL}} &= \sum_w \nabla_\theta p_{\pi_\theta}(w)\left[ \log p_{\pi_\theta}(w) - \log p(w) - s\, O(w) \right] + p_{\pi_\theta}(w)\, \nabla_\theta \log p_{\pi_\theta}(w) \end{aligned}

Here we can use the same log derivative identity, ∇θpπθ(w)=pπθ(w)∇θlog⁡pπθ(w)\nabla_\theta p_{\pi_\theta}(w)= p_{\pi_\theta}(w) \nabla_\theta \log p_{\pi_\theta}(w), as in the previous derivation to make the terms similar and allow us to combine them,

∇θDKL=∑wpπθ(w)∇θlogpπθ(w)[log⁡pπθ(w)−log⁡p(w)−sO(w)+1] \begin{aligned} \nabla_\theta D_{\mathrm{KL}} &= \sum_w p_{\pi_\theta}(w)\, \nabla_\theta \log p_{\pi_\theta}(w) \left[ \log p_{\pi_\theta}(w) - \log p(w) - s\, O(w) + 1 \right] \end{aligned}

Write in expectation form: ∇θDKL=𝔼w∼pπθ[∇θlog⁡pπθ(w)(log⁡pπθ(w)−log⁡p(w)−sO(w)+1)] \nabla_\theta D_{\mathrm{KL}} = \mathbb{E}_{w \sim p_{\pi_\theta}}\left[ \nabla_\theta \log p_{\pi_\theta}(w) \left( \log p_{\pi_\theta}(w) - \log p(w) - s\, O(w) + 1 \right) \right]

Noting the similarities in structure between this and the loss function allows us to identify a reward,

Rt=so(wt)−log⁡pπθ(at∣st)p(at∣st) R_t = s o(w_t) - \log \frac{p_{\pi_\theta}(a_t \mid s_t)}{p(a_t \mid s_t)}

Where ∑to(wt)=O(w)\sum_t o(w_t) = O(w) is the per step contribution of the observable,and (at∣st)(a_t \mid s_t) is taken to be the per timestep contribution of (w)(w). Additionally, the +1+1 term is dropped as constants only act as baselines and have no effect on the gradient. Using G(w)=∑tRtG(w) = \sum_t R_t, we arrive at:

∇θDKL=−𝔼w∼pπθ[G(w)∑t∇θlog⁡πθ(at∣st)] \nabla_\theta D_{\mathrm{KL}} = -\mathbb{E}_{w \sim p_{\pi_\theta}}\left[ G(w)\, \sum_t \nabla_\theta \log \pi_\theta(a_t \mid s_t) \right]

Which is the additive inverse of our policy gradient function and thus minimising the kl divergence is equivelant to maximisng the policy gradient.

The reward identified by this equivalence has two contributions. The term so(wt)s\, o(w_t) scores each timestep by the observable, directly incentivising trajectories that realise the rare event of interest. The log-ratio log⁡pπθ/p\log p_{\pi_\theta} / p, serves as an entropy regularizer, rare-event sampling requires a policy that covers the full distribution of trajectories leading to the event. Penalising deviation from the unbiased dynamics achieves this, and (as shown above) doing so is precisely equivalent to minimising the KL divergence to the Doob distribution.


← Back to the main article: The KL divergence