Views
No views yet
| Metric | Value |
|---|---|
| Mean Reward | 496.53 |
| Std Reward | 26.20 |
| Min Reward | 257.00 |
| Max Reward | 500.00 |
| Mean Episode Length | 496.53 |
| Score (mean - std) | 470.33 |
| Evaluation Episodes | 100 |
1import torch
2import torch.nn as nn
3import torch.nn.functional as F
4import gymnasium as gym
5import numpy as np
6
7class Policy(nn.Module):
8 def __init__(self, s_size, a_size, h_size=128):
9 super(Policy, self).__init__()
10 self.fc1 = nn.Linear(s_size, h_size)
11 self.fc2 = nn.Linear(h_size, a_size)
12
13 def forward(self, x):
14 x = F.relu(self.fc1(x))
15 x = self.fc2(x)
16 return F.softmax(x, dim=1)
17
18device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
19checkpoint = torch.load("reinforce_cartpole.pth", map_location=device)
20
21policy = Policy(checkpoint['s_size'], checkpoint['a_size'], checkpoint['hidden_size'])
22policy.load_state_dict(checkpoint['policy_state_dict'])
23policy.eval()
24
25env = gym.make("CartPole-v1")
26state, _ = env.reset()
27
28for step in range(1000):
29 state_tensor = torch.from_numpy(state).float().unsqueeze(0)
30 with torch.no_grad():
31 probs = policy(state_tensor)
32 action = torch.argmax(probs, dim=1).item()
33
34 state, reward, terminated, truncated, _ = env.step(action)
35
36 if terminated or truncated:
37 state, _ = env.reset()
38
39
40## Training Configuration
41
42- **Algorithm**: REINFORCE (Policy Gradient)
43- **Policy Network**: 2-layer MLP (128 hidden units)
44- **Optimizer**: Adam
45- **Learning Rate**: 0.003
46- **Discount Factor**: 0.99
47- **Training Episodes**: 800
48- **Device**: cuda:0
49
50## Training Hyperparameters
51- Episodes: 800
52- Max steps per episode: 1000
53- Learning rate: 0.01
54- Gamma (discount factor): 0.99
55- Hidden layer size: 128
56- Optimizer: Adam