A professional, hands-on repository for replicating foundational machine learning papers from scratch using PyTorch.
- Overview
- Key Features
- Repository Structure
- Module Details
- Installation & Setup
- Quick Start Guide
- Implementation Philosophy
- Project Roadmap
- Contributing Guidelines
- FAQ & Troubleshooting
- Citation
- License & Disclaimer
- References
This repository provides implementation-first learning of influential machine learning architectures by replicating foundational research papers. Each module includes:
- Complete PyTorch implementations of architectures from original papers
- From-scratch implementations to deeply understand core concepts
- Training and evaluation pipelines with reproducible results
- Clear documentation of design choices, challenges, and practical insights
- Jupyter notebooks and scripts for both learning and experimentation
The goal is to bridge the gap between theoretical understanding of research papers and practical implementation skills in modern machine learning frameworks.
- π¬ Paper-driven Implementation: Each module is based on a specific research paper with proper citations
- π Educational Focus: Designed for learners to understand architectures from first principles
- ποΈ Modular Architecture: Each module is self-contained with clear, well-documented code
- π Multiple Implementation Approaches: From-scratch implementations alongside framework-optimized versions
- π Reproducible Results: Clear metrics tracking and validation methodologies
- π Comprehensive Documentation: Detailed READMEs, architecture diagrams, and training guides in each module
- β‘ Production-Ready Features: Advanced training techniques, checkpointing, and inference pipelines
- π Pure Python/PyTorch: No custom CUDA kernels or complex dependencies (except where necessary)
Paper-Replicating/
βββ AutoEncoders/ # Autoencoder architectures and experiments
β βββ AutoEncoders.ipynb # Vanilla autoencoder implementation
β βββ README.md
βββ CNN/ # Convolutional Neural Networks from classic papers
β βββ LeNet.ipynb # LeNet-5 (LeCun et al., 1998)
β βββ AlexNet.ipynb # AlexNet (Krizhevsky et al., 2012)
β βββ README.md
βββ RNN/ # Recurrent Neural Networks and LSTM
β βββ lstm.ipynb # LSTM from-scratch and PyTorch implementations
β βββ README.md
βββ LLAMA/ # Large Language Model implementation
β βββ model.py # LLaMA architecture
β βββ train.py # Basic training script
β βββ train_advanced.py # Advanced training with scheduling and amp
β βββ inference.py # Text generation and inference
β βββ data_utils.py # Data processing utilities
β βββ config.py # Configuration management
β βββ requirements.txt # Python dependencies
β βββ README.md
βββ README.md # This file
Paper Reference: Foundational Autoencoder Principles
Notebook: AutoEncoders.ipynb
Implements a vanilla autoencoder with:
- Multi-layer encoder-decoder architecture with bottleneck representation
- Training on MNIST dataset for unsupervised learning
- Latent space visualization using PCA and t-SNE
- Reconstruction quality evaluation metrics
Key Features:
- Encoder: 1024 β 128 β 64 β 32 β bottleneck dimensions
- ReLU hidden layers, Sigmoid output layer
- MSE loss function
- Interactive visualizations and reconstruction examples
Future Work: Variational Autoencoders (VAE), Denoising Autoencoders, Sparse Autoencoders
Implementations:
Paper: "Gradient-Based Learning Applied to Document Recognition"
- 5-layer architecture with 2 convolutional layers and 3 fully connected layers
- Average pooling for dimensionality reduction
- Optimized for MNIST digit recognition (32Γ32 input)
- Implements ReLU activations and proper weight initialization
Paper: "ImageNet Classification with Deep Convolutional Neural Networks"
- 8-layer deep architecture (5 conv + 3 fully connected)
- Local Response Normalization for improved generalization
- Max pooling with stride for hierarchical feature extraction
- Dropout regularization to prevent overfitting
- Designed for 224Γ224 ImageNet classification
Common Features:
- Layer-by-layer implementations matching original papers
- Proper architectural details and hyperparameters
- Training and evaluation pipelines
Future Work: VGG, GoogLeNet/Inception, ResNet, DenseNet
Paper Reference: Hochreiter & Schmidhuber (1997) - "Long Short-Term Memory"
Notebook: lstm.ipynb
Implements LSTM networks with:
- From-scratch LSTM: Complete custom implementation of LSTM cells with input, forget, and output gates
- PyTorch LSTM: Official implementation for performance comparison
- Text Generation: Trained on TimeMachine dataset with custom prompt generation
- Gradient Clipping: Prevention of exploding gradients during training
Model Architecture:
- 32 hidden units (configurable)
- Vocabulary-sized input and output dimensions
- Multi-step sequence processing for temporal dependencies
Future Work: GRU, Bidirectional RNN, Attention mechanisms, Sequence-to-sequence models
Paper Reference: Touvron et al. (2023) - "LLaMA: Open and Efficient Foundation Language Models"
Complete Production-Ready Implementation:
-
Model Architecture:
- RMSNorm (Root Mean Square Layer Normalization)
- RoPE (Rotary Position Embeddings) for position-aware attention
- Multi-Head Attention with Grouped Query Attention (GQA)
- SwiGLU activation function in feed-forward networks
- Pre-normalization (pre-norm) transformer blocks
-
Training Systems:
train.py: Basic training with simple checkpointingtrain_advanced.py: Advanced training with:- Learning rate scheduling (warmup + cosine decay)
- Gradient clipping for training stability
- Mixed precision training (AMP) for GPU efficiency
- Weights & Biases integration for experiment tracking
- Early stopping and best model checkpointing
-
Inference & Generation:
- Text generation with configurable sampling
- Prompt-based generation pipeline
- Support for multiple model sizes
-
Configuration System:
- Predefined sizes: small (2M params), medium (15M params), large (100M params)
- Fully customizable parameters for experiments
Supported Model Sizes:
| Size | Dimension | Layers | Heads | Parameters |
|---|---|---|---|---|
| Small | 256 | 4 | 4 | ~2M |
| Medium | 512 | 8 | 8 | ~15M |
| Large | 1024 | 16 | 16 | ~100M |
- Python: 3.9 or higher
- RAM: 8GB minimum (16GB recommended)
- GPU: Optional but recommended (CUDA 11.8+ for NVIDIA GPUs)
- OS: Linux, macOS, or Windows
# Using HTTPS
git clone https://github.com/MohitGoyal09/Paper-Replicating.git
cd Paper-Replicating
# Using SSH
git clone git@github.com:MohitGoyal09/Paper-Replicating.git
cd Paper-Replicating# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# On Linux/macOS:
source venv/bin/activate
# On Windows:
venv\Scripts\activate# Upgrade pip, setuptools, and wheel
pip install --upgrade pip setuptools wheel
# Install PyTorch (CPU version)
pip install torch torchvision torchaudio
# Or install PyTorch with CUDA support (recommended for GPU)
# Visit https://pytorch.org/get-started/locally/ for your specific configuration
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118# For Jupyter notebook modules (AutoEncoders, CNN, RNN)
pip install jupyter jupyterlab matplotlib numpy scikit-learn scipy seaborn
# For LLAMA module (optional, required only if using LLAMA module)
cd LLAMA
pip install -r requirements.txt
cd ..# Test PyTorch installation
python -c "import torch; print(f'PyTorch version: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"
# Test Jupyter (for notebook modules)
python -m jupyter --version# Start Jupyter
jupyter notebook
# Navigate to the module directory and open the desired notebook:
# - AutoEncoders/AutoEncoders.ipynb
# - CNN/LeNet.ipynb or CNN/AlexNet.ipynb
# - RNN/lstm.ipynbThen:
- Read the markdown cells for context and theory
- Run cells sequentially to see implementations and results
- Modify hyperparameters and re-run to experiment
cd LLAMA
# Basic training (uses TinyShakespeare dataset by default)
python train.py
# Advanced training with mixed precision and learning rate scheduling
python train_advanced.py --config medium --use_amp --max_iters 5000
# Generate text using trained model
python inference.py --prompt "To be or not to be"cd LLAMA
# Train with custom parameters
python train_advanced.py \
--config small \
--batch_size 32 \
--learning_rate 0.001 \
--max_iters 1000 \
--use_ampOur approach to replicating research papers follows these principles:
-
Paper-First Learning: Study the original research paper thoroughly before implementation
-
From-Scratch Understanding: Implement core components from scratch first, then optionally use framework optimizations
-
Faithful Reproduction: Maintain architectural fidelity to original papers while adapting for modern PyTorch
-
Clear Documentation: Every implementation includes:
- Architecture diagrams
- Detailed code comments explaining key concepts
- Comparison with original paper specifications
- Empirical validation and metrics
-
Modular Design: Each component can be understood independently
-
Reproducibility: Fixed random seeds, documented hyperparameters, and clear training procedures
-
Educational Value: Prioritize clarity and learning over performance optimizations
Each module includes validation metrics:
- AutoEncoders: Reconstruction MSE, latent space quality, visualization metrics
- CNN: Classification accuracy on MNIST/ImageNet, per-layer feature visualizations
- RNN/LSTM: Perplexity on text generation, sequence prediction accuracy
- LLAMA: Loss curves, text generation coherence, training stability metrics
See individual module READMEs for detailed performance benchmarks.
- Add comprehensive test suites for all modules
- Improve benchmark reporting with standardized metrics
- Add performance comparison tables across implementations
- Implement Transformer architectures from papers (Attention is All You Need)
- Add GAN implementations (DCGANs, StyleGAN concepts)
- Implement Vision Transformers (ViT)
- Diffusion model implementations
- Multimodal models (CLIP-like architectures)
- Continuous integration with automated testing
- Performance benchmarking against official implementations
We welcome contributions from the community! Please follow these guidelines:
- Check existing issues and PRs to avoid duplicates
- Ensure your contribution aligns with the repository's educational mission
- Read the implementation philosophy section above
-
Fork the repository on GitHub
-
Create a feature branch:
git checkout -b feature/your-feature-name
-
Make your changes:
- Follow the existing code style (PEP 8 for Python)
- Add comments explaining complex logic
- Include docstrings for functions and classes
- Update relevant README files
-
Test your changes:
- Verify the code runs without errors
- Test edge cases and different inputs
- Document any new dependencies
-
Commit with clear messages:
git commit -m "Add feature: [clear description of change]" -
Push and create a Pull Request:
git push origin feature/your-feature-name
We appreciate contributions in these areas:
- New paper implementations: CNNs, Vision Transformers, Diffusion models, etc.
- Bug fixes: Report with clear reproduction steps
- Documentation improvements: Better explanations, tutorials, visualizations
- Test additions: Unit tests and integration tests
- Performance optimizations: While maintaining code clarity
- Educational materials: Blogs, videos, tutorials using these implementations
- Follow PEP 8 for Python code
- Use meaningful variable names
- Add type hints where applicable
- Keep functions small and focused (single responsibility principle)
- Use comments for "why", not "what"
Q: I get "ModuleNotFoundError: No module named 'torch'" A: PyTorch is not installed. Run:
pip install torch torchvision torchaudioQ: CUDA is available but PyTorch isn't using it A: Verify CUDA installation and reinstall PyTorch for your CUDA version:
python -c "import torch; print(torch.cuda.is_available())"
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Q: Jupyter notebook won't start A: Ensure Jupyter is installed:
pip install jupyter jupyterlab
python -m jupyter notebookQ: Kernel keeps dying when running cells A: Increase memory limits or reduce batch size:
# In notebook, restart kernel and reduce batch_size parameterQ: "CUDA out of memory" error during training A: Reduce batch size or model dimension:
python train_advanced.py --config small --batch_size 8Q: Training loss is unstable or diverging A: Use gradient clipping and lower learning rate:
python train_advanced.py --config medium --learning_rate 0.0005 --gradient_clip 1.0Q: Very slow training on CPU A: Consider using GPU or reducing model size:
python train_advanced.py --config small --batch_size 8- Check Python version:
python --version(should be 3.9+) - Verify all dependencies:
pip list - Check PyTorch version:
python -c "import torch; print(torch.__version__)" - Review module README: Each module has troubleshooting in its README.md
If you use this repository in your research or educational materials, please cite it as:
@misc{paper_replicating,
title={Paper-Replicating: Implementation-First Learning of ML Papers},
author={Goyal, Mohit},
year={2024},
publisher={GitHub},
howpublished{\url{https://github.com/MohitGoyal09/Paper-Replicating}}
}Also cite the original papers for each module you use:
- AutoEncoders: Original autoencoder papers by Hinton & Salakhutdinov
- LeNet: LeCun, Y., et al. (1998). "Gradient-Based Learning Applied to Document Recognition"
- AlexNet: Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012). "ImageNet Classification with Deep Convolutional Neural Networks"
- LSTM: Hochreiter, S., & Schmidhuber, J. (1997). "Long Short-Term Memory"
- LLaMA: Touvron, H., et al. (2023). "LLaMA: Open and Efficient Foundation Language Models"
This repository is provided for educational and research purposes only. While the code and implementations are original, they are based on published research papers. Please respect the intellectual property and licensing of the original papers.
- This is an educational project, not an official implementation
- Code is provided "as-is" without warranty
- For production use, refer to official implementations and licensing terms
- Always cite original papers when using implementations from this repository
- Ensure you have the right to use datasets (MNIST, ImageNet, etc.) in your context
- Respect the licensing terms of the original research papers
- When citing papers, follow their official citation guidelines
- For commercial applications, verify licensing with original authors/institutions
- Hinton & Salakhutdinov (2006) - "Reducing the Dimensionality of Data with Neural Networks"
- LeCun et al. (1998) - "Gradient-Based Learning Applied to Document Recognition"
- Krizhevsky et al. (2012) - "ImageNet Classification with Deep Convolutional Neural Networks"
- Hochreiter & Schmidhuber (1997) - "Long Short-Term Memory"
- Touvron et al. (2023) - "LLaMA: Open and Efficient Foundation Language Models"
- PyTorch Official Documentation
- Deep Learning Book - Goodfellow, Bengio, Courville
- CS231n: CNNs - Stanford University
- CS224n: NLP - Stanford University
- Attention is All You Need - Original Transformer paper
- Issues: Report bugs and request features on GitHub Issues
- Discussions: Join community discussions for questions and ideas
- Contribute: See Contributing Guidelines
Last Updated: 2024 Maintainer: Mohit Goyal
If you find this repository helpful, please star β it to show your support!