Overview
For imitation learning, it is easy for us to have a fit on the expert behavior, However when we encounter a new situation, we can’t solve the problem. So we imagine that there is an expert strategy in the real world, and this expert strategy will give a reward function to guide our agents to learn. AIRL is based on the past GAIL. In GAIL, we directly use GAN to fit the behavior of the agent to the expert behavior, and do not directly give the reward function. This behavior will lead to our learning is not clear and not good. Migration, so we will record the function of the discriminator in GAN as D, and use D to represent our reward function. Below I will write in detail, how this is derived.
the goal of the IRL
Firstly, we define a distribution p θ ( τ ) p_\theta(\tau)pθ(τ). in another word, it is the expectation for the accumulated over the expert trajectory. so it can be wrote in this form. And D is the demonstrations of the expert.
p θ ( τ ) ∝ p ( s 0 ) ∏ t = 0 T − 1 p ( s t + 1 ∣ s t , a t ) e r θ ( s t , a t ) p_\theta(\tau)\propto p(s_0)\prod_{t=0}^{T-1}p(s_{t+1}|s_t,a_t)e^{r_{\theta}(s_t,a_t)}pθ(τ)∝p(s0)∏t=0T−1p(st+1∣st,at)erθ(st,at)
the goal of IRL can been seen as training a generative model over trajectories as:
max θ { J ( θ ) } = max θ E τ ∼ D [ log p θ ( τ ) ] \max \limits_{\theta}\{J(\theta)\}=\max \limits_{\theta}E_{\tau\sim D}[\log p_{\theta}(\tau)]θmax{J(θ)}=θmaxEτ∼D[logpθ(τ)]
Then we calculate the gradient with respect to θ \thetaθ(where p θ , t = ∫ s t ′ ≠ t , a t ′ ≠ t p θ ( τ ) p_{\theta,t}=\int_{s_{t'}\neq t,a_{t'}\neq t}p_{\theta(\tau) \qquad}pθ,t=∫st′=t,at′=tpθ(τ) denote the state-action marginal at time t):
∂ ∂ θ J ( θ ) = ∑ t = 0 T E D [ ∂ ∂ θ r θ ( s t , a t ) ] − E p θ , t [ ∂ ∂ θ r θ ( s t , a t ) ] \frac{\partial}{\partial \theta} J(\theta)=\sum_{t=0}^{T} E_{\mathcal{D}}\left[\frac{\partial}{\partial \theta} r_{\theta}\left(s_{t}, a_{t}\right)\right]-E_{p_{\theta, t}}\left[\frac{\partial}{\partial \theta} r_{\theta}\left(s_{t}, a_{t}\right)\right]∂θ∂J(θ)=∑t=0TED[∂θ∂rθ(st,at)]−Epθ,t[∂θ∂rθ(st,at)]
then we do importance sampling. here we instead train a separate importance sampling distribution μ ( τ ) \mu(\tau)μ(τ) . And the μ ( a ∣ s ) = 1 2 π ( a ∣ s ) + 1 2 p ( a ∣ s ) ^ \mu(a|s)=\frac{1}{2}\pi(a|s)+\frac{1}{2}\hat{p(a|s)}μ(a∣s)=21π(a∣s)+21p(a∣s)^.π ( a ∣ s ) \pi(a|s)π(a∣s) is our agent policy p ^ ( a ∣ s ) \hat p(a|s)p^(a∣s) is a rough density estimate trained on the demonstrations.
So, the gradient can be convert to the form below:
∂ ∂ θ J ( θ ) = ∑ t = 0 T E D [ ∂ ∂ θ r θ ( s t , a t ) ] − E μ t [ p θ , t ( s t , a t ) μ t ( s t , a t ) ∂ ∂ θ r θ ( s t , a t ) ] \frac{\partial}{\partial \theta} J(\theta)=\sum_{t=0}^{T} E_{\mathcal{D}}\left[\frac{\partial}{\partial \theta} r_{\theta}\left(s_{t}, a_{t}\right)\right]-E_{\mu_{t}}\left[\frac{p_{\theta, t}\left(s_{t}, a_{t}\right)}{\mu_{t}\left(s_{t}, a_{t}\right)} \frac{\partial}{\partial \theta} r_{\theta}\left(s_{t}, a_{t}\right)\right]∂θ∂J(θ)=∑t=0TED[∂θ∂rθ(st,at)]−Eμt[μt(st,at)pθ,t(st,at)∂θ∂rθ(st,at)]
Additionally, we wish to adapt the importance sampler π \piπ to reduce variance, by minimizing the KL divergency between π \piπ and p θ p_\thetapθ, and the KL divergency can be calculate as follow.
∵ D K L ( p ∥ q ) = E [ log p ( x ) − log q ( x ) ] \because D_{KL}(p \| q)=E[\log p(x)-\log q(x)]∵DKL(p∥q)=E[logp(x)−logq(x)]
∵ p θ ( τ ) = p ( s 0 ) ∏ t = 0 T − 1 p ( s t + 1 ∣ s t , a t ) e r θ ( s t , a t ) \because p_\theta(\tau)= p(s_0)\prod_{t=0}^{T-1}p(s_{t+1}|s_t,a_t)e^{r_{\theta}(s_t,a_t)}∵pθ(τ)=p(s0)∏t=0T−1p(st+1∣st,at)erθ(st,at)
∵ π ( τ ) = p ( s 0 ) ∏ t = 0 T − 1 p ( s t + 1 ∣ s t , a t ) π ( a t ∣ s t ) \because\pi(\tau)=p\left(s_{0}\right) \prod_{t=0}^{T-1} p\left(s_{t+1} | s_{t}, a_{t}\right) \pi\left(a_{t} | s_{t}\right)∵π(τ)=p(s0)∏t=0T−1p(st+1∣st,at)π(at∣st)
∴ D K L ( π ( τ ) ∥ p θ ( τ ) ) = l o g ( p ( s 0 ) ∏ t = 0 T − 1 p ( s t + 1 ∣ s t , a t ) π ( a t ∣ s t ) p ( s 0 ) ∏ t = 0 T − 1 p ( s t + 1 ∣ s t , a t ) e r θ ( s t , a t ) ) \therefore D_{KL}(\pi(\tau) \| p_\theta(\tau) )=log(\frac{p\left(s_{0}\right) \prod_{t=0}^{T-1} p\left(s_{t+1} | s_{t}, a_{t}\right) \pi\left(a_{t} | s_{t}\right)}{ p(s_0)\prod_{t=0}^{T-1}p(s_{t+1}|s_t,a_t)e^{r_{\theta}(s_t,a_t)}})∴DKL(π(τ)∥pθ(τ))=log(p(s0)∏t=0T−1p(st+1∣st,at)erθ(st,at)p(s0)∏t=0T−1p(st+1∣st,at)π(at∣st))
∴ D K L ( π ( τ ) ∥ p θ ( τ ) ) = l o g π ( a t ∣ s t ) − ∑ t = 0 T r θ ( s t , a t ) \therefore D_{KL}(\pi(\tau) \| p_\theta(\tau) )=log\pi(a_t|s_t)-\sum_{t=0}^{T}r_\theta(s_t,a_t)∴DKL(π(τ)∥pθ(τ))=logπ(at∣st)−∑t=0Trθ(st,at)
in a another word minimize the KL divergency is same to maximize the entropy
regularized policy objective:
max π E π [ ∑ t = 0 T r θ ( s t , a t ) − log π ( a t ∣ s t ) ) ] \left.\max _{\pi} E_{\pi}\left[\sum_{t=0}^{T} r_{\theta}\left(s_{t}, a_{t}\right)-\log \pi\left(a_{t} | s_{t}\right)\right)\right]maxπEπ[∑t=0Trθ(st,at)−logπ(at∣st))]
For AIRL, we calculate the D function from the loss of cross-entropy between expert demonstrations and generated samples(equal to the objective of the discriminator):
L ( θ ) = ∑ t = 0 T − E D [ log D θ ( s t , a t ) ] − E π t [ log ( 1 − D θ ( s t , a t ) ) ] \mathcal{L}(\theta)=\sum_{t=0}^{T}-E_{\mathcal{D}}\left[\log D_{\theta}\left(s_{t}, a_{t}\right)\right]-E_{\pi_{t}}\left[\log \left(1-D_{\theta}\left(s_{t}, a_{t}\right)\right)\right]L(θ)=∑t=0T−ED[logDθ(st,at)]−Eπt[log(1−Dθ(st,at))]
Ask the gradient of the loss function equal to zero.
− p θ D θ ( s , a ) + π ( a ∣ s ) 1 − D θ ( s , a ) = 0 -\frac{p_\theta}{D_\theta(s,a)}+\frac{\pi(a|s)}{1-D_\theta(s,a)}=0−Dθ(s,a)pθ+1−Dθ(s,a)π(a∣s)=0
∴ D θ ( s , a ) = p θ p θ + π ( a ∣ s ) \therefore D_\theta(s,a)=\frac{p_\theta}{p_\theta +\pi(a|s)}∴Dθ(s,a)=pθ+π(a∣s)pθ
Here we regard the p θ = e f θ ( s , a ) p_\theta=e^{f_{\theta(s,a)}}pθ=efθ(s,a), so the D function represent as follow:
D θ ( s , a ) = exp { f θ ( s , a ) } exp { f θ ( s , a ) } + π ( a ∣ s ) D_{\theta}(s, a)=\frac{\exp \left\{f_{\theta}(s, a)\right\}}{\exp \left\{f_{\theta}(s, a)\right\}+\pi(a | s)}Dθ(s,a)=exp{fθ(s,a)}+π(a∣s)exp{fθ(s,a)}
In the end, back to the entropy regularized policy objective, we re-write it in to the new expectation of reward as follow:
r ^ t ( s , a ) = f θ ( s , a ) − log π ( a ∣ s ) = log e f θ ( s , a ) e f θ ( s , a ) + π ( a ∣ s ) − log π ( a ∣ s ) e f θ ( s , a ) + π ( a ∣ s ) = log ( D θ ( s , a ) ) − log ( 1 − D θ ( s , a ) ) \begin{aligned} \hat{r}_{t}(s, a) &=f_{\theta}(s, a)-\log \pi(a | s) \\ &=\log \frac{e^{f_{\theta}(s, a)}}{e^{f_{\theta}(s, a)}+\pi(a | s)}-\log \frac{\pi(a | s)}{e^{f_{\theta}(s, a)}+\pi(a | s)} \\ &=\log \left(D_{\theta}(s, a)\right)-\log \left(1-D_{\theta}(s, a)\right) \end{aligned}r^t(s,a)=fθ(s,a)−logπ(a∣s)=logefθ(s,a)+π(a∣s)efθ(s,a)−logefθ(s,a)+π(a∣s)π(a∣s)=log(Dθ(s,a))−log(1−Dθ(s,a))