Interactive web application for building, training, and testing Artificial Neural Networks from scratch using Python and NumPy.
π Professional Implementation: Modern architecture with ES6 modules, PostCSS build system, Clean Architecture, and SOLID principles.
ann-from-scratch/
βββ backend/ # Python/Flask Backend
β βββ core/ # βοΈ ML Algorithms (Pure Python/NumPy)
β β βββ neural_network.py # Main NeuralNetwork class
β β βββ activation_functions.py # Sigmoid, ReLU, Softmax, Linear, Threshold
β β βββ loss_functions.py # MSE, Binary/Categorical Cross-Entropy
β β βββ optimizers.py # GD, SGD, Momentum
β β βββ __init__.py
β β
β βββ services/ # π§ Business Logic Layer
β β βββ network_service.py # Network building & management
β β βββ training_service.py # Training orchestration & metrics
β β βββ data_service.py # Data processing & validation
β β βββ __init__.py
β β
β βββ api/ # π REST API Layer
β β βββ routes/ # API Endpoints
β β β βββ network_routes.py # /build_network, /network_info
β β β βββ training_routes.py # /train, /backpropagation, /update_weights
β β β βββ prediction_routes.py # /predict, /forward_pass
β β β βββ example_routes.py # /quick_start_binary, /quick_start_multiclass
β β β βββ __init__.py
β β βββ middleware/ # Error Handling
β β β βββ error_handler.py
β β β βββ __init__.py
β β βββ app.py # Flask Application Factory
β β βββ __init__.py
β β
β βββ utils/ # π οΈ Utilities
β β βββ validators.py # Input validation
β β βββ data_processor.py # Data transformations
β β βββ __init__.py
β β
β βββ config.py # Configuration (Dev, Prod, Test)
β βββ __init__.py
β
βββ frontend/ # HTML/CSS/JavaScript Frontend
β βββ static/
β β βββ css/
β β β βββ src/ # π Source CSS (Modular)
β β β β βββ base/
β β β β β βββ variables.css # CSS custom properties
β β β β β βββ reset.css # CSS reset & base styles
β β β β β βββ layout.css # Page layout & structure
β β β β β βββ utilities.css # Animations & modals
β β β β βββ components/
β β β β β βββ network-canvas.css # Network visualization styles
β β β β βββ features/
β β β β β βββ app-features.css # App-specific UI styles
β β β β βββ main.css # Imports all CSS modules
β β β βββ dist/ # β‘ Built CSS (Generated by PostCSS)
β β β βββ style.css # Compiled & minified CSS
β β β
β β βββ js/ # π¦ JavaScript (ES6 Modules)
β β βββ modules/ # Feature Modules
β β β βββ network/
β β β βββ network-builder.js # Interactive network visualization
β β βββ utils/ # Shared Utilities
β β β βββ api-client.js # Centralized backend communication
β β β βββ formatters.js # Number formatting utilities
β β βββ config/
β β β βββ constants.js # Application constants
β β βββ app.js # π Main Entry Point (ES6 Module)
β β βββ app.js.backup # Backup of original file
β β
β βββ templates/ # Jinja2 Templates
β βββ partials/
β β βββ head.html # <head> section with CSS
β β βββ navbar.html # Navigation bar
β β βββ hero.html # Hero section
β β βββ steps.html # Progress indicator
β β βββ footer.html # Footer
β β βββ modal.html # Modal dialogs
β β βββ scripts.html # JavaScript includes
β βββ index.html # Main page with 7 interactive tabs
β
βββ .gitignore # Git ignore rules
βββ package.json # π¦ Node.js dependencies (PostCSS build)
βββ postcss.config.js # PostCSS configuration
βββ requirements.txt # π Python dependencies
βββ run.py # Application entry point
βββ run.bat # Windows batch launcher
βββ README.md # This file
βββ CLAUDE.md # AI assistant project guidance
- Clean Architecture: Core β Services β API layer separation
- SOLID Principles: Applied throughout the codebase
- ES6 Modules: Modern JavaScript with import/export
- PostCSS Build System: Modular CSS compiled to single optimized file
- Design Patterns: Strategy, Factory, Facade
- Flexible Architecture: Build custom networks with any layer configuration
- Activation Functions: Sigmoid, ReLU, Softmax, Linear, Threshold
- Loss Functions: MSE, Binary Cross-Entropy, Categorical Cross-Entropy
- Optimizers: Gradient Descent (GD), SGD, Momentum
- Smart Initialization: Xavier/He initialization prevents vanishing/exploding gradients
- Visual Network Builder: Drag-and-drop interface to create connections
- Real-time Training: Watch loss decrease during training
- 7 Interactive Tabs: Build β Dataset β Forward β Loss β Epoch β Train β Results
- Responsive Design: Works on desktop and mobile (DaisyUI + Tailwind CSS)
- Step-by-Step Workflow: Guided learning experience
- Network Visualization: Interactive canvas showing nodes and connections
- Training Charts: Loss curves with Chart.js
- Evaluation Metrics: Accuracy, Precision, Recall, F1-score
- Confusion Matrix: Per-class performance analysis
- Layer-by-layer Inspection: View activations at each layer
- Python 3.8+ with pip
- Node.js 16+ with npm (for CSS build)
-
Clone Repository
git clone https://github.com/yourrepo/ann-from-scratch.git cd ann-from-scratch -
Install Python Dependencies
# Recommended: Create virtual environment python -m venv venv # Windows venv\Scripts\activate # Linux/Mac source venv/bin/activate # Install dependencies pip install -r requirements.txt
-
Install Node.js Dependencies (for CSS build)
npm install
-
Build CSS (first time or when CSS changes)
npm run build:css
# Start Flask development server
python run.py
# Or on Windows
run.batOpen your browser to http://localhost:5000
-
Build Network (Tab 1)
- Click "Binary Classification (3-4-1)" or "Multi-Class (3-4-2)" for quick start
- Or build custom network using visual builder
- Click "Build Network" button
-
Load Dataset (Tab 2)
- Upload CSV file, or
- Click "Load Example Dataset"
- Click "Save Dataset & Continue"
-
Forward Pass (Tab 3)
- Click "Run Forward Pass"
- View predictions and layer activations
-
Calculate Loss (Tab 4)
- Select loss function
- Click "Calculate Loss"
-
Run Epoch (Tab 5)
- Configure learning rate and optimizer
- Click "Run 1 Epoch"
- See backpropagation and weight updates
-
Train (Tab 6)
- Set number of epochs
- Click "Start Training"
- Watch real-time progress
-
View Results (Tab 7)
- See loss curves
- Review metrics and confusion matrix
from backend.core import NeuralNetwork
import numpy as np
# Create network
network = NeuralNetwork()
network.add_layer(2, 'linear') # 2 inputs
network.add_layer(2, 'sigmoid') # 2 hidden neurons
network.add_layer(1, 'sigmoid') # 1 output
# Set connections (fully connected)
network.set_connections(
1, [[0,1], [0,1]], # Each hidden connects to both inputs
[[0.5,-0.3], [-0.4,0.6]], # Weights
[0.1, -0.2] # Biases
)
network.set_connections(
2, [[0,1]], # Output connects to both hidden
[[0.8,-0.5]], # Weights
[0.2] # Bias
)
# Train (AND gate example)
X = np.array([[0,0], [0,1], [1,0], [1,1]])
y = np.array([[0], [0], [0], [1]])
history = network.train(
X, y,
epochs=500,
learning_rate=0.5,
optimizer='gd',
loss_function='mse'
)
# Predict
y_pred_classes, y_pred_probs = network.predict(X)
print(f"Final Loss: {history['loss'][-1]:.6f}")
print(f"Predictions:\n{y_pred_probs}")import requests
# Load example binary network
response = requests.post('http://localhost:5000/quick_start_binary')
data = response.json()
# Train with example dataset
train_response = requests.post('http://localhost:5000/train', json={
'dataset': data['example_dataset'],
'epochs': 1000,
'learning_rate': 0.5,
'optimizer': 'gd',
'loss_function': 'binary_crossentropy'
})
result = train_response.json()
print(f"Training complete!")
print(f"Accuracy: {result['metrics']['accuracy'] * 100:.2f}%")
print(f"Final Loss: {result['final_loss']:.6f}")Network Building:
POST /build_network- Build custom networkPOST /quick_start_binary- Load binary classification example (3-4-1)POST /quick_start_multiclass- Load multi-class example (3-4-2)GET /network_info- Get current network information
Predictions:
POST /predict- Make predictions on datasetPOST /forward_pass- Get detailed layer-by-layer activations
Training:
POST /train- Train network with datasetPOST /calculate_loss- Calculate current lossPOST /backpropagation- Calculate gradients (educational)POST /update_weights- Single weight update step (educational)
- Python 3.8+ - Core language
- Flask - Web framework
- NumPy - Numerical computations
- Pandas - Data processing
- HTML5 - Markup
- Tailwind CSS + DaisyUI - UI framework
- JavaScript (ES6 Modules) - Application logic
- Chart.js - Visualizations
- PostCSS - CSS processing
- Autoprefixer - Browser compatibility
- cssnano - CSS minification
MIT License - Free to use for educational and commercial purposes.
Inspired by best practices from:
- Robert C. Martin - Clean Architecture & SOLID Principles
- Gang of Four - Design Patterns
- Martin Fowler - Refactoring
- Miguel Grinberg - Flask Web Development
- Create an issue on GitHub for bugs or questions
- See
CLAUDE.mdfor AI assistant guidance - Check inline code documentation for implementation details
Built with β€οΈ for Deep Learning Education
Learn by building. Understand by implementing. Master by teaching.