Policy Search Methods
Instead of V V V and Q Q Q functions, we directly estimate the optimal policy.
π θ ( a ∣ s ) ≈ P ( a ∣ s , θ ) \pi^{\theta}(a|s) \approx P(a|s, \theta) π θ ( a ∣ s ) ≈ P ( a ∣ s , θ )
Policy was generated directly from the value function (e.g. ϵ \epsilon ϵ -greedy)
Parameterize the policy directly.
Instead of v ( s , w ) v(s,w) v ( s , w ) or q ^ ( s , a , w ) \hat{q}(s,a,w) q ^ ( s , a , w ) , we have π θ ( a ∣ s ) \pi^{\theta}(a|s) π θ ( a ∣ s )
Given a particular state s s s , what actions should we take? (What is probability of that particular action?)
θ \theta θ is a learnable parameter which could be a neural network.
Value-based and Policy-based Methods
Value-based: Learnt value function, Implicit policy (e.g. ϵ \epsilon ϵ -greedy)
Policy-based: No value function, Learnt policy.
Good convergence properties
Effective in high-dimensional or continuous action spaces
if it is really continuous and large, it is not efficient as well.
Can learn stochastic policies.
Sometimes policies are relatively simple, values and models are complex.
Typically reaches local optima.
Obtained knowledge is specific and does not generalize well.
Actor-Critic: Learnt value function, Learnt policy.
Policy Gradient Techniques
Trust-Region Based: Optimize the policy in a trust zone (closer circle).
Stochastic policies
Rock-Paper-Scissors
A deterministic policy is easily exploited
Deterministic policy: If we follow a set of action or sequence of actions, we will definitely reach the goal and will always reach teh same location.
A uniform random policy is optimal policy.
Grid World
ϕ ( s , a ) = ( 1010 ⏟ w a l l s , 0100 ⏟ a c t i o n s ) \phi(s,a) = \big(\underbrace{1 0 1 0}_{walls}, \underbrace{0 1 0 0}_{actions}\big) ϕ ( s , a ) = ( w a l l s 1010 , a c t i o n s 0100 )
Agent cannot differentiate between the grey states.
ϕ ( s , a ) \phi(s,a) ϕ ( s , a ) features describing state s s s and action a a a .
ϕ ( s , E ) = ( 1 , 0 , 1 , 0 ⏟ NESW , 0 , 1 , 0 , 0 ⏟ NESW ) \phi(s,E) = (\underbrace{1, 0, 1, 0}_{\text{NESW}}, \underbrace{0, 1, 0, 0}_{\text{NESW}}) ϕ ( s , E ) = ( NESW 1 , 0 , 1 , 0 , NESW 0 , 1 , 0 , 0 ) is the North and the South are walls and the agent will move to the East.
If π ( grey ) = W \pi(\text{grey}) = W π ( grey ) = W , then when the agent is in the left grey state, it will move west, get stuck in a loop, and never reach the goal.
Therefore, an optimal stochastic policy moves randomly E E E or W W W in grey states.
π θ ( wall to N and S, move E ) = 0.5 \pi_{\theta}(\text{wall to N and S, move E}) = 0.5 π θ ( wall to N and S, move E ) = 0.5
π θ ( wall to N and S, move W ) = 0.5 \pi_{\theta}(\text{wall to N and S, move W}) = 0.5 π θ ( wall to N and S, move W ) = 0.5
The agent will reach the goal with high probability.
Policy-based methods can learn Stochastic policies.
4563
Objective Function in Policy-based Methods
Given a policy π θ ( a ∣ s ) \pi_{\theta}(a|s) π θ ( a ∣ s ) with parameters θ \theta θ , the goal is to find the best θ \theta θ .
J 1 ( θ ) = v π θ ( S 1 ) J_1(\theta) = v_{\pi_{\theta}}(S_1) J 1 ( θ ) = v π θ ( S 1 )
In an episodic task (with an end state), J 1 ( θ ) J_1(\theta) J 1 ( θ ) is for that particular start state and measures how much expected return we can get.
S 1 S_1 S 1 is the start state.
J ( θ ) J(\theta) J ( θ ) is the measure of the quality of the policy or the objective function to optimize.
J a v V ( θ ) = ∑ s μ π θ ( s ) v π θ ( s ) J_{avV}(\theta) = \sum_{s}\mu_{\pi\theta}(s)v_{\pi\theta}(s) J a v V ( θ ) = s ∑ μ π θ ( s ) v π θ ( s )
In continuing environments (doesn't have an end state), consider the states and take the average value weighted by how often the agent visits each state.
μ π θ ( s ) \mu_{\pi\theta}(s) μ π θ ( s ) is how often the agent is visiting that particular state s s s .
J a v R ( θ ) = ∑ s μ π θ ( s ) ∑ a π θ ( a ∣ s ) ∑ r p ( r ∣ s , a ) r J_{avR}(\theta) = \sum_{s}\mu_{\pi\theta}(s)\sum_{a}\pi_{\theta}(a|s)\sum_{r}p(r|s,a)r J a v R ( θ ) = s ∑ μ π θ ( s ) a ∑ π θ ( a ∣ s ) r ∑ p ( r ∣ s , a ) r
Use the average reward per time step.
Small batches can be used to estimate this average reward during training.
Policy-based Methods
Policy-based RL is an optimization problem, find θ \theta θ that maximize J ( θ ) J(\theta) J ( θ ) .
J ( θ ) J(\theta) J ( θ ) is the policy objective function.
Gradient-free methods: Hill Climbing, Genetic Algorithms, ...
Gradient-based
Δ θ = α ∇ θ J ( θ ) \Delta{\theta} = \alpha \nabla_{\theta} J(\theta) Δ θ = α ∇ θ J ( θ )
Policy gradient method will search for a local minimum in J ( θ ) J(\theta) J ( θ ) by ascending the gradient of the Policy.
why ascending? because we want to maximize the objective function's value/rewards.
∇ θ J ( θ ) = ( ∂ J ( θ ) θ 1 ⋮ ∂ J ( θ ) θ n ) \nabla_{\theta} J(\theta) = \begin{pmatrix} \frac{\partial J(\theta)}{\theta_1} \\ \vdots \\ \frac{\partial J(\theta)}{\theta_n} \end{pmatrix} ∇ θ J ( θ ) = θ 1 ∂ J ( θ ) ⋮ θ n ∂ J ( θ )
α \alpha α is the step size.
Δ θ J ( θ ) = ∇ θ E d [ v π θ ( s ) ] \Delta{\theta} J(\theta) = \nabla_{\theta} \mathbb{E}_d[v_{\pi\theta}(s)] Δ θ J ( θ ) = ∇ θ E d [ v π θ ( s )]
Assume the policy π θ \pi_{\theta} π θ is differentiable.
Compute an estimate of the policy gradient.
Compute Δ θ J ( θ ) \Delta{\theta} J(\theta) Δ θ J ( θ ) using Monte Carlo samples since we need to explore the environment, gather information, and then learn the policy.
Score Function
Assume the policy π θ \pi_{\theta} π θ is differentiable.
∇ θ π θ ( s , a ) \nabla_{\theta} \pi_{\theta}(s, a) ∇ θ π θ ( s , a ) is the gradient.
∇ θ π θ ( s , a ) = π θ ( s , a ) ∇ θ π θ ( s , a ) π θ ( s , a ) = π θ ( s , a ) ∇ θ log π θ ( s , a ) ( ∵ ∇ θ log f ( θ ) = 1 f ( θ ) ∇ θ f ( θ ) ) \begin{aligned}
\nabla_\theta \pi_\theta(s,a)
&=
\pi_\theta(s,a)
\frac{\nabla_\theta \pi_\theta(s,a)}
{\pi_\theta(s,a)}
\\
&=
\pi_\theta(s,a)
\nabla_\theta \log \pi_\theta(s,a)
\quad
\big(
\because \
\nabla_\theta \log f(\theta)
=
\frac{1}{f(\theta)}
\nabla_\theta f(\theta)
\big)
\end{aligned} ∇ θ π θ ( s , a ) = π θ ( s , a ) π θ ( s , a ) ∇ θ π θ ( s , a ) = π θ ( s , a ) ∇ θ log π θ ( s , a ) ( ∵ ∇ θ log f ( θ ) = f ( θ ) 1 ∇ θ f ( θ ) )
Multiplying and dividing by π θ ( s , a ) \pi_\theta(s,a) π θ ( s , a ) is called the Likelihood Ratio Trick .
Since π θ ( s , a ) π θ ( s , a ) = 1 \frac{\pi_\theta(s,a)}{\pi_\theta(s,a)} = 1 π θ ( s , a ) π θ ( s , a ) = 1 , this does not change the original value.
∇ θ π θ ( s , a ) \nabla_\theta \pi_\theta(s,a) ∇ θ π θ ( s , a ) is Gradient of the policy
π θ ( s , a ) \pi_\theta(s,a) π θ ( s , a ) is the policy.
∇ θ π θ ( s , a ) π θ ( s , a ) \frac{\nabla_\theta \pi_\theta(s,a)}{\pi_\theta(s,a)} π θ ( s , a ) ∇ θ π θ ( s , a ) measures how sensitive the policy is to changes in θ \theta θ , relative to the policy value itself.
From the derivative rule of the logarithm, ∇ θ π θ ( s , a ) π θ ( s , a ) = ∇ θ log π θ ( s , a ) \frac{\nabla_\theta \pi_\theta(s,a)}{\pi_\theta(s,a)} = \nabla_\theta \log \pi_\theta(s,a) π θ ( s , a ) ∇ θ π θ ( s , a ) = ∇ θ log π θ ( s , a ) .
Score Function : ∇ θ log π θ ( s , a ) \nabla_\theta \log \pi_\theta(s,a) ∇ θ log π θ ( s , a ) .
Policy Gradient Theorem
Policy Gradient = How policy changes × \times × How good the action is.
It generalizes the likelihood ratio approach to multi-step MDPs.
Replaces the instantaneous reward r ( s , a ) r(s,a) r ( s , a ) with the action value function Q π θ ( s , a ) Q^{\pi\theta}(s,a) Q π θ ( s , a ) .
s 1 → a 1 s 2 → a 2 s 3 → a 3 Goal s_1 \xrightarrow{a_1} s_2 \xrightarrow{a_2} s_3 \xrightarrow{a_3} \text{Goal} s 1 a 1 s 2 a 2 s 3 a 3 Goal
∇ θ J ( θ ) = E π θ [ ∇ θ log π θ ( s , a ) ⏟ Score Function Q π θ ( s , a ) ⏟ Action Value Function ] \
\nabla_\theta J(\theta) = \mathbb{E}_{\pi\theta}[ \underbrace{\nabla_\theta \log \pi_\theta(s,a)}_{\text{Score Function}} \underbrace{Q^{\pi\theta}(s,a)}_{\text{Action Value Function}} ] ∇ θ J ( θ ) = E π θ [ Score Function ∇ θ log π θ ( s , a ) Action Value Function Q π θ ( s , a ) ]
The theorem applies to start state objective, average reward, and average value objectives.
Q π θ ( s , a ) Q^{\pi_\theta}(s,a) Q π θ ( s , a ) : the expected long-term reward starting from state s s s , taking action a a a and following policy π θ \pi_\theta π θ
∇ θ log π θ ( s , a ) \nabla_\theta \log \pi_\theta(s,a) ∇ θ log π θ ( s , a ) : How sensitive the policy is to changes in θ \theta θ .
E π θ [ ⋅ ] \mathbb{E}{\pi\theta}[\cdot] E π θ [ ⋅ ] : Average over all possible states and actions according to the policy π θ \pi_\theta π θ .
∇ θ J ( θ ) \nabla_\theta J(\theta) ∇ θ J ( θ ) : The direction in which θ \theta θ should be changed to increase the objective J ( θ ) J(\theta) J ( θ ) .
Simplified Policy Gradient steps
Loop
Collect trajectories for Policy π θ \pi_\theta π θ
Estimate advantage function A A A
Compute Policy Gradient g ^ \hat{g} g ^
Update Policy Parameter θ new = θ old + α g ^ \theta_{\text{new}} = \theta_{\text{old}} + \alpha \hat{g} θ new = θ old + α g ^
g ^ \hat{g} g ^ is the estimated policy gradient.
Can be MC policy gradient, or Actor-Critic Policy Gradient.
if α \alpha α is too big, data collected under bad policy and it cannot be recovered.
if α \alpha α is too small, it is not efficient use of experience.
REINFORCE Algorithm
Monte Carlo Policy Gradient: explore and learn from the environment and learn the optimal policy.
Update Parameters by Stochastic Gradient Ascent .
Using policy gradient theorem: find the partial derivatives to find the direction of the gradient.
Using return v t v_t v t as an unbiased sample of Q π θ ( s , a ) Q^{\pi\theta}(s,a) Q π θ ( s , a ) .
Q π θ ( s t , a t ) = E π θ [ v t ∣ s t , a t ] Q^{\pi\theta}(s_t,a_t) = \mathbb{E}_{\pi\theta}[v_t | s_t, a_t] Q π θ ( s t , a t ) = E π θ [ v t ∣ s t , a t ]
Δ θ = α ∇ θ log π θ ( s t , a t ) v t \Delta{\theta} = \alpha \nabla_\theta \log \pi_\theta(s_t,a_t) v_t Δ θ = α ∇ θ log π θ ( s t , a t ) v t
Function REINFORCE Initialize θ arbitrarily for each episode { s 1 , a 1 , r 1 , … , s T − 1 , a T − 1 , r T } ∼ π θ do for t = 1 to T − 1 do θ ← θ + α ∇ θ log π θ ( s t , a t ) v t return θ \begin{array}{l}
\textbf{Function REINFORCE}
\\
\qquad \text{Initialize } \theta \text{ arbitrarily}
\\
\qquad \textbf{for each episode }
\{s_1,a_1,r_1,\ldots,s_{T-1},a_{T-1},r_T\}
\sim \pi_\theta
\textbf{ do}
\\
\qquad\qquad \textbf{for } t=1 \textbf{ to } T-1 \textbf{ do}
\\
\qquad\qquad\qquad
\theta
\leftarrow
\theta
+
\alpha
\nabla_\theta
\log \pi_\theta(s_t,a_t)
v_t
\\
\qquad \textbf{return } \theta
\\
\end{array} Function REINFORCE Initialize θ arbitrarily for each episode { s 1 , a 1 , r 1 , … , s T − 1 , a T − 1 , r T } ∼ π θ do for t = 1 to T − 1 do θ ← θ + α ∇ θ log π θ ( s t , a t ) v t return θ
REINFORCE: Sutton and Barto
Input: a differentiable policy parameterization π ( a ∣ s , θ ) Algorithm parameter: step size α > 0 Initialize policy parameter θ ∈ R d ′ (e.g., to 0) Loop forever (for each episode): Generate an episode S 0 , A 0 , R 1 , … , S T − 1 , A T − 1 , R T , following π ( ⋅ ∣ ⋅ , θ ) Loop for each step of the episode t = 0 , 1 , … , T − 1 : G ← ∑ k = t + 1 T γ k − t − 1 R k ( G t ) θ ← θ + α γ t G ∇ ln π ( A t ∣ S t , θ ) \begin{array}{l}
\textbf{Input: } \text{a differentiable policy parameterization } \pi(a \mid s,\theta)
\\
\textbf{Algorithm parameter: } \text{step size } \alpha > 0
\\
\textbf{Initialize } \text{policy parameter } \theta \in \mathbb{R}^{d'} \text{ (e.g., to 0)}
\\
\\
\textbf{Loop forever (for each episode):}
\\
\qquad \text{Generate an episode }
S_0,A_0,R_1,\ldots,S_{T-1},A_{T-1},R_T,
\text{ following } \pi(\cdot \mid \cdot,\theta)
\\
\qquad \textbf{Loop for each step of the episode } t=0,1,\ldots,T-1:
\\
\qquad\qquad
G
\leftarrow
\sum_{k=t+1}^{T}
\gamma^{k-t-1}R_k
\qquad (G_t)
\\
\qquad\qquad
\theta
\leftarrow
\theta
+
\alpha
\gamma^t
G
\nabla
\ln \pi(A_t \mid S_t,\theta)
\end{array} Input: a differentiable policy parameterization π ( a ∣ s , θ ) Algorithm parameter: step size α > 0 Initialize policy parameter θ ∈ R d ′ (e.g., to 0) Loop forever (for each episode): Generate an episode S 0 , A 0 , R 1 , … , S T − 1 , A T − 1 , R T , following π ( ⋅ ∣ ⋅ , θ ) Loop for each step of the episode t = 0 , 1 , … , T − 1 : G ← ∑ k = t + 1 T γ k − t − 1 R k ( G t ) θ ← θ + α γ t G ∇ ln π ( A t ∣ S t , θ )
Slow to converge.
Going to have low bias but high variance.
Actor-Critic
Actor has a supervisor (Critic) that tells it how good or bad the action actor took is.
Actor : Controls agent's behavior using Policy-based methods.
Critic : Measures how good the action taken by the agent is using Value-based methods.
Reducing Variance using a Critic
Q w ( s , a ) ≈ Q π θ ( s , a ) Q_w(s,a) \approx Q^{\pi\theta}(s,a) Q w ( s , a ) ≈ Q π θ ( s , a )
Monte Carlo policy gradient has high variance.
Use a Critic to estimate the action-value function.
Actor-Critic algorithms maintains two sets of parameters:
Critic : Updates action-value function parameters w w w .
It is solving the problem of policy evaluation.
How good is the policy π θ \pi_\theta π θ for current parameters θ \theta θ ?
MC or TD methods to estimate the value function.
Actor : Updates policy parameters θ \theta θ in the direction advised by the Critic.
Δ θ J ( θ ) = E π θ [ ∇ θ log π θ ( s , a ) Q w ( s , a ) ] Δ θ = α ∇ θ log π θ ( s , a ) Q w ( s , a ) \Delta_{\theta} J(\theta) = \mathbb{E}_{\pi\theta}[ \nabla_\theta \log \pi_\theta(s,a) Q_w(s,a) ] \\
\Delta_{\theta} = \alpha \nabla_\theta \log \pi_\theta(s,a) Q_w(s,a) Δ θ J ( θ ) = E π θ [ ∇ θ log π θ ( s , a ) Q w ( s , a )] Δ θ = α ∇ θ log π θ ( s , a ) Q w ( s , a )
It follows an approximate policy gradient.
Action-Value Actor-Critic
Using Linear value function approximation: Q w ( s , a ) = ϕ ( s , a ) T w Q_w(s,a) = \phi(s,a)^T w Q w ( s , a ) = ϕ ( s , a ) T w
Critic : Use Linear TD(0) to update w w w .
Actor : Use policy gradient to update θ \theta θ .
Function Actor-Critic Initialize θ , s Sample a ∼ π θ for each step do Sample reward r = R s a Sample transition s ′ ∼ P s a Sample action a ′ ∼ π θ ( s ′ , a ′ ) δ = r + γ Q w ( s ′ , a ′ ) − Q w ( s , a ) θ = θ + α ∇ θ log π θ ( s , a ) Q w ( s , a ) w ← w + β δ ϕ ( s , a ) a ← a ′ , s ← s ′ \begin{array}{l}
\textbf{Function Actor-Critic}
\\
\qquad \text{Initialize } \theta, s
\\
\qquad \text{Sample } a \sim \pi_\theta
\\
\\
\qquad \textbf{for each step do}
\\
\qquad\qquad \text{Sample reward } r = R_s^a
\\
\qquad\qquad\text{Sample transition } s' \sim P_s^a
\\
\qquad\qquad \text{Sample action } a' \sim \pi_\theta(s',a')
\\
\\
\qquad\qquad \delta = r + \gamma Q_w(s',a') - Q_w(s,a)
\\
\qquad\qquad \theta = \theta + \alpha \nabla_\theta \log \pi_\theta(s,a) Q_w(s,a)
\\
\\
\qquad\qquad \mathbf{w} \leftarrow \mathbf{w} + \beta \delta \phi(s,a)
\\
\qquad\qquad a \leftarrow a',\; s \leftarrow s'
\end{array} Function Actor-Critic Initialize θ , s Sample a ∼ π θ for each step do Sample reward r = R s a Sample transition s ′ ∼ P s a Sample action a ′ ∼ π θ ( s ′ , a ′ ) δ = r + γ Q w ( s ′ , a ′ ) − Q w ( s , a ) θ = θ + α ∇ θ log π θ ( s , a ) Q w ( s , a ) w ← w + β δ ϕ ( s , a ) a ← a ′ , s ← s ′
δ \delta δ is Critic.
θ \theta θ is Actor.
Update w w w : Critic can provide a better advice.
Actor-Critic: Sutton and Barto
Input: a differentiable policy parameterization π ( a ∣ s , θ ) Input: a differentiable state-value function parameterization v ^ ( s , w ) Parameters: step sizes α θ > 0 , α w > 0 Initialize policy parameter θ ∈ R d ′ and state-value weights w ∈ R d (e.g., to 0) Loop forever (for each episode): Initialize S (first state of episode) I ← 1 Loop while S is not terminal (for each time step): A ∼ π ( ⋅ ∣ S , θ ) Take action A , observe S ′ , R δ ← R + γ v ^ ( S ′ , w ) − v ^ ( S , w ) ( if S ′ is terminal, then v ^ ( S ′ , w ) ≐ 0 ) w ← w + α w δ ∇ v ^ ( S , w ) θ ← θ + α θ I δ ∇ ln π ( A ∣ S , θ ) I ← γ I S ← S ′ \begin{array}{l}
\textbf{Input: } \text{a differentiable policy parameterization } \pi(a \mid s,\theta)
\\
\textbf{Input: } \text{a differentiable state-value function parameterization } \hat{v}(s,\mathbf{w})
\\
\textbf{Parameters: } \text{step sizes } \alpha^\theta > 0,\; \alpha^{\mathbf{w}} > 0
\\
\textbf{Initialize } \text{policy parameter } \theta \in \mathbb{R}^{d'}
\text{ and state-value weights } \mathbf{w} \in \mathbb{R}^{d}
\text{ (e.g., to 0)}
\\
\textbf{Loop forever (for each episode):}
\\
\qquad \text{Initialize } S \text{ (first state of episode)}
\\
\qquad I \leftarrow 1
\\
\qquad \textbf{Loop while } S \textbf{ is not terminal (for each time step):}
\\
\qquad\qquad A \sim \pi(\cdot \mid S,\theta)
\\
\qquad\qquad \text{Take action } A,\; \text{observe } S',R
\\
\qquad\qquad
\delta
\leftarrow
R
+
\gamma \hat{v}(S',\mathbf{w})
-
\hat{v}(S,\mathbf{w})
\qquad
\left(
\text{if } S' \text{ is terminal, then } \hat{v}(S',\mathbf{w}) \doteq 0
\right)
\\
\qquad\qquad
\mathbf{w}
\leftarrow
\mathbf{w}
+
\alpha^{\mathbf{w}}
\delta
\nabla \hat{v}(S,\mathbf{w})
\\
\qquad\qquad
\theta
\leftarrow
\theta
+
\alpha^\theta
I
\delta
\nabla
\ln \pi(A \mid S,\theta)
\\
\qquad\qquad
I
\leftarrow
\gamma I
\\
\qquad\qquad
S
\leftarrow
S'
\end{array} Input: a differentiable policy parameterization π ( a ∣ s , θ ) Input: a differentiable state-value function parameterization v ^ ( s , w ) Parameters: step sizes α θ > 0 , α w > 0 Initialize policy parameter θ ∈ R d ′ and state-value weights w ∈ R d (e.g., to 0) Loop forever (for each episode): Initialize S (first state of episode) I ← 1 Loop while S is not terminal (for each time step): A ∼ π ( ⋅ ∣ S , θ ) Take action A , observe S ′ , R δ ← R + γ v ^ ( S ′ , w ) − v ^ ( S , w ) ( if S ′ is terminal, then v ^ ( S ′ , w ) ≐ 0 ) w ← w + α w δ ∇ v ^ ( S , w ) θ ← θ + α θ I δ ∇ ln π ( A ∣ S , θ ) I ← γ I S ← S ′
TRPO, Trust Region Policy Optimization
A ( s , a ) = Q ( s , a ) − V ( s ) A(s,a) = Q(s,a) - V(s) A ( s , a ) = Q ( s , a ) − V ( s )
Advantage : the difference between the Q and the V values.
How good is an action compared to the average action for a specific state.
Q ( s , a ) Q(s,a) Q ( s , a ) = the expected long-term return when taking action a a a in state s s s
V ( s ) V(s) V ( s ) = the expected average long-term return when following the policy from state s s s
Used in many Deep RL algorithms such as TRPO, PPO, GRP and etc.
Stationary Target: Fixed target during training.
Update the old policy to a new policy such that they are with in a "Trusted" distance apart.
Conservative policy update allows improvement instead of degradation of policy.
Improve training stability by avoiding parameter updates that changes the policy too much at one step.
By enforcing KL divergence constraint on size of the policy update at each iteration.
Kullback-Leibler Divergence
KL divergence score
K L ( P ∣ ∣ Q ) KL(P || Q) K L ( P ∣∣ Q )
It quantifies how much one probability distribution differs from another probability distribution.
∣ ∣ || ∣∣ indicates divergence or P P P 's divergence from Q Q Q .
D K L ( P ∣ ∣ Q ) = ∑ x ∈ X P ( x ) log P ( x ) Q ( x ) D_{KL}(P || Q) = \sum_{x \in X} P(x) \log \frac{P(x)}{Q(x)} D K L ( P ∣∣ Q ) = ∑ x ∈ X P ( x ) log Q ( x ) P ( x )
Given two probability distributions P P P and Q Q Q over a discrete random variable X X X , the KL divergence is the expected value under P P P of the log ratio between P P P and Q Q Q .
Multiplying by P ( x ) P(x) P ( x ) gives more weight to events that occur frequently under P P P .
P P P large, Q Q Q small → large divergence
P P P small, Q Q Q large → effect is smaller
e.g. π old ( a 1 ∣ s ) = 0.8 \pi_{\text{old}}(a_1|s) = 0.8 π old ( a 1 ∣ s ) = 0.8 and π old ( a 2 ∣ s ) = 0.2 \pi_{\text{old}}(a_2|s) = 0.2 π old ( a 2 ∣ s ) = 0.2 , while π new ( a 1 ∣ s ) = 0.1 \pi_{\text{new}}(a_1|s) = 0.1 π new ( a 1 ∣ s ) = 0.1 and π new ( a 2 ∣ s ) = 0.9 \pi_{\text{new}}(a_2|s) = 0.9 π new ( a 2 ∣ s ) = 0.9
The policy changed drastically in one update step.
TRPO prevents this by enforcing D K L ( π old ∥ π new ) ≤ δ D_{KL}(\pi_{\text{old}} \| \pi_{\text{new}}) \leq \delta D K L ( π old ∥ π new ) ≤ δ .
TRPO vs Gradient Descent
Comparison Gradient Descent Trust Region Optimization approach Picks the steepest direction. Defines a region in which to search for the next point. Step Moves forward by a step size. Determines the maximum step size to explore. Next point Moves directly in the selected direction. Locates the optimal point within the trust region. Search process Repeats the directional step. Resumes the search from the selected point. Main characteristic Fast and simple for optimizing an objective function. Controls how far the optimization explores at each step. In Reinforcement Learning May not work well in reinforcement learning. Provides a restricted region for finding the next optimal point.
PPO, Proximal Policy Optimization
TRPO is relatively complicated, every time we need to find trust region and solve the optimization problem, leading to high computational cost.
PPO simplifies it by using a Clipped Surrogate function
Based on a ratio of two policies
Current Policy π θ \pi_{\theta} π θ
Baseline Policy (Old Policy) π θ old \pi_{\theta_{\text{old}}} π θ old
Policy Ratio
r t ( θ ) = π θ ( a t ∣ s t ) π θ old ( a t ∣ s t ) r_t(\theta)
=
\frac{\pi_\theta(a_t|s_t)}
{\pi_{\theta_{\text{old}}}(a_t|s_t)} r t ( θ ) = π θ old ( a t ∣ s t ) π θ ( a t ∣ s t )
r t ( θ ) = 1 r_t(\theta)=1 r t ( θ ) = 1 : no policy change
r t ( θ ) > 1 r_t(\theta)>1 r t ( θ ) > 1 : the action becomes more likely
r t ( θ ) < 1 r_t(\theta)<1 r t ( θ ) < 1 : the action becomes less likely
The surrogate objective is:
J ( θ ) = E t [ r t ( θ ) A ^ t ] J(\theta)
=
\mathbb{E}_t
\left[
r_t(\theta)\hat A_t
\right] J ( θ ) = E t [ r t ( θ ) A ^ t ]
A ^ t > 0 \hat A_t>0 A ^ t > 0 : the action was better than expected → increase its probability.
A ^ t < 0 \hat A_t<0 A ^ t < 0 : the action was worse than expected → decrease its probability.
Without a restriction, the policy may change too much in a single update.
Clipped Surrogate Objective
J C L I P ( θ ) = E ^ t [ min ( r t ( θ ) A ^ t ⏟ Normal Policy Gradient objective , clip ( r t ( θ ) , 1 − ϵ , 1 + ϵ ) ⏟ Clipped Policy Gradient Objective A ^ t ) ] J^{CLIP}(\theta)
=
\hat{\mathbb E}_t
\left[
\min
(
\underbrace{r_t(\theta)\hat A_t}_{\text{Normal Policy Gradient objective}},
\underbrace{\operatorname{clip}
(r_t(\theta),1-\epsilon,1+\epsilon)}_{\text{Clipped Policy Gradient Objective}}\hat A_t
)
\right] J C L I P ( θ ) = E ^ t min ( Normal Policy Gradient objective r t ( θ ) A ^ t , Clipped Policy Gradient Objective clip ( r t ( θ ) , 1 − ϵ , 1 + ϵ ) A ^ t )
r t ( θ ) = π θ ( a t ∣ s t ) π θ old ( a t ∣ s t ) r_t(\theta) = \frac{\pi_\theta(a_t|s_t)}{\pi_{\theta_{\text{old}}}(a_t|s_t)} r t ( θ ) = π θ old ( a t ∣ s t ) π θ ( a t ∣ s t ) is the ratio of the new policy to the old policy.
A ^ t = Q ( s t , a t ) − V ( s t ) \hat A_t = Q(s_t,a_t) - V(s_t) A ^ t = Q ( s t , a t ) − V ( s t ) is the advantage function.
ϵ \epsilon ϵ is a hyperparameter, usually 0.1 0.1 0.1 or 0.2 0.2 0.2 .
If ϵ = 0.2 \epsilon=0.2 ϵ = 0.2 , the clipping range is [ 0.8 , 1.2 ] [0.8,1.2] [ 0.8 , 1.2 ] .
For A ^ t > 0 \hat A_t>0 A ^ t > 0 , PPO removes the benefit of increasing r t r_t r t above 1 + ϵ 1+\epsilon 1 + ϵ .
For A ^ t < 0 \hat A_t<0 A ^ t < 0 , PPO removes the benefit of decreasing r t r_t r t below 1 − ϵ 1-\epsilon 1 − ϵ .
The policy ratio itself is not strictly forced to remain inside the clipping range.
Instead, clipping removes the incentive for excessively large beneficial policy updates.
Algorithm: PPO with Clipped Objective Input: θ 0 , ϵ for k = 0 , 1 , 2 , … do Collect set of partial trajectories D k on policy π k = π ( θ k ) Estimate advantages A ^ t π k using any advantage estimation algorithm Compute policy update θ k + 1 = arg max θ L θ k C L I P ( θ ) by taking K steps of minibatch SGD (via Adam), where L θ k C L I P ( θ ) = E τ ∼ π k [ ∑ t = 0 T min ( r t ( θ ) A ^ t π k , clip ( r t ( θ ) , 1 − ϵ , 1 + ϵ ) A ^ t π k ) ] \begin{array}{l}
\textbf{Algorithm: PPO with Clipped Objective} \\
\textbf{Input: } \theta_0,\ \epsilon \\
\textbf{for } k=0,1,2,\ldots \textbf{ do} \\
\qquad \text{Collect set of partial trajectories } \mathcal{D}_k
\text{ on policy } \pi_k=\pi(\theta_k) \\
\qquad \text{Estimate advantages } \hat{A}_t^{\pi_k}
\text{ using any advantage estimation algorithm} \\
\\
\qquad \text{Compute policy update} \\
\qquad \theta_{k+1}
=
\arg\max_{\theta}
\mathcal{L}_{\theta_k}^{CLIP}(\theta) \\
\\
\qquad \text{by taking } K \text{ steps of minibatch SGD (via Adam), where} \\
\qquad
\mathcal{L}_{\theta_k}^{CLIP}(\theta)
=
\mathbb{E}_{\tau\sim\pi_k}
\left[
\sum_{t=0}^{T}
\min
\left(
r_t(\theta)\hat{A}_t^{\pi_k},
\operatorname{clip}
\left(
r_t(\theta),1-\epsilon,1+\epsilon
\right)
\hat{A}_t^{\pi_k}
\right)
\right] \\
\end{array} Algorithm: PPO with Clipped Objective Input: θ 0 , ϵ for k = 0 , 1 , 2 , … do Collect set of partial trajectories D k on policy π k = π ( θ k ) Estimate advantages A ^ t π k using any advantage estimation algorithm Compute policy update θ k + 1 = arg max θ L θ k C L I P ( θ ) by taking K steps of minibatch SGD (via Adam), where L θ k C L I P ( θ ) = E τ ∼ π k [ ∑ t = 0 T min ( r t ( θ ) A ^ t π k , clip ( r t ( θ ) , 1 − ϵ , 1 + ϵ ) A ^ t π k ) ]
θ 0 \theta_0 θ 0 : Initial policy parameters.
ϵ \epsilon ϵ : Clipping threshold.
A ^ t π k \hat{A}_t^{\pi_k} A ^ t π k : A π ( s , a ) = Q π ( s , a ) − V π ( s ) A_\pi(s,a)=Q_\pi(s,a)-V_\pi(s) A π ( s , a ) = Q π ( s , a ) − V π ( s ) , where a t ∼ π ( a t ∣ s t ) , s t + 1 ∼ P ( s t + 1 ∣ s t , a t ) a_t\sim\pi(a_t\mid s_t),\quad
s_{t+1}\sim P(s_{t+1}\mid s_t,a_t) a t ∼ π ( a t ∣ s t ) , s t + 1 ∼ P ( s t + 1 ∣ s t , a t ) , for t ≥ 0 t\ge0 t ≥ 0 .