Views
No views yet
LunarLander-v3 environment from Gymnasium.| Parameter | Value |
|---|---|
| Total Timesteps | 750,000 |
| Start Timesteps (Random) | 10,000 |
| Batch Size | 256 |
| Discount Factor (γ) | 0.99 |
| Soft Update Rate (τ) | 0.005 |
| Entropy Coefficient (α) | 0.2 |
| Learning Rate | 3e-4 |
| Replay Buffer Capacity | 500,000 |
| Network Architecture | MLP (256, 256) |
LunarLander-v3Input (8) → Linear(256) → ReLU → Linear(256) → ReLU → [Mean(2), Log_Std(2)]Input (8 + 2) → Linear(256) → ReLU → Linear(256) → ReLU → Q-value(1)| File | Description |
|---|---|
sac_model_actor.pth | Actor (policy) network weights |
sac_model_critic.pth | Twin critic network weights |
sac_model_critic_target.pth | Target critic network weights |
pip install gymnasium[box2d] torch numpy1import torch
2import numpy as np
3import gymnasium as gym
4
5# Define the Actor Network
6class ActorNetwork(torch.nn.Module):
7 def __init__(self, state_dim, action_dim, max_action):
8 super(ActorNetwork, self).__init__()
9 self.l1 = torch.nn.Linear(state_dim, 256)
10 self.l2 = torch.nn.Linear(256, 256)
11 self.mean = torch.nn.Linear(256, action_dim)
12 self.log_std_layer = torch.nn.Linear(256, action_dim)
13 self.max_action = float(max_action)
14
15 def forward(self, state):
16 x = torch.nn.functional.relu(self.l1(state))
17 x = torch.nn.functional.relu(self.l2(x))
18 mean = self.mean(x)
19 log_std = self.log_std_layer(x)
20 log_std = torch.clamp(log_std, -20.0, 2.0)
21 return mean, log_std
22
23# Download and load the model
24from huggingface_hub import hf_hub_download
25
26# Download model file
27actor_path = hf_hub_download(
28 repo_id="MohamedMaher003/LunarLander-v3-SAC",
29 filename="sac_model_actor.pth"
30)
31
32# Initialize environment and model
33env = gym.make("LunarLander-v3", continuous=True, render_mode="human")
34state_dim = 8
35action_dim = 2
36max_action = 1.0
37
38# Load actor
39device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40actor = ActorNetwork(state_dim, action_dim, max_action).to(device)
41actor.load_state_dict(torch.load(actor_path, map_location=device))
42actor.eval()
43
44# Run evaluation
45def select_action(state, actor, device):
46 with torch.no_grad():
47 state_tensor = torch.FloatTensor(state).unsqueeze(0).to(device)
48 mean, _ = actor(state_tensor)
49 action = torch.tanh(mean) * actor.max_action
50 return action.cpu().numpy().flatten()
51
52state, _ = env.reset()
53done = False
54total_reward = 0
55
56while not done:
57 action = select_action(state, actor, device)
58 state, reward, terminated, truncated, _ = env.step(action)
59 total_reward += reward
60 done = terminated or truncated
61
62print(f"Episode Reward: {total_reward:.2f}")
63env.close()1@misc{maher2024sac-lunarlander,
2 author = {Mohamed Maher},
3 title = {SAC Agent for LunarLander-v3},
4 year = {2024},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/MohamedMaher003/LunarLander-v3-SAC}}
7}