Views
No views yet
1{
2 "algorithm": "PPO",
3 "environment": "LunarLander-v3",
4 "total_episodes": 1000,
5 "n_steps": 128,
6 "batch_size": 16,
7 "epochs": 4,
8 "actor_hidden_size": 256,
9 "critic_hidden_size": 256,
10 "epsilon": 0.2,
11 "gae_lambda": 0.95,
12 "gamma": 0.99,
13 "actor_learning_rate": 0.0003,
14 "critic_learning_rate": 0.001,
15 "gradient_clip": 0.5,
16 "optimizer": "Adam"
17}
pip install gymnasium torch numpy matplotlib1import torch
2import torch.nn as nn
3import gymnasium as gym
4from huggingface_hub import hf_hub_download
5
6# Define the actor network architecture (must match training)
7class ActorNetwork(nn.Module):
8 def __init__(self, input_dims=8, output_dims=4, hidden_layer1=256, hidden_layer2=256):
9 super().__init__()
10 self.actor = nn.Sequential(
11 nn.Linear(input_dims, hidden_layer1),
12 nn.ReLU(),
13 nn.Linear(hidden_layer1, hidden_layer2),
14 nn.ReLU(),
15 nn.Linear(hidden_layer2, output_dims)
16 )
17
18 def forward(self, state):
19 dist = torch.distributions.Categorical(logits=self.actor(state))
20 return dist
21
22# Download actor model
23actor_path = hf_hub_download(
24 repo_id="ketencrypt10n/ppo-lunar-lander",
25 filename="actor_network.pth"
26)
27
28# Load model
29device = 'cuda' if torch.cuda.is_available() else 'cpu'
30actor = ActorNetwork(input_dims=8, output_dims=4).to(device)
31actor.actor.load_state_dict(torch.load(actor_path, map_location=device))
32actor.eval()
33
34# Test the agent
35env = gym.make("LunarLander-v3", render_mode="human")
36
37for episode in range(30):
38 done = False
39 total_reward = 0
40 state, info = env.reset()
41
42 while not done:
43 state_tensor = torch.tensor(state, dtype=torch.float32, device=device)
44 with torch.no_grad():
45 dist = actor(state_tensor)
46 action = dist.sample()
47
48 state, reward, terminated, truncated, info = env.step(action.item())
49 total_reward += reward
50 done = terminated or truncated
51
52 print(f"Episode: {episode+1}, Total Reward: {total_reward:.2f}")
53
54env.close()statistics_ppo.png which shows the complete training visualization with:1import numpy as np
2from huggingface_hub import hf_hub_download
3
4# Download training history (last 100 episodes)
5reward_path = hf_hub_download(repo_id="ketencrypt10n/ppo-lunar-lander", filename="reward_history_last100.npy")
6duration_path = hf_hub_download(repo_id="ketencrypt10n/ppo-lunar-lander", filename="episode_durations_last100.npy")
7
8rewards = np.load(reward_path)
9durations = np.load(duration_path)
10
11print(f"Average reward (last 100 episodes): {np.mean(rewards):.2f}")
12print(f"Max reward (last 100 episodes): {np.max(rewards):.2f}")
13print(f"Average duration (last 100 episodes): {np.mean(durations):.2f} steps")L^CLIP(θ) = E[min(r_t(θ)Â_t, clip(r_t(θ), 1-ε, 1+ε)Â_t)]r_t(θ) is the probability ratio between new and old policiesÂ_t is the generalized advantage estimateε is the clipping parameter (0.2)1@misc{ppo_lunar_lander,
2 author = {ketencrypt10n},
3 title = {PPO Agent for Lunar Lander},
4 year = {2025},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/ketencrypt10n/ppo-lunar-lander}}
7}