Neural Network in pure C++ without PyTorch and TensorFlow.
Currently supports:
More to come.
It would be great if you could star this project on GitHub. Discussion and suggestions are more welcome!
Make sure you have CMake installed.
For Mac OS, run the following commands:
brew install cmake
For Linux, run the following commands:
sudo apt-get install cmake
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
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
.
Tensor
provides a lot of useful methods such as add
, sub
, mul
, div
, matmul
, transpose
, etc. You can find the detailed documentation in include/core/tensor.hpp
.
Note that Tensor
currently only supports up to 3-dimensional vectors.
#include "tensor.hpp"
// default type is double
Tensor<> your_tensor = { { 1.2, 2.3, 3.4 }, { 4.5, 5.6, 6.7 } }; // shape: (2, 3)
// Or you can create a tensor with a specific type
Tensor<int> your_int_tensor = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } // shape: (3, 3);
// Lots of operations are supported, including element-wise operations, matrix multiplication, etc.
Tensor<> transposed_tensor = your_tensor.transpose(); // shape: (3, 2)
// You can also create a tensor from a vector
vector<vector<double>> your_vec = { { 1.2, 2.3, 3.4 }, { 4.5, 5.6, 6.7 } };
Tensor<> your_tensor_from_vec = Tensor<>(your_vec);
The module API is defined in include/core/module.hpp
.
To build your custom module, follow the instructions in include/core/module.hpp
.
class MyModule : public nn::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
}
};
Please refer to the TODO list.