Update README.md
Browse files
README.md
CHANGED
|
@@ -1,29 +1,44 @@
|
|
| 1 |
---
|
| 2 |
-
tags:
|
| 3 |
-
- lunarlander-v2
|
| 4 |
-
- ppo
|
| 5 |
-
- deep-reinforcement-learning
|
| 6 |
-
- reinforcement-learning
|
| 7 |
-
- custom-implementation
|
| 8 |
-
- deep-rl-course
|
| 9 |
library_name: pytorch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
# PPO Agent
|
| 13 |
|
| 14 |
-
This is a trained Proximal Policy Optimization (PPO) agent playing
|
| 15 |
|
| 16 |
-
This project was completed as part of the Hugging Face Deep Reinforcement Learning Course, Unit 8 - Part 1.
|
| 17 |
|
| 18 |
-
The PPO agent was implemented from scratch using PyTorch, following the
|
| 19 |
|
| 20 |
## Environment
|
| 21 |
|
| 22 |
-
The agent
|
| 23 |
|
| 24 |
-
The
|
| 25 |
|
| 26 |
-
The
|
| 27 |
|
| 28 |
- Horizontal position
|
| 29 |
- Vertical position
|
|
@@ -43,38 +58,34 @@ The action space contains four discrete actions:
|
|
| 43 |
| 2 | Fire main engine |
|
| 44 |
| 3 | Fire right orientation engine |
|
| 45 |
|
| 46 |
-
##
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
The implementation
|
| 53 |
|
| 54 |
-
-
|
| 55 |
-
-
|
| 56 |
-
-
|
| 57 |
-
-
|
| 58 |
-
-
|
| 59 |
-
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
## PPO Clipped Objective
|
| 62 |
|
| 63 |
-
The probability ratio between the current
|
| 64 |
|
| 65 |
`r_t(theta) = pi_theta(a_t | s_t) / pi_theta_old(a_t | s_t)`
|
| 66 |
|
| 67 |
The PPO clipped objective is:
|
| 68 |
|
| 69 |
-
`L_CLIP = E[min(r_t
|
| 70 |
-
|
| 71 |
-
where:
|
| 72 |
-
|
| 73 |
-
- `A_t` is the advantage estimate.
|
| 74 |
-
- `r_t(theta)` is the probability ratio.
|
| 75 |
-
- `epsilon` is the clipping coefficient.
|
| 76 |
|
| 77 |
-
The clipping coefficient is:
|
| 78 |
|
| 79 |
`epsilon = 0.2`
|
| 80 |
|
|
@@ -84,44 +95,42 @@ Therefore, the clipping range is:
|
|
| 84 |
|
| 85 |
Clipping prevents the policy from making excessively large updates.
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
For a positive advantage, the selected action was better than expected and the policy is encouraged to increase its probability.
|
| 92 |
-
|
| 93 |
-
For a negative advantage, the selected action was worse than expected and the policy is encouraged to decrease its probability.
|
| 94 |
|
| 95 |
## Generalized Advantage Estimation
|
| 96 |
|
| 97 |
-
The implementation uses Generalized Advantage Estimation (GAE).
|
| 98 |
|
| 99 |
The temporal-difference error is:
|
| 100 |
|
| 101 |
`delta_t = r_t + gamma * V(s_t+1) - V(s_t)`
|
| 102 |
|
| 103 |
-
The advantage
|
| 104 |
|
| 105 |
`A_t = delta_t + gamma * lambda * A_t+1`
|
| 106 |
|
| 107 |
-
The
|
| 108 |
|
| 109 |
- Gamma = 0.99
|
| 110 |
- GAE Lambda = 0.95
|
| 111 |
|
| 112 |
-
GAE provides a
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
### Actor
|
| 117 |
|
| 118 |
-
The Actor receives the environment observation and
|
| 119 |
|
| 120 |
Architecture:
|
| 121 |
|
| 122 |
-
`Input -> Linear(64) -> Tanh -> Linear(64) -> Tanh -> Linear(
|
| 123 |
|
| 124 |
-
The output
|
| 125 |
|
| 126 |
### Critic
|
| 127 |
|
|
@@ -131,20 +140,17 @@ Architecture:
|
|
| 131 |
|
| 132 |
`Input -> Linear(64) -> Tanh -> Linear(64) -> Tanh -> Linear(1)`
|
| 133 |
|
| 134 |
-
The value estimate is used to calculate advantages and returns.
|
| 135 |
-
|
| 136 |
## Training Configuration
|
| 137 |
|
| 138 |
-
The PPO implementation provided in the course uses the following default configuration:
|
| 139 |
-
|
| 140 |
| Parameter | Value |
|
| 141 |
| --- | ---: |
|
| 142 |
-
| Environment | LunarLander-
|
| 143 |
| Algorithm | PPO |
|
| 144 |
| Framework | PyTorch |
|
| 145 |
-
|
|
|
|
|
| 146 |
| Learning rate | 0.00025 |
|
| 147 |
-
| Number of environments |
|
| 148 |
| Steps per rollout | 128 |
|
| 149 |
| Minibatches | 4 |
|
| 150 |
| Update epochs | 4 |
|
|
@@ -154,65 +160,36 @@ The PPO implementation provided in the course uses the following default configu
|
|
| 154 |
| Entropy coefficient | 0.01 |
|
| 155 |
| Value function coefficient | 0.5 |
|
| 156 |
| Maximum gradient norm | 0.5 |
|
| 157 |
-
| GAE | Enabled |
|
| 158 |
| Advantage normalization | Enabled |
|
|
|
|
| 159 |
| Learning-rate annealing | Enabled |
|
| 160 |
| Clipped value loss | Enabled |
|
| 161 |
-
|
| 162 |
-
The actual hyperparameters used for this trained model are available in `hyperparameters.txt`.
|
| 163 |
-
|
| 164 |
-
## Training Process
|
| 165 |
-
|
| 166 |
-
The training process follows these steps:
|
| 167 |
-
|
| 168 |
-
1. Create multiple LunarLander-v2 environments.
|
| 169 |
-
2. Collect observations from the environments.
|
| 170 |
-
3. Use the Actor to select actions.
|
| 171 |
-
4. Execute the actions in the environments.
|
| 172 |
-
5. Store observations, actions, rewards, log probabilities, and value estimates.
|
| 173 |
-
6. Calculate advantages using GAE.
|
| 174 |
-
7. Calculate returns.
|
| 175 |
-
8. Calculate the PPO probability ratio.
|
| 176 |
-
9. Apply the PPO clipped surrogate objective.
|
| 177 |
-
10. Calculate the value-function loss and entropy bonus.
|
| 178 |
-
11. Update the Actor and Critic networks.
|
| 179 |
-
12. Repeat the process for the specified number of timesteps.
|
| 180 |
-
|
| 181 |
-
Multiple environments are used in parallel to collect experience efficiently.
|
| 182 |
|
| 183 |
## Evaluation
|
| 184 |
|
| 185 |
-
The trained agent
|
| 186 |
|
| 187 |
-
The evaluation results are stored in `evaluation.txt`
|
| 188 |
|
| 189 |
-
The
|
| 190 |
|
|
|
|
| 191 |
- Mean reward
|
| 192 |
- Standard deviation
|
| 193 |
-
-
|
| 194 |
-
- Environment ID
|
| 195 |
-
- Evaluation date and time
|
| 196 |
|
| 197 |
-
The evaluation
|
| 198 |
|
| 199 |
-
##
|
| 200 |
|
| 201 |
-
|
| 202 |
-
| --- | --- |
|
| 203 |
-
| `model.pt` | Trained PyTorch Actor-Critic model |
|
| 204 |
-
| `hyperparameters.txt` | Training hyperparameters |
|
| 205 |
-
| `evaluation.txt` | Evaluation results |
|
| 206 |
-
| `README.md` | Model card |
|
| 207 |
-
| `replay.mp4` | Replay video of the trained agent, if included |
|
| 208 |
-
|
| 209 |
-
## Loading the Model
|
| 210 |
|
| 211 |
-
The
|
| 212 |
|
| 213 |
-
The
|
| 214 |
|
| 215 |
-
|
| 216 |
|
| 217 |
import torch
|
| 218 |
|
|
@@ -225,50 +202,45 @@ To load the model, recreate the same `Agent` architecture and load the state dic
|
|
| 225 |
|
| 226 |
agent.eval()
|
| 227 |
|
| 228 |
-
|
| 229 |
|
| 230 |
-
|
| 231 |
|
| 232 |
-
|
| 233 |
|
| 234 |
observation, info = env.reset()
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
observation, reward, terminated, truncated, info = env.step(action)
|
| 239 |
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
## Hugging Face Deep Reinforcement Learning Course
|
| 243 |
|
| 244 |
-
This project
|
| 245 |
|
| 246 |
**Hugging Face Deep Reinforcement Learning Course**
|
| 247 |
|
| 248 |
-
**Unit 8 - Part 1: Proximal Policy Optimization (PPO)
|
| 249 |
|
| 250 |
-
The
|
| 251 |
|
| 252 |
-
The
|
| 253 |
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
This project demonstrates:
|
| 257 |
-
|
| 258 |
-
- Implementing PPO from scratch with PyTorch
|
| 259 |
-
- Understanding the PPO clipped objective
|
| 260 |
- Implementing an Actor-Critic architecture
|
| 261 |
-
-
|
| 262 |
-
-
|
| 263 |
-
-
|
| 264 |
-
-
|
| 265 |
-
-
|
| 266 |
|
| 267 |
## References
|
| 268 |
|
| 269 |
-
- Hugging Face Deep Reinforcement Learning Course
|
| 270 |
-
-
|
| 271 |
-
- CleanRL PPO implementation
|
| 272 |
- Gymnasium
|
| 273 |
- PyTorch
|
| 274 |
|
|
@@ -283,14 +255,4 @@ Performance may vary depending on:
|
|
| 283 |
- Hyperparameters
|
| 284 |
- Environment version
|
| 285 |
- Hardware
|
| 286 |
-
- Stochasticity of the environment
|
| 287 |
-
|
| 288 |
-
The model should not be considered an optimal LunarLander-v2 policy.
|
| 289 |
-
|
| 290 |
-
## Summary
|
| 291 |
-
|
| 292 |
-
This project demonstrates the complete PPO pipeline:
|
| 293 |
-
|
| 294 |
-
`LunarLander-v2 -> Actor-Critic -> Experience Collection -> GAE -> PPO Clipping -> Optimization -> Evaluation`
|
| 295 |
-
|
| 296 |
-
The main idea behind PPO is to improve the policy using collected experience while preventing excessively large policy updates.
|
|
|
|
| 1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
library_name: pytorch
|
| 3 |
+
tags:
|
| 4 |
+
- LunarLander-v2
|
| 5 |
+
- deep-reinforcement-learning
|
| 6 |
+
- reinforcement-learning
|
| 7 |
+
- ppo
|
| 8 |
+
- pytorch
|
| 9 |
+
- gymnasium
|
| 10 |
+
- deep-rl-course
|
| 11 |
+
model-index:
|
| 12 |
+
- name: PPO
|
| 13 |
+
results:
|
| 14 |
+
- task:
|
| 15 |
+
type: reinforcement-learning
|
| 16 |
+
name: reinforcement-learning
|
| 17 |
+
dataset:
|
| 18 |
+
name: LunarLander-v2
|
| 19 |
+
type: LunarLander-v2
|
| 20 |
+
metrics:
|
| 21 |
+
- type: mean_reward
|
| 22 |
+
value: -167.32 +/- 88.93
|
| 23 |
+
name: mean_reward
|
| 24 |
+
verified: false
|
| 25 |
---
|
| 26 |
|
| 27 |
+
# **PPO** Agent playing **LunarLander-v2**
|
| 28 |
|
| 29 |
+
This is a trained **Proximal Policy Optimization (PPO)** agent playing **LunarLander-v2**.
|
| 30 |
|
| 31 |
+
This project was completed as part of the **Hugging Face Deep Reinforcement Learning Course, Unit 8 - Part 1**.
|
| 32 |
|
| 33 |
+
The PPO agent was implemented from scratch using **PyTorch** and **Gymnasium**, following the PPO implementation and concepts covered in the course.
|
| 34 |
|
| 35 |
## Environment
|
| 36 |
|
| 37 |
+
The agent was trained on **LunarLander-v2**.
|
| 38 |
|
| 39 |
+
The goal is to learn a policy that controls a lunar lander and successfully lands it on the landing pad while maximizing the cumulative reward.
|
| 40 |
|
| 41 |
+
The environment provides an 8-dimensional observation describing:
|
| 42 |
|
| 43 |
- Horizontal position
|
| 44 |
- Vertical position
|
|
|
|
| 58 |
| 2 | Fire main engine |
|
| 59 |
| 3 | Fire right orientation engine |
|
| 60 |
|
| 61 |
+
## Algorithm
|
| 62 |
|
| 63 |
+
The agent uses **Proximal Policy Optimization (PPO)**.
|
| 64 |
|
| 65 |
+
PPO is an on-policy policy-gradient reinforcement learning algorithm that improves the policy while limiting excessively large policy updates.
|
| 66 |
|
| 67 |
+
The implementation includes:
|
| 68 |
|
| 69 |
+
- Actor-Critic architecture
|
| 70 |
+
- Generalized Advantage Estimation (GAE)
|
| 71 |
+
- PPO clipped surrogate objective
|
| 72 |
+
- Clipped value loss
|
| 73 |
+
- Advantage normalization
|
| 74 |
+
- Entropy regularization
|
| 75 |
+
- Gradient clipping
|
| 76 |
+
- Learning-rate annealing
|
| 77 |
|
| 78 |
## PPO Clipped Objective
|
| 79 |
|
| 80 |
+
The probability ratio between the current and old policies is:
|
| 81 |
|
| 82 |
`r_t(theta) = pi_theta(a_t | s_t) / pi_theta_old(a_t | s_t)`
|
| 83 |
|
| 84 |
The PPO clipped objective is:
|
| 85 |
|
| 86 |
+
`L_CLIP = E[min(r_t A_t, clip(r_t, 1-epsilon, 1+epsilon) A_t)]`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
The clipping coefficient used is:
|
| 89 |
|
| 90 |
`epsilon = 0.2`
|
| 91 |
|
|
|
|
| 95 |
|
| 96 |
Clipping prevents the policy from making excessively large updates.
|
| 97 |
|
| 98 |
+
When the ratio is within the clipping range, the policy can be updated normally.
|
| 99 |
|
| 100 |
+
When the ratio moves outside the range in a direction that would make the policy update excessively large, the clipped objective limits the update.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
## Generalized Advantage Estimation
|
| 103 |
|
| 104 |
+
The implementation uses **Generalized Advantage Estimation (GAE)**.
|
| 105 |
|
| 106 |
The temporal-difference error is:
|
| 107 |
|
| 108 |
`delta_t = r_t + gamma * V(s_t+1) - V(s_t)`
|
| 109 |
|
| 110 |
+
The advantage is estimated recursively using:
|
| 111 |
|
| 112 |
`A_t = delta_t + gamma * lambda * A_t+1`
|
| 113 |
|
| 114 |
+
The implementation uses:
|
| 115 |
|
| 116 |
- Gamma = 0.99
|
| 117 |
- GAE Lambda = 0.95
|
| 118 |
|
| 119 |
+
GAE provides a balance between bias and variance when estimating advantages.
|
| 120 |
+
|
| 121 |
+
## Model Architecture
|
| 122 |
|
| 123 |
+
The PPO agent uses an **Actor-Critic architecture**.
|
| 124 |
|
| 125 |
### Actor
|
| 126 |
|
| 127 |
+
The Actor receives the environment observation and produces action logits.
|
| 128 |
|
| 129 |
Architecture:
|
| 130 |
|
| 131 |
+
`Input -> Linear(64) -> Tanh -> Linear(64) -> Tanh -> Linear(4)`
|
| 132 |
|
| 133 |
+
The output is used to create a categorical probability distribution over the four possible actions.
|
| 134 |
|
| 135 |
### Critic
|
| 136 |
|
|
|
|
| 140 |
|
| 141 |
`Input -> Linear(64) -> Tanh -> Linear(64) -> Tanh -> Linear(1)`
|
| 142 |
|
|
|
|
|
|
|
| 143 |
## Training Configuration
|
| 144 |
|
|
|
|
|
|
|
| 145 |
| Parameter | Value |
|
| 146 |
| --- | ---: |
|
| 147 |
+
| Environment | LunarLander-v3 |
|
| 148 |
| Algorithm | PPO |
|
| 149 |
| Framework | PyTorch |
|
| 150 |
+
| Environment library | Gymnasium |
|
| 151 |
+
| Total timesteps | 100,000 |
|
| 152 |
| Learning rate | 0.00025 |
|
| 153 |
+
| Number of environments | 8 |
|
| 154 |
| Steps per rollout | 128 |
|
| 155 |
| Minibatches | 4 |
|
| 156 |
| Update epochs | 4 |
|
|
|
|
| 160 |
| Entropy coefficient | 0.01 |
|
| 161 |
| Value function coefficient | 0.5 |
|
| 162 |
| Maximum gradient norm | 0.5 |
|
|
|
|
| 163 |
| Advantage normalization | Enabled |
|
| 164 |
+
| GAE | Enabled |
|
| 165 |
| Learning-rate annealing | Enabled |
|
| 166 |
| Clipped value loss | Enabled |
|
| 167 |
+
| Random seed | 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
## Evaluation
|
| 170 |
|
| 171 |
+
The trained agent was evaluated for **10 episodes**.
|
| 172 |
|
| 173 |
+
The evaluation results are stored in `evaluation.txt`.
|
| 174 |
|
| 175 |
+
The file contains:
|
| 176 |
|
| 177 |
+
- Number of evaluation episodes
|
| 178 |
- Mean reward
|
| 179 |
- Standard deviation
|
| 180 |
+
- Individual episode rewards
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
The evaluation result shown at the top of this model card should be replaced with the actual mean reward from `evaluation.txt`.
|
| 183 |
|
| 184 |
+
## Usage
|
| 185 |
|
| 186 |
+
The trained model is stored as `model.pt`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
+
The model contains the PyTorch state dictionary of the trained Actor-Critic agent.
|
| 189 |
|
| 190 |
+
The same `Agent` architecture must be recreated before loading the weights.
|
| 191 |
|
| 192 |
+
Example:
|
| 193 |
|
| 194 |
import torch
|
| 195 |
|
|
|
|
| 202 |
|
| 203 |
agent.eval()
|
| 204 |
|
| 205 |
+
The environment can be created using:
|
| 206 |
|
| 207 |
+
import gymnasium as gym
|
| 208 |
|
| 209 |
+
env = gym.make("LunarLander-v2")
|
| 210 |
|
| 211 |
observation, info = env.reset()
|
| 212 |
|
| 213 |
+
## Repository Contents
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
- `model.pt` - trained PPO Actor-Critic model
|
| 216 |
+
- `hyperparameters.txt` - PPO training hyperparameters
|
| 217 |
+
- `evaluation.txt` - evaluation results
|
| 218 |
+
- `README.md` - model card
|
| 219 |
|
| 220 |
## Hugging Face Deep Reinforcement Learning Course
|
| 221 |
|
| 222 |
+
This project was completed as part of the:
|
| 223 |
|
| 224 |
**Hugging Face Deep Reinforcement Learning Course**
|
| 225 |
|
| 226 |
+
**Unit 8 - Part 1: Proximal Policy Optimization (PPO)**
|
| 227 |
|
| 228 |
+
The project demonstrates the implementation of PPO from scratch and its application to the LunarLander environment.
|
| 229 |
|
| 230 |
+
The main learning objectives include:
|
| 231 |
|
| 232 |
+
- Understanding PPO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
- Implementing an Actor-Critic architecture
|
| 234 |
+
- Collecting experience from multiple environments
|
| 235 |
+
- Implementing Generalized Advantage Estimation
|
| 236 |
+
- Implementing the PPO clipped objective
|
| 237 |
+
- Training and evaluating the agent
|
| 238 |
+
- Sharing the trained model on the Hugging Face Hub
|
| 239 |
|
| 240 |
## References
|
| 241 |
|
| 242 |
+
- Hugging Face Deep Reinforcement Learning Course
|
| 243 |
+
- Proximal Policy Optimization Algorithms - Schulman et al.
|
|
|
|
| 244 |
- Gymnasium
|
| 245 |
- PyTorch
|
| 246 |
|
|
|
|
| 255 |
- Hyperparameters
|
| 256 |
- Environment version
|
| 257 |
- Hardware
|
| 258 |
+
- Stochasticity of the environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|