Views
No views yet
pip install stable-baselines3
pip install huggingface_sb31import gym
2
3from huggingface_sb3 import load_from_hub
4from stable_baselines3 import PPO
5from stable_baselines3.common.evaluation import evaluate_policy
6
7# Retrieve the model from the hub
8## repo_id = id of the model repository from the Hugging Face Hub (repo_id = {organization}/{repo_name})
9## filename = name of the model zip file from the repository
10checkpoint = load_from_hub(repo_id="mrm8488/ppo-CartPole-v1", filename="cartpole-v1.zip")
11model = PPO.load(checkpoint)
12
13# Evaluate the agent
14eval_env = gym.make('CartPole-v1')
15mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
16print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
17
18# Watch the agent play
19obs = env.reset()
20for i in range(1000):
21 action, _state = model.predict(obs)
22 obs, reward, done, info = env.step(action)
23 env.render()
24 if done:
25 obs = env.reset()
26env.close()