Downloads · 30 days
0
aryannzzz/dqn-cartpole-scratch
dqn-cartpole-scratch is a reinforcement learning model from aryannzzz. Use it for the reinforcement learning task on the model card, and read the license before you ship it in a product. It is set up for pytorch. The card lists the license as mit.
This is a Deep Q-Network (DQN) agent trained from scratch on the CartPole-v1 environment using PyTorch and Gymnasium.
Downloads · 30 days
0
Access
Public
Updated Dec 21, 2025
Repo size
603 KB
Likes
0
Public
Click a slice to open those files.
.png314 KB · 52%
From the Hugging Face model README
This is a Deep Q-Network (DQN) agent trained from scratch on the CartPole-v1 environment using PyTorch and Gymnasium.
Input (4) → FC(128) → ReLU → FC(128) → ReLU → Output(2)
Total Parameters: 17,410
The agent achieves perfect performance (500/500 steps):

import torch
import gymnasium as gym
from huggingface_hub import hf_hub_download
# Download model
model_path = hf_hub_download(
repo_id="aryannzzz/dqn-cartpole-scratch",
filename="dqn_cartpole.pth"
)
# Define Q-Network architecture
class QNetwork(torch.nn.Module):
def __init__(self, state_dim, action_dim, hidden_dim=128):
super().__init__()
self.network = torch.nn.Sequential(
torch.nn.Linear(state_dim, hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, action_dim)
)
def forward(self, x):
return self.network(x)
# Load model
checkpoint = torch.load(model_path, map_location='cpu')
q_network = QNetwork(state_dim=4, action_dim=2)
q_network.load_state_dict(checkpoint['q_network_state_dict'])
q_network.eval()
# Use the agent
env = gym.make('CartPole-v1')
state, _ = env.reset()
done = False
total_reward = 0
while not done:
state_tensor = torch.FloatTensor(state).unsqueeze(0)
with torch.no_grad():
q_values = q_network(state_tensor)
action = q_values.argmax().item()
state, reward, terminated, truncated, _ = env.step(action)
total_reward += reward
done = terminated or truncated
print(f"Total Reward: {total_reward}")
env.close()
dqn_cartpole.pth - Complete model checkpoint (Q-network + optimizer state)dqn_training.png - Training progress visualizationThis model was trained using custom DQN implementation:
02_dqn_from_scratch.py| Metric | Value |
|---|---|
| Average Reward | 500.00 |
| Success Rate | 100% |
| Training Episodes | 500 |
| Training Time | ~2 minutes (GPU) |
| Parameters | 17,410 |
If you use this model, please reference:
@misc{dqn-cartpole-scratch,
author = {aryannzzz},
title = {DQN CartPole Model from Scratch},
year = {2025},
publisher = {HuggingFace},
url = {https://huggingface.co/aryannzzz/dqn-cartpole-scratch}
}