https://github.com/ajlekcahdp4/scalgrad
A tiny Autograd engine for C++ with small neural network on top
https://github.com/ajlekcahdp4/scalgrad
autograd cpp neural-networks
Last synced: 3 months ago
JSON representation
A tiny Autograd engine for C++ with small neural network on top
- Host: GitHub
- URL: https://github.com/ajlekcahdp4/scalgrad
- Owner: ajlekcahdp4
- License: mit
- Created: 2023-01-26T11:26:17.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-31T09:03:33.000Z (over 2 years ago)
- Last Synced: 2025-02-09T15:46:40.747Z (4 months ago)
- Topics: autograd, cpp, neural-networks
- Language: C++
- Homepage:
- Size: 147 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scalgrad
A minimal C++ autograd engine implementation for scalars. Inspired by [micrograd](https://github.com/karpathy/micrograd). Also implemented small neural network on top of the engine.## Example
Here is the example of engine usage:
```cpp
#include "engine.hpp"using scal = typename red_engine::scalar;
int main() {
auto a = scal::create(2.0);
auto b = scal::create(3.0);
auto c = scal::create(4.0);
auto d = c - b;
auto r = red_engine::exponentiate((a + b) * c + d);
r->backprop();
r->draw_dot("dump.dot");
```Example of the neural network usage:
```cpp
#include "engine.hpp"
#include "nn.hpp"using scal = typename red_engine::scalar;
int main() {
std::vector x{scal::create(1.0), scal::create(-2.0)};auto n = nn::MLP{2, {2, 1}};
auto tmp = n(x);
auto r = std::get<1>(tmp);
r->backprop();
r->draw_dot("dump.dot");
}
```
The graph of the neural network example is [here](example/example.png).
You can also try to run the [example](example/src/main.cc) yourself.## How to build
```terminal
git clone [email protected]:ajlekcahdp4/scalgrad.git
cd scalargrad
cmake -S . -B build -DNOGTEST=False
make -C build -j8 install
```
## How to run (after build)
```terminal
# to run unit tests:
cd build
ctest# to run the example
cd example
bin/example
```