Skip to Content
CSE510CSE510 Deep Reinforcement Learning (Lecture 13)

CSE510 Deep Reinforcement Learning (Lecture 13)

Recap from last lecture

For any differentiable policy πθ(s,a)\pi_\theta(s,a), for any o the policy objective functions J=J1,JavRJ=J_1, J_{avR} or 11−γJavV\frac{1}{1-\gamma} J_{avV}

The policy gradient is

∇θJ(θ)=Eπθ[∇θlog⁡πθ(s,a)Qπθ(s,a)]\nabla_{\theta}J(\theta)=\mathbb{E}_{\pi_{\theta}}\left[\nabla_\theta \log \pi_\theta(s,a)Q^{\pi_\theta}(s,a)\right]

Problem for policy gradient method

Data Inefficiency

  • On-policy method: for each new policy, we need to generate a completely new trajectory
  • The data is thrown out after just one gradient update
  • As complex neural networks need many updates, this makes the training process very slow

Unstable update: step size is very important

  • If step size is too large:
    • Large step -> bad policy
    • Next batch is generated from current bad policy → collect bad samples
    • Bad samples -> worse policy (compare to supervised learning: the correct label and data in the following batches may correct it)
  • If step size is too small: the learning process is slow

Deriving the optimization objection function of Trusted Region Policy Optimization (TRPO)

Objective of Policy Gradient Methods

Policy Objective

J(πθ)=Eτ∼πtheta∑t=0∞γtrtJ(\pi_\theta)=\mathbb{E}_{\tau\sim \pi_theta}\sum_{t=0}^{\infty} \gamma^t r^t

here τ\tau is the trajectory for the policy πθ\pi_\theta.

Policy objective can be written in terms of old one:

J(πθ′)−J(πθ)=Eτ∼πθ′∑t=0∞γtAπθ(st,at)J(\pi_{\theta'})-J(\pi_{\theta})=\mathbb{E}_{\tau \sim \pi_{\theta'}}\sum_{t=0}^{\infty}\gamma^t A^{\pi_\theta}(s_t,a_t)

Equivalently for succinctness:

J(π′)−J(π)=Eτ∈π′∑t=0∞Aπ(st,at)J(\pi')-J(\pi)=\mathbb{E}_{\tau\in \pi'}\sum_{t=0}^{\infty} A^{\pi}(s_t,a_t)

Proof

Eτ∼π′[γtAπθ(st,at)]=Eτ∼π′[∑t=0∞γtR(s0)+∑t=0∞γt+1Vπ(st+1)−∑t=0∞γtVπ(st)]=J(π′)+∑t=1∞γtVπ(st)−∑t=0∞γtVπ(st)=J(π′)−Eτ∼π′Vπ(s0)=J(π′)−J(π)\begin{aligned} &\mathbb{E}_{\tau\sim \pi'}\left[\gamma^t A^{\pi_\theta}(s_t,a_t)\right]\\ &=E_{\tau\sim \pi'}\left[\sum_{t=0}^{\infty}\gamma^t R(s_0)+\sum_{t=0}^{\infty} \gamma^{t+1}V^{\pi}(s_{t+1})-\sum_{t=0}^{\infty} \gamma^{t}V^\pi(s_t)\right]\\ &=J(\pi')+\sum_{t=1}^{\infty} \gamma^{t}V^{\pi}(s_t)-\sum_{t=0}^{\infty} \gamma^{t}V^\pi(s_t)\\ &=J(\pi')-\mathbb{E}_{\tau\sim\pi'}V^{\pi}(s_0)\\ &=J(\pi')-J(\pi) \end{aligned}

Importance Sampling

Estimate one distribution by sampling form anther distribution

Ex∼p[f(x)]=∫f(x)p(x)dx=∫f(x)p(x)q(x)q(x)dx=Ex∼q[f(x)p(x)q(x)]≈1N∑i=1,xi∈qNf(xi)p(xi)q(xi)\begin{aligned} \mathbb{E}_{x\sim p}[f(x)]&=\int f(x)p(x)dx &=\int f(x)\frac{p(x)}{q(x)}q(x)dx\\ &=\mathbb{E}_{x\sim q}\left[f(x)\frac{p(x)}{q(x)}\right]\\ &\approx \frac{1}{N}\sum_{i=1,x^i\in q}^N f(x^i)\frac{p(x^i)}{q(x^i)} \end{aligned}

Estimating objective with importance sampling

Discounted state visit distribution:

dπ(s)=(1−γ)∑t=0∞γtP(st=s∣π)d^\pi(s)=(1-\gamma)\sum_{t=0}^{\infty}\gamma^t P(s_t=s|\pi) J(π′)−J(π)=Eτ∼π′∑t=0∞γtAπ(st,at)=Es∼dπ′,a∼π′Aπ(s,a)=Es∼dπ′,a∼π[Aπ′(s,a)π′(a∣s)π(a∣s)]\begin{aligned} J(\pi')-J(\pi)&=\mathbb{E}_{\tau\sim\pi'}\sum_{t=0}^{\infty} \gamma^t A^{\pi}(s_t,a_t)\\ &=\mathbb{E}_{s\sim d^{\pi'}, a\sim \pi'} A^{\pi}(s,a)\\ &=\mathbb{E}_{s\sim d^{\pi'}, a\sim \pi}\left[A^{\pi'}(s,a)\frac{\pi'(a|s)}{\pi(a|s)}\right]\\ \end{aligned}

Using the old policy to sample states form a policy that we are trying to optimize.

Lπ(π′)=Es∼dπ′,a∼π[Aπ′(s,a)π′(a∣s)π(a∣s)]L_\pi(\pi')=\mathbb{E}_{s\sim d^{\pi'}, a\sim \pi}\left[A^{\pi'}(s,a)\frac{\pi'(a|s)}{\pi(a|s)}\right]

Lower bound of Optimization

Note

(Kullback-Leibler) KL divergence is a measure of the difference between two probability distributions.

DKL(π(∣˙s)∣∣π′(∣˙s))=∫sπ(a∣s)log⁡π(a∣s)π′(a∣s)daD_{KL}(\pi(\dot|s)||\pi'(\dot|s))=\int_s \pi(a|s)\log \frac{\pi(a|s)}{\pi'(a|s)}da

J(π′)−J(π)≥Lπ(π′)−Cmax⁡s∈SDKL(π(∣˙s)∣∣π′(∣˙s))J(\pi')-J(\pi)\geq L_\pi(\pi')-C\max_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))

where CC is a constant.

Optimizing the objective function:

max⁡π′J(π′)−J(π)\max_{\pi'} J(\pi')-J(\pi)

By maximizing the lower bound

max⁡π′Lπ(π′)−Cmax⁡s∈SDKL(π(∣˙s)∣∣π′(∣˙s))\max_{\pi'} L_\pi(\pi')-C\max_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))

Monotonic Improvement Theorem

Proof of improvement guarantee: Suppose πk+1\pi_{k+1} and πk\pi_k are related by

πk+1=max⁡π′Lπ(π′)−Cmax⁡s∈SDKL(π(∣˙s)∣∣π′(∣˙s))\pi_{k+1}=\max_{\pi'} L_\pi(\pi')-C\max_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))

πk\pi_{k} is a feasible point, and the objective at πk\pi_k is equal to 0.

Lπk(πk)∝Es,a∼dπk[Aπk(s,a)]=0L_{\pi_k}(\pi_{k})\propto \mathbb{E}_{s,a\sim d^{\pi_k}}[A^{\pi_k}(s,a)]=0 DKL(πk∣∣πk)[s]=0D_{KL}(\pi_k||\pi_k)[s]=0

Optimal value ≥0\geq 0.

By the performance bound, Jpik+1−Jpik≥0J_{pi_{k+1}}-J_{pi_k}\geq 0.

Final objective function

max⁡π′Es∼dπ,a∼π[Aπ′(s,a)π′(a∣s)π(a∣s)]−Cmax⁡s∈SDKL(π(∣˙s)∣∣π′(∣˙s))\max_{\pi'}\mathbb{E}_{s\sim d^{\pi}, a\sim \pi}[A^{\pi'}(s,a)\frac{\pi'(a|s)}{\pi(a|s)}]-C\max_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))

by approximation

max⁡π′Es∼dπ,a∼π[Aπ′(s,a)π′(a∣s)π(a∣s)]−CEs∈SDKL(π(∣˙s)∣∣π′(∣˙s))\max_{\pi'}\mathbb{E}_{s\sim d^{\pi}, a\sim \pi}[A^{\pi'}(s,a)\frac{\pi'(a|s)}{\pi(a|s)}]-C\mathbb{E}_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))

With the Lagrangian Duality, the objective is mathematically the same as following using a trust region constraint:

max⁡π′Lπ(π′)\max_{\pi'} L_\pi(\pi')

such that

Es∈SDKL(π(∣˙s)∣∣π′(∣˙s))≤δ\mathbb{E}_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))\leq \delta

CC gets very high when γ\gamma is close to one and the corresponding gradient step size becomes too small.

C∝ϵγ(1−γ)2C\propto \frac{\epsilon \gamma}{(1-\gamma)^2}
  • Empirical results show that it needs to more adaptive
  • But Tuning CC is hard (need some trick just like PPO)
  • TRPO uses trust region constraint and make δ\delta a tunable hyperparameter.

Trust Region Policy Optimization (TRPO)

max⁡π′Lπ(π′)\max_{\pi'} L_\pi(\pi')

such that

Es∈SDKL(π(∣˙s)∣∣π′(∣˙s))≤δ\mathbb{E}_{s\in S}D_{KL}(\pi(\dot|s)||\pi'(\dot|s))\leq \delta

Make linear approximation to LπθoldL_{\pi_{\theta_{old}}} and quadratic approximation to KL term.

Maximize g⋅(θ−θold)−β2(θ−θold)⊤F(θ−θold)g\cdot(\theta-\theta_{old})-\frac{\beta}{2}(\theta-\theta_{old})^\top F(\theta-\theta_{old})

where g=∂∂θLπθold(πθ)∣θ=θoldg=\frac{\partial}{\partial \theta}L_{\pi_{\theta_{old}}}(\pi_{\theta})\vert_{\theta=\theta_{old}} and F=∂2∂θ2KL‾πθold(πθ)∣θ=θoldF=\frac{\partial^2}{\partial \theta^2}\overline{KL}_{\pi_{\theta_{old}}}(\pi_{\theta})\vert_{\theta=\theta_{old}}

Taylor Expansion of KL Term

DKL(πθold∣πθ)≈DKL(πθold∣πθold)+d⊤∇θDKL(πθold∣πθ)∣θ=θold+12d⊤∇θ2DKL(πθold∣πθ)∣θ=θolddD_{KL}(\pi_{\theta_{old}}|\pi_{\theta})\approx D_{KL}(\pi_{\theta_{old}}|\pi_{\theta_{old}})+d^\top \nabla_\theta D_{KL}(\pi_{\theta_{old}}|\pi_{\theta})\vert_{\theta=\theta_{old}}+\frac{1}{2}d^\top \nabla_\theta^2 D_{KL}(\pi_{\theta_{old}}|\pi_{\theta})\vert_{\theta=\theta_{old}}d ∇θDKL(πθold∣πθ)=−∇θEx∼πθoldlog⁡Pθ(x)∣θ=θold=−Ex∼πθold∇θlog⁡Pθ(x)∣θ=θold=−Ex∼πθold1πθold(x)∇θPθ(x)∣θ=θold=∫xPθold(x)1Pθold(x)∇θPθ(x)∣θ=θolddx=∫xPθold(x)∇θPθ(x)∣θ=θolddx=∇θ∫xPθold(x)∣θ=θolddx=0\begin{aligned} \nabla_\theta D_{KL}(\pi_{\theta_{old}}|\pi_{\theta})&=-\nabla_\theta \mathbb{E}_{x\sim \pi_{\theta_{old}}}\log P_\theta(x)\vert_{\theta=\theta_{old}}\\ &=-\mathbb{E}_{x\sim \pi_{\theta_{old}}}\nabla_\theta \log P_\theta(x)\vert_{\theta=\theta_{old}}\\ &=-\mathbb{E}_{x\sim \pi_{\theta_{old}}}\frac{1}{\pi_{\theta_{old}}(x)}\nabla_\theta P_\theta(x)\vert_{\theta=\theta_{old}}\\ &=\int_x P_{\theta_{old}}(x)\frac{1}{P_{\theta_{old}}(x)}\nabla_\theta P_\theta(x)\vert_{\theta=\theta_{old}} dx\\ &=\int_x P_{\theta_{old}}(x)\nabla_\theta P_\theta(x)\vert_{\theta=\theta_{old}} dx\\ &=\nabla_\theta \int_x P_{\theta_{old}}(x) \vert_{\theta=\theta_{old}} dx\\ &=0 \end{aligned} ∇θ2DKL(πθold∣πθ)∣θ=θold=−Ex∼πθold∇θ2log⁡Pθ(x)∣θ=θold=−Ex∼πθold∇θ(∇θPθ(x)Pθ(x))∣θ=θold=−Ex∼πθold(∇θ2Pθ(x)−∇θPθ(x)∇θPθ(x)⊤Pθ(x)2)∣θ=θold=−Ex∼πθold(∇θ2Pθ(x)∣θ=θoldPθold(x))+Ex∼πθold(∇θlog⁡Pθ(x)∇θlog⁡Pθ(x)⊤)∣θ=θold=Ex∼πθold∇θlog⁡Pθ(x)∇θlog⁡Pθ(x)⊤∣θ=θold\begin{aligned} \nabla_\theta^2 D_{KL}(\pi_{\theta_{old}}|\pi_{\theta})\vert_{\theta=\theta_{old}}&=-\mathbb{E}_{x\sim \pi_{\theta_{old}}}\nabla_\theta^2 \log P_\theta(x)\vert_{\theta=\theta_{old}}\\ &=-\mathbb{E}_{x\sim \pi_{\theta_{old}}}\nabla_\theta \left(\frac{\nabla_\theta P_\theta(x)}{P_\theta(x)}\right)\vert_{\theta=\theta_{old}}\\ &=-\mathbb{E}_{x\sim \pi_{\theta_{old}}}\left(\frac{\nabla_\theta^2 P_\theta(x)-\nabla_\theta P_\theta(x)\nabla_\theta P_\theta(x)^\top}{P_\theta(x)^2}\right)\vert_{\theta=\theta_{old}}\\ &=-\mathbb{E}_{x\sim \pi_{\theta_{old}}}\left(\frac{\nabla_\theta^2 P_\theta(x)\vert_{\theta=\theta_{old}}}P_{\theta_{old}}(x)\right)+\mathbb{E}_{x\sim \pi_{\theta_{old}}}\left(\nabla_\theta \log P_\theta(x)\nabla_\theta \log P_\theta(x)^\top\right)\vert_{\theta=\theta_{old}}\\ &=\mathbb{E}_{x\sim \pi_{\theta_{old}}}\nabla_\theta\log P_\theta(x)\nabla_\theta\log P_\theta(x)^\top\vert_{\theta=\theta_{old}}\\ \end{aligned}
Last updated on