Views
No views yet
pip install stable-baselines3
pip install huggingface_sb31
2import gym
3import pybullet_envs
4
5from huggingface_sb3 import load_from_hub
6
7from stable_baselines3 import PPO
8from stable_baselines3.common.vec_env import DummyVecEnv, VecNormalize
9from stable_baselines3.common.evaluation import evaluate_policy
10
11# Retrieve the model from the hub
12## repo_id = id of the model repository from the Hugging Face Hub (repo_id = {organization}/{repo_name})
13## filename = name of the model zip file from the repository
14repo_id = "ThomasSimonini/ppo-AntBulletEnv-v0"
15checkpoint = load_from_hub(repo_id = repo_id, filename="ppo-AntBulletEnv-v0.zip")
16model = PPO.load(checkpoint)
17
18# Load the saved statistics
19stats_path = load_from_hub(repo_id = repo_id, filename="vec_normalize.pkl")
20
21eval_env = DummyVecEnv([lambda: gym.make("AntBulletEnv-v0")])
22eval_env = VecNormalize.load(stats_path, eval_env)
23# do not update them at test time
24eval_env.training = False
25# reward normalization is not needed at test time
26eval_env.norm_reward = False
27
28from stable_baselines3.common.evaluation import evaluate_policy
29
30mean_reward, std_reward = evaluate_policy(model, eval_env)
31print(f"Mean reward = {mean_reward:.2f} +/- {std_reward:.2f}")
32