Learn how neural networks actually work by building one completely from scratch using only NumPy!
This project teaches you exactly how neural networks work by building one from scratch to recognize handwritten Devanagari characters (the script used for Hindi, Nepali, and Sanskrit).
No fancy libraries - just pure Python and NumPy mathematics!
- How neurons actually process information
- What forward propagation really means
- How backpropagation updates weights (the "learning" part)
- Why we use different activation functions
- How gradient descent finds the best solution
- What overfitting looks like and how to spot it
git clone https://github.com/yourusername/devanagariMNIST.git
cd devanagariMNISTpip install numpy pandas matplotlib jupyterDownload the preprocessed dataset from Kaggle: Devanagari MNIST Dataset
Place the CSV files in the data/ folder:
data/
├── trainDataMNIST.csv
└── testDataMNIST.csv
jupyter notebook devanagarimnist.ipynbThat's it! Start from the first cell and run everything step by step.
devanagariMNIST/
├── 📊 data/ # Dataset files
│ ├── trainDataMNIST.csv # Training data (73,600 images)
│ └── testDataMNIST.csv # Test data (18,400 images)
├── 🔬 preProcessing/ # Data cleaning scripts
│ └── preProcess.ipynb # Data preparation notebook
├── 🧠 devanagarimnist.ipynb # MAIN NOTEBOOK (start here!)
├── 📖 README.md # This file
- Load 92,000 images of Devanagari characters
- Understand the train/test split (why it matters)
- See what the data actually looks like
- Convert text labels to numbers
- Create one-hot encoding (and learn why we need it)
- Shuffle data to prevent bias
- Separate features from targets
- Build a 4-layer network from scratch
- Understand what each layer does
- Learn about activation functions (tanh, softmax)
- See how information flows through the network
- Forward Propagation: How the network makes predictions
- Loss Function: How we measure "wrongness"
- Backpropagation: How the network learns from mistakes
- Gradient Descent: How we find the best weights
- Train the network epoch by epoch
- Watch loss decrease and accuracy improve
- Understand mini-batch processing
- Monitor for overfitting
- Plot training curves
- Interpret what the graphs mean
- Understand when training is successful
- Learn warning signs of problems
Devanagari is the script used to write Hindi, Nepali, Sanskrit, and other languages. Think of it like the "alphabet" for these languages.
Examples: क (ka), ख (kha), ग (ga), घ (gha)
- 46 different characters (like having 46 letters in an alphabet)
- 92,000 total images (32×32 pixels each, grayscale)
- Training set: 73,600 images (80%) - for teaching the network
- Test set: 18,400 images (20%) - for testing how well it learned
- Balanced: Each character has exactly the same number of examples
- Perfect size: Not too big, not too small for learning
- Clear images: 32×32 pixels are easy to visualize
- Real challenge: 46 classes is complex enough to be interesting
- Cultural significance: Learn about non-Latin scripts
Imagine you're trying to recognize handwritten letters. A neural network is like having thousands of tiny decision-makers (neurons) that each look at different parts of the image and vote on what letter they think it is.
- Each neuron looks at one pixel of the 32×32 image
- "Is this pixel dark or light?"
2. Hidden Layers (512 → 256 → 128 neurons)
- First hidden layer: Detects basic shapes (lines, curves)
- Second hidden layer: Combines shapes into parts (tops of letters, bottoms)
- Third hidden layer: Recognizes whole character patterns
- Each neuron represents one Devanagari character
- The neuron with the highest value is the network's guess
- Show the network an image: "Here's a क (ka)"
- Network makes a guess: "I think it's ग (ga)" (wrong!)
- Calculate the error: "You were wrong by this much"
- Adjust the weights: "Next time, pay more attention to these features"
- Repeat 57,600 times: Network gets better and better
- Pure Implementation: No TensorFlow, PyTorch, or Keras black boxes
- Mathematical Understanding: See every equation in action
- Debugging Skills: Understand when and why things go wrong
- Performance Analysis: Interpret training curves and metrics
- Real Problem Solving: Build something that actually works
- Complete Transparency: Every line of code is explained
- Educational Focus: Built for learning, not just results
- Step-by-Step: Never assumes prior knowledge
- Real Data: Work with actual handwriting recognition
- Practical Skills: Applicable to any neural network problem
- Training Accuracy: Should reach 85-95% after enough epochs
- Validation Accuracy: Should be close to training accuracy (within 5-10%)
- Loss: Should decrease steadily during training
- Time: Training takes a few minutes on a modern laptop
Epoch 1/100 -> Train Loss: 3.8234, Val Loss: 3.7891 | Train Acc: 12.45%, Val Acc: 13.20%
Epoch 10/100 -> Train Loss: 2.1543, Val Loss: 2.2156 | Train Acc: 45.67%, Val Acc: 44.32%
Epoch 50/100 -> Train Loss: 0.8932, Val Loss: 0.9445 | Train Acc: 78.90%, Val Acc: 76.54%
Epoch 100/100 -> Train Loss: 0.4521, Val Loss: 0.5234 | Train Acc: 89.12%, Val Acc: 87.65%
pip install numpy pandas matplotlib- Download the dataset from the Kaggle link above
- Make sure CSV files are in the
data/folder
- Lower the learning rate:
learningRate=0.001instead of0.01 - Train for more epochs:
epochs=100instead of10 - Check your data is shuffled properly
- This is overfitting - the network memorized training data
- Try a smaller network:
layers=[256, 128]instead of[512, 256, 128] - Add more training data or use regularization techniques
- This usually means gradients exploded
- Use a much smaller learning rate:
learningRate=0.0001 - Check that your data is normalized (pixel values 0-1)
- Try Different Architectures: Change layer sizes
[128, 64]vs[512, 256, 128] - Experiment with Learning Rates: Try
0.001,0.01,0.1and see what happens - More Epochs: Train for 50, 100, or 200 epochs
- Visualize Predictions: Show which characters the network gets wrong
- Add Dropout: Prevent overfitting by randomly turning off neurons
- Learning Rate Scheduling: Start high, decrease over time
- Different Optimizers: Implement momentum or Adam optimizer
- Data Augmentation: Rotate, scale, or shift images for more training data
- Convolutional Neural Network: Preserve spatial structure of images
- Attention Mechanisms: Let the network focus on important parts
- Transfer Learning: Use pretrained features
- Deploy as Web App: Make it usable by others online
Found a bug? Have a suggestion? Want to add features?
- Fork the repository
- Create a feature branch:
git checkout -b amazing-feature - Make your changes: Add code, fix bugs, improve documentation
- Test thoroughly: Make sure everything still works
- Submit a pull request: Explain what you changed and why
- Add more comments to complex functions
- Create additional visualizations
- Write tutorials for specific concepts
- Add support for other languages/scripts
- Optimize training speed
- Add more activation functions
This project uses the Devanagari Handwritten Character Dataset from the UCI Machine Learning Repository:
@misc{prabesh_sagar_baral_shailesh_acharya_prashnna_gyawali_2025,
title={mnistDevanagari},
url={https://www.kaggle.com/ds/8256842},
DOI={10.34740/KAGGLE/DS/8256842},
publisher={Kaggle},
author={Prabesh Sagar Baral and Shailesh Acharya and Prashnna Gyawali},
year={2025}
}Original Dataset: UCI ML Repository
Preprocessed Version: Kaggle Dataset
- UCI Machine Learning Repository for the original dataset
- NumPy Community for the amazing mathematical library
- Jupyter Project for the interactive notebook environment
- Open Source Community for making education accessible to everyone
- Read the notebook carefully - every step is explained
- Check the troubleshooting section above
- Search existing issues on GitHub
- Create a new issue with:
- What you were trying to do
- What error you got
- Your Python version and operating system
- GitHub Issues: For bugs and feature requests
If this project helped you understand neural networks, please:
- ⭐ Star this repository
- 🍴 Fork it and try your own experiments
- 📢 Share it with friends learning AI/ML
- 💝 Contribute improvements back to the community
Happy Learning! 🎉
Remember: The goal isn't just to get good accuracy, but to understand exactly HOW and WHY neural networks work. Take your time, experiment, and don't be afraid to break things!