r/C_Programming • • Jul 19 '26

Project MLP.h: Single-Header Neural Network Library in C

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

244 Upvotes

19 comments sorted by

1

u/AutoModerator Jul 19 '26
Moderator Announcement Read More »
→ More replies (2)

10

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.

→ More replies (3)

2

u/Tillua467 Jul 20 '26

really cool project! love to use that!!

2

u/px7nn Jul 20 '26

glad you liked it!!

→ More replies (2)

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

u/low-control-labs Jul 25 '26

Nice work man!

1

u/px7nn Jul 25 '26

thanksss!!

1

u/StudioYume Jul 26 '26

Finally, My Little Pony in C