Views
No views yet
pip install stable-baselines3
pip install huggingface_sb31import os
2
3import gymnasium as gym
4
5from huggingface_sb3 import load_from_hub
6from stable_baselines3 import PPO
7from stable_baselines3.common.evaluation import evaluate_policy
8
9# Allow the use of `pickle.load()` when downloading model from the hub
10# Please make sure that the organization from which you download can be trusted
11os.environ["TRUST_REMOTE_CODE"] = "True"
12
13# Retrieve the model from the hub
14## repo_id = id of the model repository from the Hugging Face Hub (repo_id = {organization}/{repo_name})
15## filename = name of the model zip file from the repository
16checkpoint = load_from_hub(
17 repo_id="sb3/demo-hf-CartPole-v1",
18 filename="ppo-CartPole-v1",
19)
20model = PPO.load(checkpoint)
21
22# Evaluate the agent and watch it
23eval_env = gym.make("CartPole-v1")
24mean_reward, std_reward = evaluate_policy(
25 model, eval_env, render=True, n_eval_episodes=5, deterministic=True, warn=False
26)
27print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")