⚠️ A upcoming updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introduction.
This article is part of the Deep Reinforcement Learning Class. A free course from beginner to expert. Check the syllabus here. (Related: What Is Considered Upper Middle Class? Income and Net W...)
⚠️ A new updated version of this article is available here 👉 https://huggingface.co/deep-rl-course/unit1/introduction.
This article is part of the Deep Reinforcement Learning Class. A free course from beginner to expert. Check the syllabus here. (Related: Virgin Atlantic → ANA "The Room" Business Cla...)
In the last Unit, we learned about Advantage Actor Critic (A2C), a hybrid architecture combining value-based and policy-based methods that help to stabilize the training by reducing the variance with:.
Sources indicate that today we'll learn about Proximal Policy Optimization (PPO), an architecture that improves our agent's training stability by avoiding too large policy updates. To do that, we use a ratio that will indicates the difference between our current and old policy and clip this ratio from a specific range [1−ϵ,1+ϵ] [1 - \epsilon, 1 + \epsilon] [1−ϵ,1+ϵ] .
Doing this will ensure that our policy upgrade will not be too large and that the training is more stable.
And then, after the theory, we'll code a PPO architecture from scratch using PyTorch and bulletproof our implementation with CartPole-v1 and LunarLander-v2.
It has been revealed that sounds exciting? Let's get started!
The idea with Proximal Policy Optimization (PPO) is that we want to improve the training stability of the policy by limiting the change you make to the policy at each training epoch: we want to avoid having too large policy updates.
Industry observers note that so with PPO, we upgrade the policy conservatively. To do so, we need to measure how much the current policy changed compared to the former one using a ratio calculation between the current and former policy. And we clip this ratio in a range [1−ϵ,1+ϵ] [1 - \epsilon, 1 + \epsilon] [1−ϵ,1+ϵ], meaning that we remove the incentive for the current policy to go too far from the old one (hence the proximal policy term).
Let’s remember what is the objective to optimize in Reinforce:.
The idea was that by taking a gradient ascent step on this function (equivalent to taking gradient descent of the negative of this function), we would push our agent to take actions that lead to higher rewards and avoid harmful actions.
However, the problem comes from the step size:.
Here with PPO, the idea is to constrain our policy update with a newly introduced objective function called the Clipped surrogate objective function that will constrain the policy change in a small range using a clip.
This new function is designed to avoid destructive large weights updates :.
Let’s study each part to understand how it works.
This ratio is calculated this way:.
It’s the probability of taking action at a_t at at state st s_t st in the current policy divided by the previous one.
As we can see, rt(θ) r_t(\theta) rt(θ) denotes the probability ratio between the current and old policy:.
So this probability ratio is an easy way to estimate the divergence between old and current policy.
Sources indicate that this ratio can replace the log probability we use in the policy objective function. This gives us the left part of the new objective function: multiplying the ratio by the advantage.
However, without a constraint, if the action taken is much more probable in our current policy than in our former, this would lead to a significant policy gradient step and, therefore, an excessive policy enhancement.
Consequently, we need to constrain this objective function by penalizing changes that lead to a ratio away from 1 (in the paper, the ratio can only vary from 0.8 to 1.2).
By clipping the ratio, we ensure that we do not have a too large policy update because the current policy can't be too different from the older one.
To do that, we have two solutions:.
This clipped part is a version where rt(theta) is clipped between [1−ϵ,1+ϵ] [1 - \epsilon, 1 + \epsilon] [1−ϵ,1+ϵ].
With the Clipped Surrogate Objective function, we have two probability ratios, one non-clipped and one clipped in a range (between [1−ϵ,1+ϵ] [1 - \epsilon, 1 + \epsilon] [1−ϵ,1+ϵ], epsilon is a hyperparameter that helps us to define this clip range (in the paper ϵ=0.2 \epsilon = 0.2 ϵ=0.2.).
Then, we take the minimum of the clipped and non-clipped objective, so the final objective is a lower bound (pessimistic bound) of the unclipped objective.
Taking the minimum of the clipped and non-clipped objective means we'll select either the clipped or the non-clipped objective based on the ratio and advantage situation.
It has been revealed that don't worry. It's normal if this seems complex to handle right now. But we're going to see what this Clipped Surrogate Objective Function looks like, and this will help you to visualize better what's going on.
We have six different situations. Remember first that we take the minimum between the clipped and unclipped objectives.
In situations 1 and 2, the clipping does not apply since the ratio is between the range [1−ϵ,1+ϵ] [1 - \epsilon, 1 + \epsilon] [1−ϵ,1+ϵ].
According to recent reports, in situation 1, we have a positive advantage: the action is better than the average of all the actions in that state. Therefore, we should encourage our current policy to increase the probability of taking that action in that state.
Since the ratio is between intervals, we can increase our policy's probability of taking that action at that state.
In situation 2, we have a negative advantage: the action is worse than the average of all actions at that state. Therefore, we should discourage our current policy from taking that action in that state.
Since the ratio is between intervals, we can decrease the probability that our policy takes that action at that state.
If the probability ratio is lower than [1−ϵ] [1 - \epsilon] [1−ϵ], the probability of taking that action at that state is much lower than with the old policy.
If, like in situation 3, the advantage estimate is positive (A>0), then you want to increase the probability of taking that action at that state.
But if, like situation 4, the advantage estimate is negative, we don't want to decrease further the probability of taking that action at that state. Therefore, the gradient is = 0 (since we're on a flat line), so we don't update our weights.
If the probability ratio is higher than [1+ϵ] [1 + \epsilon] [1+ϵ], the probability of taking that action at that state in the current policy is much higher than in the former policy.
Industry observers note that if, like in situation 5, the advantage is positive, we don't want to get too greedy. We already have a higher probability of taking that action at that state than the former policy. Therefore, the gradient is = 0 (since we're on a flat line), so we don't update our weights.
If, like in situation 6, the advantage is negative, we want to decrease the probability of taking that action at that state.
So if we recap, we only enhancement the policy with the unclipped objective part. When the minimum is the clipped objective part, we don't update our policy weights since the gradient will equal 0.
So we patch our policy only if:.
You might wonder why, when the minimum is the clipped ratio, the gradient is 0. When the ratio is clipped, the derivative in this case will not be the derivative of the rt(θ)∗At r_t(\theta) * A_t rt(θ)∗At but the derivative of either (1−ϵ)∗At (1 - \epsilon)* A_t(1−ϵ)∗At or the derivative of (1+ϵ)∗At (1 + \epsilon)* A_t(1+ϵ)∗At which both = 0.
Industry observers note that to summarize, thanks to this clipped surrogate objective, we restrict the range that the current policy can vary from the old one. Because we remove the incentive for the probability ratio to move outside of the interval since, the clip have the effect to gradient. If the ratio is > 1+ϵ 1 + \epsilon 1+ϵ or < 1−ϵ 1 - \epsilon 1−ϵ the gradient will be equal to 0.
The final Clipped Surrogate Objective Loss for PPO Actor-Critic style looks like this, it's a combination of Clipped Surrogate Objective function, Value Loss Function and Entropy bonus:.
That was quite complex. Take time to understand these situations by looking at the table and the graph. You must understand why this makes sense. If you want to go deeper, the best resource is the article Towards Delivering a Coherent Self-Contained Explanation of Proximal Policy Optimization" by Daniel Bick, especially part 3.4.
Now that we studied the theory behind PPO, the best way to understand how it works is to implement it from scratch.
In a recent development, implementing an architecture from scratch is the best way to understand it, and it's a good habit. We have already done it for a value-based method with Q-Learning and a Policy-based method with Reinforce.
So, to be able to code it, we're going to use two resources:.
Then, to test its robustness, we're going to train it in 2 different classical environments:.
And finally, we will be push the trained model to the Hub to evaluate and visualize your agent playing.
Industry observers note that lunarLander-v2 is the first environment you used when you started this course. At that time, you didn't know how it worked, and now, you can code it from scratch and train it. How incredible is that 🤩.
Start the tutorial here 👉 https://github.com/huggingface/deep-rl-class/blob/main/unit8/unit8.ipynb.
It has been revealed that congrats on finishing this chapter! There was a lot of information. And congrats on finishing the tutorial. 🥳, this was one of the hardest of the course.
Don't hesitate to train your agent in other environments. The best way to learn is to try things on your own!
I want you to think about your progress since the first Unit. With these eight units, you've built a strong background in Deep Reinforcement Learning. Congratulations!
But this is not the end, even if the foundations part of the course is finished, this is not the end of the journey. We're working on latest elements:.
And don't forget to share with your friends who want to learn 🤗!
Recent findings suggest that finally, with your feedback, we want to improve and patch the course iteratively. If you have some, please fill this form 👉 https://forms.gle/3HgA7bEHwAmmLfwh9.