Downloads ยท 30 days
0
0% of all-time downloads
dns08/LunarLander-v2
LunarLander-v2 is a reinforcement learning model from dns08. Use it for the reinforcement learning task on the model card, and read the license before you ship it in a product. It is set up for stable-baselines3.
This is a trained model of a PPO agent playing LunarLander-v2 using the stable-baselines3 library.
Downloads ยท 30 days
0
0% of all-time downloads
All-time downloads
9
Public
Repo size
283 KB
Likes
0
Public
Click a slice to open those files.
.mp4191 KB ยท 37%
From the Hugging Face model README
This is a trained model of a PPO agent playing LunarLander-v2 using the stable-baselines3 library.
TODO: Add your code
!apt install swig cmake
!sudo apt-get update !sudo apt-get install -y python3-opengl !apt install ffmpeg !apt install xvfb !pip3 install pyvirtualdisplay import os os.kill(os.getpid(), 9)
from pyvirtualdisplay import Display virtual_display = Display(visible=0, size=(1400, 900)) virtual_display.start() !pip install gymnasium[box2d] !pip install stable-baselines3[extra] !pip install huggingface_sb3 from huggingface_sb3 import load_from_hub, package_to_hub from huggingface_hub import notebook_loginub from stable_baselines3 import PPO from stable_baselines3.common.env_util import make_vec_env from stable_baselines3.common.evaluation import evaluate_policy from stable_baselines3.common.monitor import Monitor import gymnasium as gym
environ = gym.make("LunarLander-v2")
observation, info = environ.reset()
for i in range(20):
action = environ.action_space.sample() print("Action taken:", action)
observation, reward, terminated, truncated, info = environ.step(action)
if terminated or truncated: # Reset the environment print("Environment is reset") observation, info = environ.reset() environ.close() environ = gym.make("LunarLander-v2") environ.reset() print("OBSERVATION SPACE \n") print("Observation Space Shape", environ.observation_space.shape)
print("Sample observation", environ.observation_space.sample()) print("\n ACTION SPACE \n") print("Action Space Shape", environ.action_space.n) print("Action Space Sample", environ.action_space.sample())
environ = make_vec_env('LunarLander-v2', n_envs=16)
environ = gym.make('LunarLander-v2')
model = PPO(
policy='MlpPolicy',
env=environ,
n_steps=1024,
batch_size=64,
n_epochs=4,
gamma=0.999,
gae_lambda=0.98,
ent_coef=0.01,
verbose=1
)
model.learn(total_timesteps=1000000)
model_name = "ppo-LunarLander-v2" model.save(model_name)
eval_env = Monitor(gym.make("LunarLander-v2", render_mode='rgb_array'))
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
print(f"mean_reward={mean_reward:.2f} +/- {std_reward}") import gymnasium as gym
from stable_baselines3 import PPO from stable_baselines3.common.vec_env import DummyVecEnv from stable_baselines3.common.env_util import make_vec_env
from huggingface_sb3 import package_to_hub
env_id = "LunarLander-v2"
model_architecture = "PPO"
repo_id = "dns08/LunarLander-v2" # Change with your repo id, you can't push with mine ๐
commit_message = "Upload PPO LunarLander-v2 trained agent"
eval_env = DummyVecEnv([lambda: gym.make(env_id, render_mode="rgb_array")])
package_to_hub(model=model, # Our trained model model_name=model_name, # The name of our trained model model_architecture=model_architecture, # The model architecture we used: in our case PPO env_id=env_id, # Name of the environment eval_env=eval_env, # Evaluation Environment repo_id=repo_id, # id of the model repository from the Hugging Face Hub (repo_id = {organization}/{repo_name} for instance ThomasSimonini/ppo-LunarLander-v2 commit_message=commit_message) !pip install huggingface_sb3 !pip install gymnasium stable-baselines3 huggingface_sb3 from stable_baselines3 import PPO from huggingface_sb3 import load_from_hub repo_id = "dns08/LunarLander-v2" # The repo_id filename = "ppo-LunarLander-v2.zip" # The model filename.zip
custom_objects = { "learning_rate": 0.0, "lr_schedule": lambda _: 0.0, "clip_range": lambda _: 0.0, }
checkpoint = load_from_hub(repo_id, filename) model = PPO.load(checkpoint, custom_objects=custom_objects, print_system_info=True) !apt-get install swig !pip install box2d-py !pip install gymnasium[box2d] from stable_baselines3.common.monitor import Monitor from stable_baselines3.common.evaluation import evaluate_policy import gymnasium as gym
eval_env = Monitor(gym.make("LunarLander-v2"))
mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10, deterministic=True)
print(f"mean_reward={mean_reward:.2f} +/- {std_reward}")