Views
No views yet
1from stable_baselines3 import PPO
2from stable_baselines3.common.envs import LunarLander
3from stable_baselines3.common.env_util import make_vec_env
4from stable_baselines3.common.evaluation import evaluate_policy
5
6# Create the LunarLander environment
7env = LunarLander()
8
9# Vectorize the environment for parallel training
10vec_env = make_vec_env('LunarLander-v2', n_envs=16)
11
12# Instantiate the PPO agent
13model = PPO("MlpPolicy", vec_env, verbose=1)
14
15# Train the agent
16model.learn(total_timesteps=int(2e5))
17
18# Evaluate the trained agent
19eval_env = LunarLander()
20mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
21print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")
22
23# Save the trained model
24model_name = "ppo-LunarLander-v2"
25model.save(model_name)
26
27# Package and upload the model to the Hub
28from huggingface_sb3 import package_to_hub
29package_to_hub(model=model,
30 model_name=model_name,
31 model_architecture="PPO",
32 env_id="LunarLander-v2",
33 eval_env=eval_env,
34 repo_id="your-username/ppo-LunarLander-v2",
35 commit_message="Upload PPO LunarLander-v2 trained agent")"your-username" with your Hugging Face username.