Update README.md
Browse files
README.md
CHANGED
@@ -25,13 +25,137 @@ model-index:
|
|
25 |
This is a trained model of a **PPO** agent playing **LunarLander-v2**
|
26 |
using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
|
27 |
|
28 |
-
##
|
29 |
-
TODO: Add your code
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
|
|
32 |
```python
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
This is a trained model of a **PPO** agent playing **LunarLander-v2**
|
26 |
using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
|
27 |
|
28 |
+
## Install dependencies and create a virtual screen π½
|
|
|
29 |
|
30 |
+
```python
|
31 |
+
# Virtual display
|
32 |
+
from pyvirtualdisplay import Display
|
33 |
+
|
34 |
+
virtual_display = Display(visible=0, size=(1400, 900))
|
35 |
+
virtual_display.start()
|
36 |
+
```
|
37 |
+
## Import the packages π¦
|
38 |
+
```python
|
39 |
+
import gym
|
40 |
+
|
41 |
+
from huggingface_sb3 import load_from_hub, package_to_hub, push_to_hub
|
42 |
+
from huggingface_hub import notebook_login # To log to our Hugging Face account to be able to upload models to the Hub.
|
43 |
+
|
44 |
+
from stable_baselines3 import PPO
|
45 |
+
from stable_baselines3.common.evaluation import evaluate_policy
|
46 |
+
from stable_baselines3.common.env_util import make_vec_env
|
47 |
+
```
|
48 |
+
## Understand what is Gym and how it works π€
|
49 |
+
```python
|
50 |
+
import gym
|
51 |
+
|
52 |
+
# First, we create our environment called LunarLander-v2
|
53 |
+
env = gym.make("LunarLander-v2")
|
54 |
+
|
55 |
+
# Then we reset this environment
|
56 |
+
observation = env.reset()
|
57 |
+
|
58 |
+
for _ in range(20):
|
59 |
+
# Take a random action
|
60 |
+
action = env.action_space.sample()
|
61 |
+
print("Action taken:", action)
|
62 |
+
|
63 |
+
# Do this action in the environment and get
|
64 |
+
# next_state, reward, done and info
|
65 |
+
observation, reward, done, info = env.step(action)
|
66 |
+
|
67 |
+
# If the game is done (in our case we land, crashed or timeout)
|
68 |
+
if done:
|
69 |
+
# Reset the environment
|
70 |
+
print("Environment is reset")
|
71 |
+
observation = env.reset()
|
72 |
+
```
|
73 |
+
## Create the LunarLander environment π and understand how it works
|
74 |
+
```python
|
75 |
+
# We create our environment with gym.make("<name_of_the_environment>")
|
76 |
+
env = gym.make("LunarLander-v2")
|
77 |
+
env.reset()
|
78 |
+
print("_____OBSERVATION SPACE_____ \n")
|
79 |
+
print("Observation Space Shape", env.observation_space.shape)
|
80 |
+
print("Sample observation", env.observation_space.sample()) # Get a random observation
|
81 |
+
print("\n _____ACTION SPACE_____ \n")
|
82 |
+
print("Action Space Shape", env.action_space.n)
|
83 |
+
print("Action Space Sample", env.action_space.sample()) # Take a random action
|
84 |
+
|
85 |
+
# Create the environment
|
86 |
+
env = make_vec_env('LunarLander-v2', n_envs=16)
|
87 |
+
```
|
88 |
+
## Create the Model π€
|
89 |
+
```python
|
90 |
+
# We added some parameters to accelerate the training
|
91 |
+
model = PPO(
|
92 |
+
policy = 'MlpPolicy',
|
93 |
+
env = env,
|
94 |
+
n_steps = 1024,
|
95 |
+
batch_size = 64,
|
96 |
+
n_epochs = 4,
|
97 |
+
gamma = 0.999,
|
98 |
+
gae_lambda = 0.98,
|
99 |
+
ent_coef = 0.01,
|
100 |
+
verbose=1)
|
101 |
+
model_name = "ppo-LunarLander-v2"
|
102 |
+
```
|
103 |
+
|
104 |
+
## Train the PPO agent π
|
105 |
+
```python
|
106 |
+
# Train it for 1,000,000 timesteps
|
107 |
+
model.learn(total_timesteps=3000000)
|
108 |
+
# Save the model
|
109 |
+
model.save(model_name)
|
110 |
+
```
|
111 |
|
112 |
+
## Evaluate the agent π
|
113 |
```python
|
114 |
+
#load the model
|
115 |
+
model = model.load("/content/ppo-LunarLander-v2.zip")
|
116 |
|
117 |
+
eval_env = gym.make("LunarLander-v2")
|
118 |
+
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
|
119 |
+
print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
|
120 |
+
```
|
121 |
+
## Publish our trained model on the Hub π₯
|
122 |
+
```python
|
123 |
+
notebook_login()
|
124 |
+
!git config --global credential.helper store
|
125 |
```
|
126 |
+
```python
|
127 |
+
import gym
|
128 |
+
|
129 |
+
from stable_baselines3 import PPO
|
130 |
+
from stable_baselines3.common.vec_env import DummyVecEnv
|
131 |
+
from stable_baselines3.common.env_util import make_vec_env
|
132 |
+
|
133 |
+
from huggingface_sb3 import package_to_hub
|
134 |
+
|
135 |
+
# PLACE the variables you've just defined two cells above
|
136 |
+
# Define the name of the environment
|
137 |
+
env_id = "LunarLander-v2"
|
138 |
+
|
139 |
+
# TODO: Define the model architecture we used
|
140 |
+
model_architecture = "PPO"
|
141 |
+
|
142 |
+
## Define a repo_id
|
143 |
+
## repo_id is the id of the model repository from the Hugging Face Hub (repo_id = {organization}/{repo_name} for instance ThomasSimonini/ppo-LunarLander-v2
|
144 |
+
## CHANGE WITH YOUR REPO ID
|
145 |
+
repo_id = "vicfeuga/ppo-LunarLander-v2" # Change with your repo id, you can't push with mine π
|
146 |
+
|
147 |
+
## Define the commit message
|
148 |
+
commit_message = "Upload PPO LunarLander-v2 trained agent"
|
149 |
+
|
150 |
+
# Create the evaluation env
|
151 |
+
eval_env = DummyVecEnv([lambda: gym.make(env_id)])
|
152 |
+
|
153 |
+
# PLACE the package_to_hub function you've just filled here
|
154 |
+
package_to_hub(model=model, # Our trained model
|
155 |
+
model_name=model_name, # The name of our trained model
|
156 |
+
model_architecture=model_architecture, # The model architecture we used: in our case PPO
|
157 |
+
env_id=env_id, # Name of the environment
|
158 |
+
eval_env=eval_env, # Evaluation Environment
|
159 |
+
repo_id=repo_id, # id of the model repository from the Hugging Face Hub (repo_id = {organization}/{repo_name} for instance ThomasSimonini/ppo-LunarLander-v2
|
160 |
+
commit_message=commit_message)
|
161 |
+
```
|