MLask 1.0.0
A custom c++ deep learning library
Loading...
Searching...
No Matches
Model.hpp
Go to the documentation of this file.
1#pragma once
3#include "Layer.hpp"
4#include "ErrorFunction.hpp"
5#include <Eigen/Core>
6#include <cstddef>
7#include <filesystem>
8#include <memory>
9#include <vector>
11#include "exceptions.hpp"
12#include <iostream>
13#include <filesystem>
14
15#ifdef DEBUG
16
17#define LOG(X) std::cout<<"[INFO] "<<(X)<<std::endl;
18#define WARN(X) std::cout<<"[WARN] "<<(X)<<std::endl;
19#define ERR(X) std::cerr<<"[ERROR] "<<(X)<<std::endl;
20
21#else
22
23#define LOG(X)
24#define WARN(X)
25#define ERR(X)
26
27#endif
28
29
30namespace mlask {
32class Model {
33 private:
34 std::size_t in_;
35 std::size_t out_;
36 std::vector<std::unique_ptr<Layer>> layers_;
37 std::size_t epochs_;
38 bool log_;
39 std::size_t epoch_ = 0;
40
41 public:
50 Model(std::size_t in, std::size_t out, std::size_t size = 0, std::size_t epochs=0, bool log=false);
51
57 void addLayer(std::unique_ptr<Layer> layer);
58
64 template<TLayer layer, typename... Args>
65 void addLayer(Args&&... args);
66
73 template<std::size_t in, std::size_t out>
81
88 template<ErrorFunction err>
89 void backprop(vectorIn input, vectorOut expected);
90
95 void fit(float_t learning_rate);
101 vectorOut forward(vectorIn input) const;
102
110 template<ErrorFunction err>
111 [[nodiscard]] float_t error(vectorIn input, vectorOut expected)const;
112
113
121 template<ErrorFunction err>
122 [[nodiscard]] float_t whole_error(Eigen::Matrix<float_t, Eigen::Dynamic, Eigen::Dynamic> input, Eigen::Matrix<float_t, Eigen::Dynamic, Eigen::Dynamic> expected)const;
123
129 const Layer* getLayer(std::size_t index)const{ return layers_[index].get(); }
130 const Layer* operator[](std::size_t index)const { return getLayer(index); }
131
133 [[nodiscard]] std::string str()const;
134
141 void exportToONNX(std::filesystem::path path, std::string name = "MLask Model") const;
142
143private:
151 void convertToONNX(onnx::ModelProto& model, std::string name)const;
153 inline std::size_t lastOut()const{
154 if(layers_.empty()){
155 return in_;
156 }
157 return layers_.back()->getOut();
158 }
159};
160
161
162template <std::size_t in, std::size_t out>
164 std::size_t last_out = lastOut();
165 if(in != last_out){
166 ERR("Input size of the layer does not match the output size of the previous layer");
167 throw ArchitectureError("Input size of the layer does not match the output size of the previous layer", layers_.size());
168 }
169 else{
170 layers_.push_back(std::make_unique<FullyConnectedLayer<in, out>>());
171 LOG("Added FullyConnected Layer");
172 }
173}
174
175template<TLayer layer, typename... Args>
176void Model::addLayer(Args&&... args){
177 layers_.push_back(std::make_unique<layer>(std::forward<Args>(args)...));
178}
179
180template<ErrorFunction err>
185
186template<ErrorFunction err>
187float_t Model::whole_error(Eigen::Matrix<float_t, Eigen::Dynamic, Eigen::Dynamic> inputs, Eigen::Matrix<float_t, Eigen::Dynamic, Eigen::Dynamic> expected_values)const{
189 std::size_t size = inputs.rows();
190 for(std::size_t i=0;i<size;i++){
191 error_value += error<err>(inputs.row(i).transpose(), expected_values.row(i).transpose());
192 }
193 return error_value / size;
194}
195
196template<ErrorFunction err>
197void Model::backprop(vectorIn input, vectorOut expected){
198 for (const std::unique_ptr<Layer> &layer : layers_) {
199 input = layer->forward(input);
200 }
202 for (auto it = layers_.rbegin(); it!=layers_.rend(); ++it){
203 error = (*it).get()->backward(error);
204 }
205}
206
207} // namespace mlask
#define ERR(X)
Definition Model.hpp:25
#define LOG(X)
Definition Model.hpp:23
Errors associated with Neural Network architecture e.g. wrong connection beetwen layers.
Definition exceptions.hpp:6
Class representing fully connected layer.
Definition FullyConnectedLayer.hpp:17
static float_t error_scalar(vectorOut result, vectorOut expected)
get the error as a scalar value
Definition GenericErrorFunction.hpp:24
static vectorOut error(vectorOut result, vectorOut expected)
calculates derived error
Definition GenericErrorFunction.hpp:16
Base class for all layers in the neural network.
Definition Layer.hpp:10
Class representing a neural network model.
Definition Model.hpp:32
void exportToONNX(std::filesystem::path path, std::string name="MLask Model") const
exports the model to a file in ONNX format
Definition Model.cpp:96
std::string str() const
Returns a string representation of the model.
Definition Model.cpp:107
const Layer * operator[](std::size_t index) const
Definition Model.hpp:130
void addLayer(std::unique_ptr< Layer > layer)
Adds a layer to the model(Layer is an abstract class, see Layer.hpp for more details)
Definition Model.cpp:17
vectorOut forward(vectorIn input) const
calculates the output for given input
Definition Model.cpp:29
float_t error(vectorIn input, vectorOut expected) const
calculates an error of a model
Definition Model.hpp:181
void addLambdaActivationFunction(actfunc func, actfunc derv)
creates and adds ActivationFunction layer with give function form and derived form
Definition Model.cpp:47
void fit(float_t learning_rate)
Fits the entire model(every layer added)
Definition Model.cpp:36
void backprop(vectorIn input, vectorOut expected)
performs backpropagation algorithm
Definition Model.hpp:197
const Layer * getLayer(std::size_t index) const
gets the layer at a given index
Definition Model.hpp:129
void addFullyConnectedLayer()
creates and adds fullyConnectedLayer with given in and out neurons
Definition Model.hpp:163
float_t whole_error(Eigen::Matrix< float_t, Eigen::Dynamic, Eigen::Dynamic > input, Eigen::Matrix< float_t, Eigen::Dynamic, Eigen::Dynamic > expected) const
calculates an error of a model based on many inputs
Definition Model.hpp:187
concept ensuring a class derives from Layer
Definition Layer.hpp:33
Definition LeakyRelu.hpp:4
Eigen::Matrix< float_t, Eigen::Dynamic, 1 > vectorOut
Definition types.hpp:17
float float_t
Definition types.hpp:12
Eigen::Matrix< float_t, Eigen::Dynamic, 1 > vectorIn
Definition types.hpp:16
std::function< float_t(float_t)> actfunc
Definition types.hpp:19