Views
No views yet
1from stable_baselines3 import ...
2from huggingface_sb3 import load_from_hub
31import 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="ThomasSimonini/ppo-LunarLander-v2", filename="ppo-LunarLander-v2.zip")
11model = PPO.load(checkpoint)
12
13# Evaluate the agent
14eval_env = gym.make('LunarLander-v2')
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 = eval_env.reset()
20for i in range(1000):
21 action, _state = model.predict(obs)
22 obs, reward, done, info = eval_env.step(action)
23 eval_env.render()
24 if done:
25 obs = eval_env.reset()
26eval_env.close()
27