A pure C++ machine learning library inspired by Andrej Karpathy's micrograd
Features • Getting Started • Examples • Usage • Contributing
Welcome to microgradpp! This repository is inspired by Andrej Karpathy's micrograd. microgradpp is a pure C++ machine learning library designed to make machine learning accessible to everyone.
microgradpp (micrograd++) is a C++ implementation of an automatic differentiation (autograd) engine with a neural network library built on top. It implements backpropagation (reverse-mode autodiff) over a dynamically built computational graph and provides tools to build and train neural networks.
| Feature | microgradpp | Python micrograd |
|---|---|---|
| Language | Modern C++17 | Python |
| Performance | Fast (compiled) | Interpreted |
| Computer Vision | ✅ OpenCV support | ❌ |
| Header-Only Option | ✅ | ✅ |
- ✅ Pure C++: Entirely implemented in C++ for high performance
- ✅ Inspired by micrograd: Brings the simplicity and educational value of micrograd to the C++ ecosystem
- ✅ Accessible Machine Learning: Designed to be easy to use, even for those new to machine learning or C++
- ✅ Computer Vision Support: OpenCV integration for image-based neural networks
- ✅ Header-Only Option: Can be used as a header-only library
- ✅ Tensor Class: Simplified data loading and manipulation
- ✅ Multiple Activation Functions: ReLU, Tanh, Sigmoid, and more
- CMake: Version 3.15 or higher
- C++ Compiler: Supports C++17 standard
- OpenCV: For visualization (optional)
-
Clone the repository:
git clone https://github.com/gautam-sharma1/microgradpp.git cd microgradpp -
Create a build directory:
mkdir build cd build -
Configure the project with CMake:
Build microgradpp only:
cmake ..
Build with examples and tests:
cmake .. -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON
Release build:
cmake .. -DCMAKE_BUILD_TYPE=Release
-
Build the project:
make
microgradpp is also available as a header-only library. Simply include the headers from include/ in your project:
#include "microgradpp/Value.hpp"
#include "microgradpp/MLP.hpp"After building, run the provided example:
./m++Here's a brief example of how to use microgradpp:
#include <iostream>
#include "Value.h"
int main() {
// Create values
auto a = microgradpp::Value::create(2.0);
auto b = microgradpp::Value::create(3.0);
// Build computation graph
auto c = a * b;
// Backpropagation
c->backProp();
std::cout << "a->grad: " << a->grad << std::endl; // 3.0
std::cout << "b->grad: " << b->grad << std::endl; // 2.0
return 0;
}Head over to the examples/ directory to explore various examples. To build examples:
cd build
cmake -DBUILD_EXAMPLES=ON .. && make
cd examples
./example_mlp # Run MLP exampleA simple multi-layer perceptron defined in mlp.cpp. Running it for 50 iterations:
The example in images.cpp trains a neural network to learn a German Shepherd puppy face:
microgradpp/
├── include/ # Header files
├── examples/ # Example programs
├── tests/ # Unit tests
├── public/ # Images and assets
├── scripts/ # Utility scripts
├── CMakeLists.txt # CMake configuration
├── main.cpp # Main example
└── README.md
The core building block for automatic differentiation:
auto x = microgradpp::Value::create(2.0);
auto y = microgradpp::Value::create(3.0);
auto z = x * y + x->pow(2);
z->backProp();
// x->grad and y->grad now contain gradients// Create a Multi-Layer Perceptron
// Input: 3 features, Hidden: 4 neurons, Output: 1 neuron
MLP model(3, {4, 4, 1});
// Forward pass
auto output = model(input_data);
// Training
model.zeroGrad();
loss->backProp();
// Update weights...We welcome contributions to microgradpp! Here are areas where you can help:
Modify CMakeLists to Add a Flag to Build Tests: ✅ DoneMake a Tensor Class: ✅ DoneAdd an Activation Function Enum or Class: ✅ DoneMake an Abstract Base Class for Layer and Value: ✅ Done- CI/CD Pipeline: Develop a pipeline using GitHub Actions
- More Examples: Add tutorials and use cases
- Python Interface: Create Python bindings
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Andrej Karpathy: For the original micrograd library and inspiration
- The C++ community for feedback and contributions
If you use microgradpp in your research or projects:
@software{microgradpp,
author = {Gautam Sharma},
title = {microgradpp: A C++ Machine Learning Library},
year = {2024},
url = {https://github.com/ggsharma/microgradpp}
}This project is licensed under the GNU GPL-3.0 License - see the LICENSE file for details.
Gautam Sharma - gsharma.dev
If you find microgradpp useful, please consider giving it a ⭐!


