RL Agents
Collection
This is a collection of Projects of RL Agents. • 2 items • Updated • 1
This repository contains a Deep Q-Network (DQN) agent trained to solve the CartPole-v1 environment using reinforcement learning.
The agent learns to balance a pole on a moving cart by interacting with the environment and maximizing cumulative reward. The model is trained using experience replay and a target network for stable learning.
dqn_cartpole.pth – PyTorch model weights dqn_cartpole.onnx – ONNX model for deployment dqn_cartpole.onnx.data – ONNX weights config.json – Model configuration evaluation.py – Script to run and evaluate the modelimport torch
import gymnasium as gym
class DQN(torch.nn.Module):
def __init__(self):
super(DQN, self).__init__()
self.net = torch.nn.Sequential(
torch.nn.Linear(4, 128),
torch.nn.ReLU(),
torch.nn.Linear(128, 128),
torch.nn.ReLU(),
torch.nn.Linear(128, 2)
)
def forward(self, x):
return self.net(x)
model = DQN()
model.load_state_dict(torch.load("dqn_cartpole.pth", map_location="cpu"))
model.eval()
env = gym.make("CartPole-v1")
state, _ = env.reset()
done = False
while not done:
state_tensor = torch.FloatTensor(state)
with torch.no_grad():
action = torch.argmax(model(state_tensor)).item()
state, reward, done, truncated, _ = env.step(action)
if truncated:
break
env.close()
The model evaluation over 2000 episodes.
The model evaluation over 200 episodes.