🚀 DQN Agent for LunarLander-v2
This is a trained DQN agent for the LunarLander-v2 environment, leveraging the stable-baselines3 library.
🚀 Quick Start
This is a trained model of a DQN agent playing LunarLander-v2 using the stable-baselines3 library.
✨ Features
- Trained Model: A pre - trained DQN agent for the LunarLander - v2 environment.
- Easy to Use: Simple code examples for both usage and training.
📦 Installation
No specific installation steps are provided in the original document.
💻 Usage Examples
Basic Usage
from huggingface_sb3 import load_from_hub
from stable_baselines3 import DQN
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.evaluation import evaluate_policy
checkpoint = load_from_hub("araffin/dqn-LunarLander-v2", "dqn-LunarLander-v2.zip")
kwargs = dict(target_update_interval=30)
model = DQN.load(checkpoint, **kwargs)
env = make_vec_env("LunarLander-v2", n_envs=1)
print("Evaluating model")
mean_reward, std_reward = evaluate_policy(
model,
env,
n_eval_episodes=20,
deterministic=True,
)
print(f"Mean reward = {mean_reward:.2f} +/- {std_reward:.2f}")
obs = env.reset()
try:
while True:
action, _states = model.predict(obs, deterministic=True)
obs, rewards, dones, info = env.step(action)
env.render()
except KeyboardInterrupt:
pass
Advanced Usage
Here is the training code for the DQN agent in the LunarLander - v2 environment.
from stable_baselines3 import DQN
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.callbacks import EvalCallback
env_id = "LunarLander-v2"
n_envs = 8
env = make_vec_env(env_id, n_envs=n_envs)
eval_envs = make_vec_env(env_id, n_envs=5)
eval_freq = int(1e5)
eval_freq = max(eval_freq // n_envs, 1)
eval_callback = EvalCallback(
eval_envs,
best_model_save_path="./logs/",
eval_freq=eval_freq,
n_eval_episodes=10,
)
model = DQN(
"MlpPolicy",
env,
learning_starts=0,
batch_size=128,
buffer_size=100000,
learning_rate=7e-4,
target_update_interval=250,
train_freq=1,
gradient_steps=4,
exploration_fraction=0.08,
exploration_final_eps=0.05,
policy_kwargs=dict(net_arch=[256, 256]),
verbose=1,
)
try:
model.learn(total_timesteps=int(5e5), callback=eval_callback)
except KeyboardInterrupt:
pass
model = DQN.load("logs/best_model.zip")
📚 Documentation
Model Information
Property |
Details |
Model Type |
DQN |
Training Data |
LunarLander - v2 |
Mean Reward |
280.22 +/- 13.03 |