Views
No views yet
1import gym
2
3from huggingface_sb3 import load_from_hub, package_to_hub, push_to_hub
4from huggingface_hub import notebook_login # To log to our Hugging Face account to be able to upload models to the Hub.
5
6from stable_baselines3 import PPO
7from stable_baselines3.common.evaluation import evaluate_policy
8from stable_baselines3.common.env_util import make_vec_env
9
10# Create the environment
11env = make_vec_env('LunarLander-v2', n_envs=16)
12
13model = PPO(
14 policy = 'MlpPolicy',
15 env = env,
16 n_steps = 1024,
17 batch_size = 64,
18 n_epochs = 8,
19 gamma = 0.995,
20 gae_lambda = 1,
21 ent_coef = 0.001,
22 verbose=1
23 )
24
25model.learn(total_timesteps=2_000_000, log_interval=25, progress_bar=True)
26
27model_name = "ppo-LunarLander-v2"
28
29# Evaluate the agent
30# Create a new environment for evaluation
31eval_env = gym.make("LunarLander-v2")
32
33# Evaluate the model with 10 evaluation episodes and deterministic=True
34mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
35
36# Print the results
37print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
38
39# Upload to Hugging Face Hub
40...