Views
No views yet
1import gymnasium
2from stable_baselines3 import PPO
3from stable_baselines3.common.env_util import make_vec_env
4from stable_baselines3.common.evaluation import evaluate_policy
5from stable_baselines3.common.monitor import Monitor
6from huggingface_sb3 import load_from_hub
7
8repo_id = "AdanLee/ppo-LunarLander-v2" # The repo_id
9filename = "ppo-LunarLander-v2.zip" # The model filename.zip
10
11# When the model was trained on Python 3.8 the pickle protocol is 5
12# But Python 3.6, 3.7 use protocol 4
13# In order to get compatibility we need to:
14# 1. Install pickle5 (we done it at the beginning of the colab)
15# 2. Create a custom empty object we pass as parameter to PPO.load()
16custom_objects = {
17 "learning_rate": 0.0,
18 "lr_schedule": lambda _: 0.0,
19 "clip_range": lambda _: 0.0,
20}
21
22checkpoint = load_from_hub(repo_id, filename)
23model = PPO.load(checkpoint, custom_objects=custom_objects, print_system_info=True)
24
25eval_env = Monitor(gym.make("LunarLander-v2"))
26mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
27print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
28...