This directory contains a custom implementation of Deep Q-Network (DQN) for Atari games.
📚 For general Atari information, see atari/README.md
dqn/
├── scripts/ # Training scripts
│ ├── pong-dqn.py # Train DQN on Pong
│ ├── pacman-dqn.py # Train DQN on Ms. Pacman
│ ├── dqn_trainer.py # Generic DQN trainer
│ └── dqn_tester.py # Test trained DQN models
└── src/ # Source code
├── agent.py # DQN agent implementation
├── model.py # Neural network architecture
└── config/ # Configuration classes
├── environment_config.py
├── model_config.py
├── training_config.py
├── learning_config.py
├── exploration_config.py
└── image_config.py
# Train on Pong
python scripts/pong-dqn.py
# Train on Ms. Pacman
python scripts/pacman-dqn.py
# Use generic trainer
python scripts/dqn_trainer.py pong
# Test trained model (loads the latest checkpoint by default)
python scripts/dqn_tester.py pong --load-episode 900- Dueling CNN Architecture: Separate value and advantage streams
- Experience Replay: Stores and samples past experiences
- Target Networks: Stabilizes training with separate target network
- Frame Stacking: Uses 4 consecutive frames as state representation
- Epsilon-Greedy Exploration: Balances exploration vs exploitation
The DQN uses a Dueling CNN architecture:
- Convolutional Layers: Feature extraction from game pixels
- Dueling Streams: Separate value and advantage computation
- Fully Connected: Final Q-value computation
- Output: Q-values for each action
- Input: 80x80 preprocessed grayscale frames
- Conv1: 32 filters, 8x8 kernel, stride 4
- Conv2: 64 filters, 4x4 kernel, stride 2
- Conv3: 64 filters, 3x3 kernel, stride 1
- Fully Connected: 512 hidden units
- Output: Q-values for each action
The DQN implementation uses a modular configuration system:
- Environment Config: Game-specific settings
- Model Config: Neural network architecture
- Training Config: Learning parameters
- Exploration Config: Epsilon-greedy settings
- Image Config: Frame preprocessing
# Learning
learning_rate = 0.00025
gamma = 0.99 # Discount factor
# Experience Replay
replay_buffer_size = 100000
batch_size = 32
# Exploration
epsilon_start = 1.0
epsilon_end = 0.01
epsilon_decay = 0.995
# Target Network
target_update_freq = 1000- Pong: 21+ average reward after 1M timesteps
- Pacman: Complex maze navigation with ghost avoidance
- Training Time: 1-2 hours for 1M timesteps on GPU
- Memory Usage: ~2GB for replay buffer
Trained models are saved in:
../../models/dqn/pong/for Pong models../../models/dqn/pacman/for Pacman models
.pkl: Model weights.json: Epsilon values and training state.txt: Training logs and performance metrics
pip install torch numpy gymnasium[atari] ale-py- Environment Setup: Preprocess frames to 80x80 grayscale
- Experience Collection: Store (state, action, reward, next_state, done) tuples
- Training Loop: Sample batches and update Q-network
- Target Network: Periodically update target network for stability
- Exploration: Gradually reduce epsilon for exploitation
- Action Space: 2 actions (UP/DOWN)
- Reward: +1 for winning, -1 for losing
- Preprocessing: Frame differencing for motion detection
- Action Space: 9 actions (8 directions + NOOP)
- Reward: Points for eating dots, power pellets, ghosts
- Preprocessing: Color-aware processing for ghost detection
| Aspect | DQN | Policy Gradient |
|---|---|---|
| Learning Type | Off-policy | On-policy |
| Action Space | Discrete only | Both |
| Policy Type | Deterministic | Stochastic |
| Sample Efficiency | Higher (replay) | Lower |
| Training Stability | More stable | Less stable |
| Implementation | More complex | Simpler |
- atari/README.md - General Atari information
- baselines/README.md - Stable Baselines3 DQN
- pg/README.md - Policy Gradient comparison