A Deep Reinforcement Learning framework that automatically discovers optimal neural network architectures for your machine learning tasks. DQNN uses Deep Q-Learning to explore the architecture search space and find the best model configuration for classification and regression problems.
- Automatic Architecture Search: Discovers optimal neural network architectures using Deep Q-Learning
- Multi-Task Support:
- Binary Classification
- Multiclass Classification
- Regression
- Smart Hyperparameter Tuning: Automatically optimizes learning rate, batch size, epochs, and dropout
- Experiment Tracking: Built-in tracking system (TorchTrack) for all experiments
- Interactive Dashboard: Beautiful Streamlit dashboard for visualization and analysis
- AI-Powered Insights: Integration with Ollama for intelligent model recommendations
- Export & Sharing: Export results, best architectures, and generated PyTorch code
- Automatic GPU Dectection: If using cuda enabled gpu, the code auto detects the gpu and uses it
- Architecture
- Installation
- Quick Start
- Usage
- Dashboard
- AI Insights with Ollama
- Project Structure
- How It Works
- Examples
- Contributing
- License
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β DQNN Framework β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β DQN Agent βββββΆβ Architecture βββββΆβ Model β β
β β (RL Brain) β β Generator β β Trainer β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Experience Replay Buffer β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β² β β
β βΌ | βΌ β
β ββββββββββββββββ | ββββββββββββββββ β
β β TorchTrack β |____β Reward β β
β β (Tracking) β β Calculator β β
β ββββββββββββββββ ββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Streamlit Dashboard + Ollama AI β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Python 3.8 or higher
- pip package manager
- (Optional) CUDA-capable GPU for faster training
- (Optional) Ollama for AI insights
git clone https://github.com/yourusername/dqnn.git
cd dqnn# Using venv
python -m venv venv
# Activate on Windows
venv\Scripts\activate
# Activate on macOS/Linux
source venv/bin/activatepip install torch torchvision torchaudio
pip install scikit-learn numpy pandas matplotlib seaborn
pip install streamlit plotly
pip install ollama # For AI insightsOr install all at once:
pip install -r requirements.txtcurl -fsSL https://ollama.com/install.sh | shDownload and install from https://ollama.com/download
ollama pull gemma2:2b
# or
ollama pull llama3.2:3b
# or any other model you preferNote: Update the model name in app.py if you use a different model:
md = "gemma2:2b" # Change this to your preferred modelfrom dqnn import DQNN
import torch
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load and prepare data
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Convert to PyTorch tensors
X_train = torch.FloatTensor(X_train)
y_train = torch.FloatTensor(y_train)
X_test = torch.FloatTensor(X_test)
y_test = torch.FloatTensor(y_test)
# Initialize and train DQNN
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='classification')
episode_rewards = dqnn.train(episodes=20, max_steps_per_episode=8)
print("Training completed! Check the dashboard for results.")streamlit run app.pyThe dashboard will open in your browser at http://localhost:8501
Run the pre-configured examples in model.py:
python model.pyThis will run:
- Binary Classification (Breast Cancer dataset)
- Multiclass Classification (Iris dataset) - commented out by default
- Regression (Diabetes dataset) - commented out by default
Uncomment the sections you want to run.
from dqnn import DQNN
import torch
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Your custom data
X, y = your_data_loading_function()
# Split and preprocess
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Convert to tensors
X_train = torch.FloatTensor(X_train)
X_test = torch.FloatTensor(X_test)
# For classification
y_train = torch.LongTensor(y_train) # or FloatTensor for binary
y_test = torch.LongTensor(y_test)
# For regression
# y_train = torch.FloatTensor(y_train)
# y_test = torch.FloatTensor(y_test)
# Initialize DQNN
dqnn = DQNN(
X_train, y_train, X_test, y_test,
task_type='classification' # or 'regression'
)
# Train
episode_rewards = dqnn.train(
episodes=50, # Number of architecture search episodes
max_steps_per_episode=10 # Steps per episode
)DQNN automatically detects the task type:
-
Binary Classification: Automatically detected when there are 2 unique classes
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='classification')
-
Multiclass Classification: Automatically detected when there are >2 unique classes
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='classification')
-
Regression: Use for continuous target variables
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='regression')
The Streamlit dashboard provides comprehensive visualization and analysis:
-
Overview:
- Total experiments
- Best performance metrics
- Architecture search evolution
- Performance distribution
-
Best Architecture:
- Detailed architecture information
- Hyperparameters
- Visual representation
- Generated PyTorch code
-
Performance Analysis:
- Hyperparameter impact
- Correlation matrix
- 3D performance visualization
-
Hyperparameter Study:
- Layer count analysis
- Network size impact
- Parameter ranges explored
-
Detailed Results:
- All experiment results
- Sortable tables
- Epoch-wise training progress
-
AI Insights:
- Model performance analysis
- Architecture recommendations
- Training optimization tips
-
Export Results:
- Download CSV/JSON data
- Export best architecture
- Data summaries
- π Interactive Plots: Zoom, pan, and explore your data
- π¨ Beautiful Visualizations: Plotly-based charts
- π Detailed Metrics: Track every aspect of your experiments
- πΎ Export Options: Download results in multiple formats
- π€ AI Analysis: Get intelligent insights powered by Ollama
The dashboard integrates with Ollama to provide AI-powered analysis:
-
Install Ollama (see Installation section)
-
Pull a Model:
# Recommended lightweight models ollama pull gemma2:2b # Fast, 2B parameters ollama pull llama3.2:3b # Balanced, 3B parameters # More powerful models ollama pull llama3.1:8b # Advanced, 8B parameters ollama pull mixtral:8x7b # Expert mixture, very capable
-
Configure Model in Dashboard: Edit
app.py:md = "gemma2:2b" # Change to your preferred model
- Performance Analysis: Deep analysis of model performance across experiments
- Architecture Recommendations: Suggests improvements and alternative architectures
- Training Optimization: Provides tips for better training results
Make sure Ollama is running:
ollama serveOr it will start automatically on most systems.
dqnn/
β
βββ dqnn.py # Core DQNN implementation
β βββ DQNNetwork # Q-Network for architecture search
β βββ ArchitectureState # Architecture representation
β βββ ArchitectureGenerator # Creates PyTorch models
β βββ DQNNAgent # Reinforcement learning agent
β βββ ArchitectureActions # Architecture modification actions
β βββ ModelTrainer # Trains and evaluates architectures
β βββ RewardCalculator # Calculates rewards
β βββ DQNN # Main orchestrator class
β
βββ TorchTrack.py # Experiment tracking system
β βββ log() # Log experiment data
β βββ log_epoch() # Log epoch-level data
β βββ clean_previous_data() # Clear old experiments
β
βββ app.py # Streamlit dashboard
β βββ Overview # Main metrics and visualizations
β βββ Best Architecture # Best model details
β βββ Performance Analysis # Detailed analysis
β βββ AI Insights # Ollama-powered insights
β βββ Export Results # Data export functionality
β
βββ model.py # Example usage file
β βββ Binary Classification Example
β βββ Multiclass Classification Example
β βββ Regression Example
β
βββ data.json # Experiment data (auto-generated)
βββ best_architecture.json # Best architecture (auto-generated)
βββ requirements.txt # Python dependencies
βββ README.md # This file
-
Initialize: Start with a simple architecture (one hidden layer)
-
Explore: The DQN agent explores the architecture space by:
- Adding/removing layers
- Modifying layer sizes
- Changing activation functions
- Adjusting hyperparameters (learning rate, batch size, epochs, dropout)
-
Train: Each architecture is trained and evaluated on your dataset
-
Reward: Calculate reward based on:
- Performance (accuracy/RΒ² score)
- Model complexity (penalize too many parameters)
- Training time (penalize long training)
-
Learn: The DQN agent learns which actions lead to better architectures
-
Optimize: Over multiple episodes, the agent discovers optimal architectures
-
Track: All experiments are logged with TorchTrack
-
Visualize: Results are displayed in the interactive dashboard
The agent can take 8 different actions:
| Action ID | Action | Description |
|---|---|---|
| 0 | Add Layer | Add a new hidden layer |
| 1 | Remove Layer | Remove the last hidden layer |
| 2 | Modify Layer Size | Change the size of a random layer |
| 3 | Change Activation | Change activation function |
| 4 | Adjust Learning Rate | Modify the learning rate |
| 5 | Adjust Batch Size | Change the batch size |
| 6 | Adjust Epochs | Modify number of training epochs |
| 7 | Adjust Dropout | Change dropout rate |
Reward = Ξ± Γ Performance - Ξ² Γ Complexity - Ξ³ Γ Time
Where:
- Ξ± = 1.0 (performance weight)
- Ξ² = 0.001 (complexity penalty)
- Ξ³ = 0.01 (time penalty)
from dqnn import DQNN
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import torch
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
scaler = StandardScaler()
X_train = torch.FloatTensor(scaler.fit_transform(X_train))
X_test = torch.FloatTensor(scaler.transform(X_test))
y_train = torch.FloatTensor(y_train)
y_test = torch.FloatTensor(y_test)
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='classification')
dqnn.train(episodes=20, max_steps_per_episode=8)from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
scaler = StandardScaler()
X_train = torch.FloatTensor(scaler.fit_transform(X_train))
X_test = torch.FloatTensor(scaler.transform(X_test))
y_train = torch.LongTensor(y_train) # Long tensor for multiclass
y_test = torch.LongTensor(y_test)
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='classification')
dqnn.train(episodes=20, max_steps_per_episode=8)from sklearn.datasets import load_diabetes
X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
scaler = StandardScaler()
X_train = torch.FloatTensor(scaler.fit_transform(X_train))
X_test = torch.FloatTensor(scaler.transform(X_test))
y_train = torch.FloatTensor(y_train)
y_test = torch.FloatTensor(y_test)
dqnn = DQNN(X_train, y_train, X_test, y_test, task_type='regression')
dqnn.train(episodes=20, max_steps_per_episode=8)dqnn = DQNN(
X_train, # Training features
y_train, # Training labels
X_test, # Test features
y_test, # Test labels
task_type='classification' # 'classification' or 'regression'
)
episode_rewards = dqnn.train(
episodes=50, # Number of search episodes
max_steps_per_episode=10 # Steps per episode
)Edit dqnn.py to modify agent parameters:
class DQNNAgent:
def __init__(self, ...):
self.epsilon = 1.0 # Initial exploration rate
self.epsilon_min = 0.01 # Minimum exploration rate
self.epsilon_decay = 0.995 # Exploration decay rate
self.learning_rate = 0.001 # DQN learning rate
self.gamma = 0.95 # Discount factor
self.batch_size = 32 # Batch size for replay
self.target_update_freq = 100 # Target network update frequencyAdjust reward calculation in dqnn.py:
reward_calculator = RewardCalculator(
alpha=1.0, # Performance weight
beta=0.001, # Complexity penalty
gamma=0.01 # Time penalty
)After training, DQNN generates:
data.json: Complete experiment history with all trialsbest_architecture.json: Best architecture found with all details- Dashboard visualizations: Available at
http://localhost:8501
{
"layers": [128, 64, 32],
"activations": ["relu", "relu", "relu"],
"learning_rate": 0.001,
"batch_size": 32,
"epochs": 150,
"dropout_rate": 0.2,
"performance": 0.9825,
"task_type": "binary_classification",
"num_classes": null
}-
Start Small: Begin with fewer episodes to test your setup
dqnn.train(episodes=10, max_steps_per_episode=5)
-
Increase Gradually: Once stable, increase search depth
dqnn.train(episodes=50, max_steps_per_episode=10)
-
Use GPU: Training is much faster with CUDA
# DQNN automatically uses GPU if available print(torch.cuda.is_available()) # Check GPU availability
-
Standardize Data: Always standardize/normalize your features
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test)
-
Monitor Progress: Watch the console output during training
-
Use the Dashboard: Visualize results in real-time
Issue: ImportError: No module named 'torch'
pip install torch torchvision torchaudioIssue: ModuleNotFoundError: No module named 'TorchTrack'
- Ensure
TorchTrack.pyis in the same directory asdqnn.py
Issue: Ollama connection error
# Start Ollama service
ollama serve
# Check if model is available
ollama listIssue: Dashboard not loading
# Reinstall Streamlit
pip install --upgrade streamlit
# Clear cache
streamlit cache clearIssue: CUDA out of memory
- Reduce batch size in the architecture
- Use fewer episodes or steps
- Close other GPU-intensive applications
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- PyTorch team for the deep learning framework
- Scikit-learn for the datasets and utilities
- Streamlit for the amazing dashboard framework
- Ollama for local AI capabilities
If you find this project useful, please consider giving it a star! β