https://github.com/fszewczyk/shkyera-grad
micrograd, but in C++ and better
https://github.com/fszewczyk/shkyera-grad
artificial-intelligence autograd machine-learning micrograd neural-network
Last synced: 5 months ago
JSON representation
micrograd, but in C++ and better
- Host: GitHub
- URL: https://github.com/fszewczyk/shkyera-grad
- Owner: fszewczyk
- License: other
- Created: 2023-03-25T16:17:27.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-10T20:08:01.000Z (over 1 year ago)
- Last Synced: 2025-02-10T20:29:16.093Z (over 1 year ago)
- Topics: artificial-intelligence, autograd, machine-learning, micrograd, neural-network
- Language: C++
- Homepage: https://fszewczyk.github.io/shkyera-grad/
- Size: 441 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Shkyera Grad
micrograd, but in C++ and better.
[](https://fszewczyk.github.io/shkyera-grad/index.html)
[](https://github.com/fszewczyk/shkyera-grad/actions/workflows/linux.yml)
[](https://github.com/fszewczyk/shkyera-grad/actions/workflows/macos.yml)
[](https://github.com/fszewczyk/shkyera-grad/actions/workflows/windows.yml)
[](https://github.com/fszewczyk/shkyera-grad/blob/master/LICENSE)
This is a small header-only library of a scalar-valued autograd based on [Andrej Karpathy's micrograd](https://github.com/karpathy/micrograd). It provides a high-level, PyTorch-like API for creating and training simple neural networks.
It supports multiple optimizers, such as Adam or SGD, all the most common activation functions and basic types of neural layers. All of it wrapped in a simple, header-only library.
## Usage
Check out oour [Get Started Guide](https://fszewczyk.github.io/shkyera-grad/md_docs_tutorials_GetStarted.html) to learn the basics of _Shkyera Engine_.
## Showcase
Here's a small example showcasing a feed-forward network learning the XOR function. Check out the `examples/` folder for more examples.
```cpp
#include "shkyera-grad/include/ShkyeraGrad.hpp"
int main() {
using namespace shkyera;
using T = Type::float32;
// This is our XOR dataset. It maps from Vec32 to Vec32
Dataset data;
data.addSample(Vec32::of(0, 0), Vec32::of(0));
data.addSample(Vec32::of(0, 1), Vec32::of(1));
data.addSample(Vec32::of(1, 0), Vec32::of(1));
data.addSample(Vec32::of(1, 1), Vec32::of(0));
// The is the data loader, it will take care of batching
size_t batchSize = 2;
bool shuffle = true;
DataLoader loader(data, batchSize, shuffle);
auto network = SequentialBuilder::begin()
.add(Linear32::create(2, 15))
.add(ReLU32::create())
.add(Linear32::create(15, 5))
.add(ReLU32::create())
.add(Linear32::create(5, 1))
.add(Sigmoid32::create())
.build();
auto optimizer = Adam32(network->parameters(), 0.1);
auto lossFunction = Loss::MSE;
for (size_t epoch = 0; epoch < 100; epoch++) { // We train for 100 epochs
auto epochLoss = Val32::create(0);
optimizer.reset(); // Reset the gradients
for (const auto &[x, y] : loader) { // For each batch
auto pred = network->forward(x); // We get some prediction
epochLoss = epochLoss + Loss::compute(lossFunction, pred, y); // And calculate its error
}
optimizer.step(); // Update the parameters
auto averageLoss = epochLoss / Val32::create(loader.getTotalBatches());
std::cout << "Epoch: " << epoch + 1 << " Loss: " << averageLoss->getValue() << std::endl;
}
for (auto &[x, y] : data) { // Go through each example
auto pred = network->forward(x); // We get some prediction
std::cout << x << " -> " << pred[0] << "\t| True: " << y[0] << std::endl;
}
}
```