Views
No views yet
dqn_cartpole.pth – PyTorch model weightsdqn_cartpole.onnx – ONNX model for deploymentdqn_cartpole.onnx.data – ONNX weightsconfig.json – Model configurationevaluation.py – Script to run and evaluate the model1import torch
2import gymnasium as gym
3
4class DQN(torch.nn.Module):
5 def __init__(self):
6 super(DQN, self).__init__()
7 self.net = torch.nn.Sequential(
8 torch.nn.Linear(4, 128),
9 torch.nn.ReLU(),
10 torch.nn.Linear(128, 128),
11 torch.nn.ReLU(),
12 torch.nn.Linear(128, 2)
13 )
14
15 def forward(self, x):
16 return self.net(x)
17
18model = DQN()
19model.load_state_dict(torch.load("dqn_cartpole.pth", map_location="cpu"))
20model.eval()
21
22env = gym.make("CartPole-v1")
23
24state, _ = env.reset()
25done = False
26
27while not done:
28 state_tensor = torch.FloatTensor(state)
29 with torch.no_grad():
30 action = torch.argmax(model(state_tensor)).item()
31
32 state, reward, done, truncated, _ = env.step(action)
33
34 if truncated:
35 break
36
37env.close()