Topics: Reinforcement Learning
The Policy Gradient is ∇θJ(πθ)∣θk. The goal is to derive an expression for it which can be calculated from samples of the interaction of the agent with the environment. The derivation is divided into 2 parts:
- derive the analytical equation of the policy gradient. It turns out that it has an expectation form E(X)
- derive the equation that estimated the expectation from samples (mean of the possible values of a random variable)
In the following derivation, a stochastic policy πθ and a finite-horizon undiscounted return are considered.
P(τ∣θ)=ρ0(s0)∏t=0TP(st+1∣st,at)πθ(at∣st)
∇θP(τ∣θ)=P(τ∣θ)⋅∇θlog[P(τ∣θ)]
log[P(τ∣θ)]=log(ρ0(s0))+∑t=0T[log(P(st+1∣st,at))+log(πθ(at∣st))]log(a⋅b)=log(a)+log(b)
∇θlog[P(τ∣θ)]=∇θρ0(s0)+t=0∑T[∇θlog(P(st+1∣st,at))+∇θlog(πθ(at∣st))]∇θlog[P(τ∣θ)]=t=0∑T∇θlog(πθ(at∣st))
because the other 2 terms are not a function of θ.
∇θJ(πθ)=∇θτ∼πθE[R(τ)]=∇θ∫τP(τ∣θ)⋅R(τ)expandexpectation=∫τ∇θP(τ∣θ)⋅R(τ)bring∇θinside∫=∫τP(τ∣θ)⋅∇θlog[P(τ∣θ)]⋅R(τ)log−derivativetrick(2)=τ∼πθE[∇θlog[P(τ∣θ)]⋅R(τ)]backtoexpectationform∇θJ(πθ)=τ∼πθE[t=0∑T∇θlog(πθ(at∣st))⋅R(τ)]replaceexpressionforgrad−log−prob(4)
Since ∇θJ(πθ) is an expectation, we can estimate it with a sample mean
g∧=∣D∣1∑τ∈D∑t=0T∇θlog(πθ(at∣st))⋅R(τ)
where D is the number of trajectories collected as samples.
- https://spinningup.openai.com/en/latest/spinningup/rl_intro3.html