Skip to content

Kamzie1/MLask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MLask

A custom c++ deep learning library.
Explore the docs »

View Demo · Report Bug >

Table of Contents
  1. About The Project

About The Project

This is a custom c++ deep learning library. It was meant to be a challenge to understand how all the professional ML libraries work under the hood.

Features

  • Fully connected layers
  • Activation functions
  • Backpropagation algorithm
  • Export to ONNX format

(back to top)

Built With

(back to top)

Getting Started

Prerequisites

  • CMake 3.28 or higher
  • c++23 compatible compiler

Installation

include(FetchContent)
FetchContent_Declare(MLask
    GIT_REPOSITORY https://gitlab.com/Kamzie1/MLask.git
    GIT_TAG 1.0.0
    GIT_SHALLOW ON
    EXCLUDE_FROM_ALL
    SYSTEM)
FetchContent_MakeAvailable(MLask)

(back to top)

Usage

Defining a model

You need to create a model with in, out, parameters. Optionally you can add number of layers, and whether to draw progress bar while learning. Then you add layers.

Model model(1, 1, 1,); // input of size 1, output of size 1, 1 layer
// You can add specific mlask layers
model.addFullyConnectedLayer<1, 1>(); // Correct output->input flow is validated
model.addActivationFunction(InternalActivationFunction::Relu); //Only builtin activation functions listed in InternalActivationFunctioon enum
model.addLambdaActivationFunction([](float_t x){ return 0.5*x*x; }, [](float_t x){ return x; } ); // A layer that should be used only for prototyping.

Adding your own layer

You can define and add your own layer. All you need to do is create a class that derives from abstract Layer class.

class CustomLayer : public Layer{
public:
    // You need to override these functions. However if you want your layer to be exportable to ONNX for example, you also need to override tryConvertToONNX.
    vectorOut forward(vectorIn input) override;
    vectorIn backward(vectorOut error) override;
    void fit(float_t learning_rate) override {}
};

model.addLayer<CustomLayer>();// You can add your custom layer to the model like this.
model.addLayer(std::make_unique<CustomLayer>()); // or like this

Training the model

float_t learning_rate = 0.001;
for(std::size_t epochs=0; epochs < EPOCHS; epochs++){
  for(int x = 0;x<=SIZE;x++){
    model.backprop<DerivedStandardMean>(vectorIn{{(x-(SIZE/2.f))/(SIZE/2.f)}}, vectorOut{{Y[x]}}); // in arguments we construct an Eigen vector using mlask predefined type for simplicity
  }
  model.fit(learning_rate); // if your model has log set to true and it has correct number of epochs, then on fit it will update progress bar.
}

Exporting the model

    std::cout<<model.str();
    model.exportToONNX("onnx_format.onnx", "One Neuron Neural Network");

For more examples, please refer to the Documentation

(back to top)

License

Distributed under the MIT. See LICENSE for more information.

(back to top)

About

Custom library for deep learning written in C++

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages