Skip to content

A PyTorch-like neural network framework in pure C++ from scratch, using only C++ STL.

License

Notifications You must be signed in to change notification settings

lucaswychan/neuralnet-cpp

Repository files navigation

neuralnet-cpp

C++ C++ Unit Tests GitHub license badge

This is a PyTorch-like neural network framework in pure C++ from scratch, using only C++ STL.

Currently supports:

More to come.

Current achievements: achieving 95% accuracy on MNIST.

Note

To support and foster the growth of the project, you could ⭐ star this project on GitHub. Discussion and suggestions are more welcome!

Get Started

This project requires C++23, GCC >= 13.3, and CMake >= 3.20 to compile. Please make sure you have GCC and CMake installed.

For Mac OS, run the following commands:

brew install cmake
brew install gcc

For Linux, run the following commands:

sudo apt update
sudo apt install cmake
sudo apt install build-essential

Get the repository:

git clone https://github.com/lucaswychan/neuralnet-cpp.git
cd neuralnet-cpp

Build the project:

./build.sh

Run the example:

./main.sh

Tensor from Scratch

I implemented a tensor from scratch as well and integrate it to my neural network implementation. The detailed implementation of Tensor can be found in include/core/tensor.hpp.

For more details about tensor, please refer to tensor tutorial.

Module API

The module API is defined in include/core/module.hpp.

To build your custom module, follow the instructions in include/core/module.hpp.

Example usage

#include <module.hpp>
using namespace nn;

// Your code here
class MyModule : public Module {
    public:
        virtual Tensor<> forward(const Tensor<>& input) override {
            // Your code here
        }
        virtual Tensor<> backward(const Tensor<>& grad_output) override {
            // Your code here
        }
        virtual void update_params(const float lr) override {
            // Your code here
        }
};

TODO

Please refer to the TODO list.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A PyTorch-like neural network framework in pure C++ from scratch, using only C++ STL.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages