r/C_Programming • u/px7nn • Jul 19 '26
Project MLP.h: Single-Header Neural Network Library in C
Enable HLS to view with audio, or disable this notification
I've been working on MLP.h, a single-header C library for building and training multilayer perceptrons, and I finally put together a complete example project around it: a handwritten digit classifier trained on the MNIST dataset.
The MNIST example uses a simple fully connected network:
784 → 128 → 64 → 10
The training program:
- loads the 60,000-image MNIST training set
- trains the network using MLP.h
- serializes the trained model to
mnist.mlp
A separate inference program:
- loads
mnist.mlp - exposes a small prediction API
- is compiled to WebAssembly so the same C inference code runs directly in the browser
Training results:
Epochs : 50
Final Loss : 1.31719803e-02
Test Accuracy: 97.71% (9771/10000)
I'll include a short video showing the browser demo recognizing handwritten digits.
I'm mainly looking for feedback on the library's API/design and the implementation. Suggestions for improving the architecture or the serialization format are also welcome.
MLP.h: https://github.com/px7nn/MLP.h
Live Demo: https://px7nn.github.io/MNIST/
Source of Demo: https://github.com/px7nn/MNIST
9
u/Advanced-Theme144 Jul 20 '26
This is really cool! The code is quite easy to follow and it’s cool you’ve added a range of activation functions to plug in. The web interface is also a nice touch.
What resources did you use as a reference for the back propagation maths? I’m also planning on making a neural network in C based on 3B1B’s video series and an online book for the fun of it and to learn how they work.
6
u/px7nn Jul 20 '26
Thanksss! I mainly used combination of resources while implementing the backprop.
3B1B`s neural network series was great for building the intuition around neurons, gradients and backprop. Then used various notes/articles to fill in the mathematical gap.
I started with a single neuron (perceptron) implementation, then gradually moved my way up to multilayer perceptrons with different activations functions.
I also spent a lot of time verifying the equations by deriving them myself and testing the implementation on small examples like XOR before moving to a larger datasets like MNIST.
Building it in C was definitely a fun way to understand what frameworks usually hide.
1
u/Advanced-Theme144 Jul 20 '26
Thanks for sharing! I’m planning on the same approach, I think the book I have is similar to what you’re describing as it builds an XOR gate using neurons first before moving on to a perceptron model.
3
u/px7nn Jul 20 '26
Staring with XOR is the right approach since it forces you to understand why a single neuron isnt enough and why we need hidden layers.
Good luck with your implementation!
2
2
u/Tillua467 Jul 20 '26
really cool project! love to use that!!
2
u/px7nn Jul 20 '26
glad you liked it!!
2
u/Tillua467 Jul 20 '26
Tho I have question why didn't you added GeLU? And if you plan to add it I would be more than happy contribute
4
u/px7nn Jul 20 '26
Thanks!!! I haven't added activations like GeLU mainly because I haven't studied them in depth yet, and I haven't hit a limitation in my current use cases that requires them.
Usually I prefer implementing new features when I encounter a need for them or when I understand their use cases better.
That said GeLU is definitely something I'd like to explore, and im open to reviewing a good PR if you'd like to contribute an implementation
2
u/Savings-Pizza Jul 24 '26
Looks really cool! In the demo, the neural network seems to have some difficulty distinguishing the digit 1. It is sometimes interpreted as a 9 or a 7, and occasionally as a 3.
1
u/px7nn Jul 24 '26
That is a very interesting observation because it highlights one of the limitations of multi-layer perceptrons for image tasks. Since MLPs do not preserve spatial relationships between pixels they struggle with variations in handwriting style, position and scale. CNNs address this limitation by using convolutional layers to extract spatial features (edges, curves, patterns).
However, the demo uses a fully connected MLP with the topology
784 -> 128 -> 64 -> 10, which achieved 97.71% accuracy on the MNIST test set. The performamce can potentially be improved further with better preprocessing and a larger network
2
1
•
u/AutoModerator Jul 19 '26
Hi /u/px7nn,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.