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