Welcome to micrograd++. This repository is inspired by Andrej Karpathy's micrograd. micrograd++ is a pure C++ machine learning library designed to make machine learning accessible to everyone.
micrograd++ aims to provide a simple yet powerful framework for building and training machine learning models. By leveraging C++, it ensures performance efficiency and allows for deep integration with C++-based systems and applications.
- 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++.
- CMake: Version 3.15 or higher
- C++ Compiler: Supports C++17 standard
- opencv: For visualization
-
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:
cmake ..
or
cmake .. -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON ..
or
cmake .. -DCMAKE_BUILD_TYPE=Release
-
Build the project:
make
Microgradpp is also available as a header only library.
After building, you can run the provided example by executing the m++
binary:
./m++
CMakeLists.txt
: Build configuration for CMake.include/
: Header files for the library.src/
: Source files for the library.main.cpp
: Example usage of the library.
Here’s a brief example of how to use micrograd++:
#include <iostream>
#include "Value.h"
int main() {
auto a = microgradpp::Value::create(2.0);
auto b = microgradpp::Value::create(3.0);
auto c = a * b;
c->backProp();
std::cout << "a->grad: " << a->grad << std::endl;
std::cout << "b->grad: " << b->grad << std::endl;
return 0;
}
Head over to the examples directory to play around with the examples. To build examples, configure cmake from the root directory as follows:
cd build
cmake -DBUILD_EXAMPLES=ON .. && make
cd examples
./example_mlp # to run mlp example
This will produce executables in the build folder
A simple multi layer perceptron is defined in mlp.cpp. Running it for 50 iterations gives the following result:
The example in images.cpp learns a cute german shephard puppy face. The output of it is as follows:
We welcome contributions to micrograd++. Here are a few areas where you can help:
Modify CMakeLists to Add a Flag to Build Tests: Enhance the build configuration to optionally include tests.Make a Tensor Class: Create a Tensor class to simplify data loading and manipulation.Add an Activation Function Enum or Class: Implement a flexible way to handle different activation functions.Make an Abstract Base Class for Layer and Value: Design abstract base classes to improve the architecture and extensibility.- CI/CD pipeline: Develop a pipeline using github actions to execute tests automatically on a commit.
- Improve README: Add few examples on how to leverage this library.
- Python Interface??: Probably make a python interface
This project is licensed under the GNU License - see the LICENSE file for details.
- Andrej Karpathy: For the original micrograd library and inspiration.
- Gautam Sharma: gsharma