update usage instructions
Browse files
README.md
CHANGED
@@ -26,12 +26,37 @@ This is a trained model of a **PPO (Proximal Policy Optimization)** agent playin
|
|
26 |
using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
|
27 |
|
28 |
## Usage (with Stable-baselines3)
|
29 |
-
TODO: Add your code
|
30 |
-
|
31 |
|
32 |
```python
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
37 |
```
|
|
|
26 |
using the [stable-baselines3 library](https://github.com/DLR-RM/stable-baselines3).
|
27 |
|
28 |
## Usage (with Stable-baselines3)
|
|
|
|
|
29 |
|
30 |
```python
|
31 |
+
import gymnasium
|
32 |
+
|
33 |
+
from huggingface_sb3 import load_from_hub, package_to_hub
|
34 |
+
from huggingface_hub import notebook_login # To log to our Hugging Face account to be able to upload models to the Hub.
|
35 |
+
|
36 |
+
from stable_baselines3 import PPO
|
37 |
+
from stable_baselines3.common.env_util import make_vec_env
|
38 |
+
from stable_baselines3.common.evaluation import evaluate_policy
|
39 |
+
from stable_baselines3.common.monitor import Monitor
|
40 |
+
|
41 |
+
repo_id = "xXrobroXx/ppo-LunarLander-v2" # The repo_id
|
42 |
+
filename = "ppo-LunarLander-v2.zip" # The model filename.zip
|
43 |
+
|
44 |
+
# When the model was trained on Python 3.8 the pickle protocol is 5
|
45 |
+
# But Python 3.6, 3.7 use protocol 4
|
46 |
+
# In order to get compatibility we need to:
|
47 |
+
# 1. Install pickle5
|
48 |
+
# 2. Create a custom empty object we pass as parameter to PPO.load()
|
49 |
+
custom_objects = {
|
50 |
+
"learning_rate": 0.0,
|
51 |
+
"lr_schedule": lambda _: 0.0,
|
52 |
+
"clip_range": lambda _: 0.0,
|
53 |
+
}
|
54 |
+
|
55 |
+
checkpoint = load_from_hub(repo_id, filename)
|
56 |
+
model = PPO.load(checkpoint, custom_objects=custom_objects, print_system_info=True)
|
57 |
|
58 |
+
# evaluate model in test environment
|
59 |
+
eval_env = Monitor(gym.make("LunarLander-v2"))
|
60 |
+
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
|
61 |
+
print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
|
62 |
```
|