PPO Agent playing LunarLander-v2
This is a trained model of a PPO agent playing LunarLander-v2 using the stable-baselines3 library.
Usage (with Stable-baselines3)
from huggingface_sb3 import load_from_hub<br /> from stable_baselines3 import PPO<br /> from stable_baselines3.common.env_util import make_vec_env<br /> from stable_baselines3.common.evaluation import evaluate_policy<br /> # Download checkpoint<br /> checkpoint = load_from_hub("araffin/ppo-LunarLander-v2", "ppo-LunarLander-v2.zip")<br /> # Load the model<br /> model = PPO.load(checkpoint)<br /> env = make_vec_env("LunarLander-v2", n_envs=1)<br /> # Evaluate<br /> print("Evaluating model")<br /> mean_reward, std_reward = evaluate_policy(<br /> model,<br /> env,<br /> n_eval_episodes=20,<br /> deterministic=True,<br /> )<br /> print(f"Mean reward = {mean_reward:.2f} +/- {std_reward:.2f}")<br /> # Start a new episode<br /> obs = env.reset()<br /> try:<br /> while True:<br /> action, _states = model.predict(obs, deterministic=True)<br /> obs, rewards, dones, info = env.step(action)<br /> env.render()<br /> except KeyboardInterrupt:<br /> pass<br />
Training code (with SB3)
from stable_baselines3 import PPO<br /> from stable_baselines3.common.env_util import make_vec_env<br /> from stable_baselines3.common.callbacks import EvalCallback<br /> # Create the environment<br /> env_id = "LunarLander-v2"<br /> n_envs = 16<br /> env = make_vec_env(env_id, n_envs=n_envs)<br /> # Create the evaluation envs<br /> eval_envs = make_vec_env(env_id, n_envs=5)<br /> # Adjust evaluation interval depending on the number of envs<br /> eval_freq = int(1e5)<br /> eval_freq = max(eval_freq // n_envs, 1)<br /> # Create evaluation callback to save best model<br /> # and monitor agent performance<br /> eval_callback = EvalCallback(<br /> eval_envs,<br /> best_model_save_path="./logs/",<br /> eval_freq=eval_freq,<br /> n_eval_episodes=10,<br /> )<br /> # Instantiate the agent<br /> # Hyperparameters from https://github.com/DLR-RM/rl-baselines3-zoo<br /> model = PPO(<br /> "MlpPolicy",<br /> env,<br /> n_steps=1024,<br /> batch_size=64,<br /> gae_lambda=0.98,<br /> gamma=0.999,<br /> n_epochs=4,<br /> ent_coef=0.01,<br /> verbose=1,<br /> )<br /> # Train the agent (you can kill it before using ctrl+c)<br /> try:<br /> model.learn(total_timesteps=int(5e6), callback=eval_callback)<br /> except KeyboardInterrupt:<br /> pass<br /> # Load best model<br /> model = PPO.load("logs/best_model.zip")<br />
收录说明:
1、本网页并非 araffin/ppo-LunarLander-v2 官网网址页面,此页面内容编录于互联网,只作展示之用;2、如果有与 araffin/ppo-LunarLander-v2 相关业务事宜,请访问其网站并获取联系方式;3、本站与 araffin/ppo-LunarLander-v2 无任何关系,对于 araffin/ppo-LunarLander-v2 网站中的信息,请用户谨慎辨识其真伪。4、本站收录 araffin/ppo-LunarLander-v2 时,此站内容访问正常,如遇跳转非法网站,有可能此网站被非法入侵或者已更换新网址,导致旧网址被非法使用,5、如果你是网站站长或者负责人,不想被收录请邮件删除:i-hu#Foxmail.com (#换@)
前往AI网址导航
声明:本文来自AIGC网址导航投稿,不代表微浪网立场,版权归原作者所有,欢迎分享本文,转载请保留出处!