r/learnmachinelearning • • 17d ago • Bravo 1

Project I built a handwritten digit recognizer Neural Network completely from scratch in C++ (No Python, No ML Libraries, Custom Matrix Math)

Post image

Hey everyone,

I wanted to truly understand the math and mechanics behind neural networks without relying on black box frameworks. So, I decided to build a handwritten digit recognizer from the ground up using C++20.

What I built:

  • Custom Dataset (No MNIST): Instead of using MNIST, I hand-drew 1000 digit samples (20×20 pixels) one by one to build my own custom CSV dataset for training and testing.
  • Implemented my own Matrix class from scratch to handle dot products, transpositions, and element-wise operations.
  • A Feed-Forward Network using Sigmoid activations. No biases, just weights initialized with Xavier/Glorot normal distribution.
  • Implemented backpropagation and stochastic gradient descent manually.
  • I built an interactive ASCII canvas using the Win32 API. You can draw a digit directly in the terminal with your mouse, hit space, and watch the network guess it in real-time.

Here is the GitHub repo if you want to check out:

https://github.com/elevenMiles/digit-recognizer-cpp

I'd love any feedback on the math, the architecture, or the C++ implementation!

212 Upvotes

22 comments sorted by

12

u/BrilliantAntique963 17d ago

Where did you learn how to do it? I'm among those who wants to build things from scratch too. And kinda dislike shortcuts and pre built libraries. So... What would suggest if anything, to a learner 

18

u/elevenMiles11 17d ago

I learned the core concepts from the book 'Make Your Own Neural Network' by Tariq Rashid. It does a fantastic job of explaining the math, forward passes, and backpropagation in a simple way.

The book actually implements everything in Python. But since I already knew C++ and linear algebra, I decided to challenge myself by translating those concepts into C++ and writing my own Matrix class instead of using NumPy.

My Advice is write a Matrix class (dot product, transpose, element-wise math). Once your linear algebra foundation is solid, building the neural network on top of it becomes 10x easier.

Before writing code for backpropagation, write down the linear equations and partial derivatives on paper. This makes translating the math into code much easier.

Writing it by hand gives you an intuition for gradients that you just can't get from standard PyTorch or TensorFlow tutorials.

1

u/Datscie_249 16d ago

this might sound ridiculous, but where can i find the ebook? since in my country, it's quite hard to have my hands on international books, especially when it is science related...

1

u/elevenMiles11 15d ago

You can find the digital ebook edition on amazon or google play.

Also check out Tariq Rashid's official GitHub repository he uploaded all the code, diagrams, and jupyter notebooks from the book.

4

u/CyberAtman 17d ago

This free ebook is a good one to learn neural networks from scratch and has the same example of digit recognizer: https://neuralnetworksanddeeplearning.com/index.html

1

u/DadAndDominant 17d ago

The dataset is mnist

Then pick some network like convolition network

Learn about the backprop or genetic algos to know how to train it

Then implement what you need in lang of your choosing

Then train it

The rest is just a bit of shell magic

4

u/elevenMiles11 17d ago

Actually, I didn't use standard MNIST. I hand drew 1000 digits myself to create a custom CSV dataset

Also, it's a simple feed forward network, no CNNs here, and the terminal interactivity was built using the Win32 API rather than shell magic.

1

u/DadAndDominant 17d ago

Wim32 API

Shell magic!! :D

1

u/elevenMiles11 17d ago

Yeah, it's true 😅

2

u/DadAndDominant 17d ago

Nice!

2

u/elevenMiles11 17d ago

Thanks! Appreciate it!

2

u/AddictedAcid 17d ago

so good man. love it!

1

u/elevenMiles11 17d ago

Thanks man! appreciate it!

2

u/captain_obvious_here 17d ago

Just wondering: Does your code analyze the way the drawing is done (starting point, directions, ending point), or only the finished drawing?

1

u/elevenMiles11 17d ago

It only analyzes the finished drawing.

It doesn't track stroke trajectory, direction, speed, or starting-ending points. When you press Space, the current 20×20 canvas buffer is captured as a static pixel grid, converted into a 400×1 input matrix and passed into the input layer of the network.

So it treats the final result as a 2D image.

2

u/captain_obvious_here 16d ago

This is great work!

It's a classic ML exercise, but most people use MNIST and Python libs. Doing it on your won, in C++, almost from scratch is a great way to understand how this all works. Grats!

2

u/anadalg 16d ago

A few years ago I did the same.
Video: https://youtu.be/oCElhUzadaA
Source code: https://github.com/albertnadal/Tensar

2

u/elevenMiles11 15d ago

OpenGL visualization is very cool! Loved seeing the activation surfaces rendered in real time. Awesome work, thanks for sharing the repo.

2

u/Fun_Recording_6485 11d ago

This is awesome. I’m jealous lowkey.

2

u/elevenMiles11 10d ago

Appreciate it. Honestly all you need is some C++, linear algebra, and a bit of calculus. If you're curious about it, you can definitely build something like this too.

1

u/Fun_Recording_6485 10d ago

Shoot I might, I gotta get through this semester tho. I totally underestimated how difficult grad school is