Giter Club home page Giter Club logo

shkyera-grad's Introduction

Shkyera Grad

micrograd, but in C++ and better.

Documentation LinuxBuild MacOSBuild WindowsBuild LICENSE

This is a small header-only library of a scalar-valued autograd based on Andrej Karpathy's 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 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.

#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<Vec32, Vec32> 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<Type::float32>::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<T>;

    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;
    }
}

shkyera-grad's People

Contributors

fszewczyk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

shkyera-grad's Issues

Why * in operator div ?

x *= Value<T>::create(val);

template Vector operator/(Vector x, T val) {
x *= Value::create(val);
return x;
}

Operator * is identical:
template Vector operator*(Vector x, T val) {
x *= Value::create(val);
return x;
}

Is that a bug or I'm missing the point?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.