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;
36 std::vector<std::unique_ptr<Layer>> layers_;
39 std::size_t epoch_ = 0;
50 Model(std::size_t in, std::size_t out, std::size_t size = 0, std::size_t epochs=0,
bool log=
false);
57 void addLayer(std::unique_ptr<Layer> layer);
64 template<
TLayer layer,
typename... Args>
73 template<std::
size_t in, std::
size_t out>
88 template<ErrorFunction err>
110 template<ErrorFunction err>
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;
129 const Layer*
getLayer(std::size_t index)
const{
return layers_[index].get(); }
133 [[nodiscard]] std::string
str()
const;
141 void exportToONNX(std::filesystem::path path, std::string name =
"MLask Model")
const;
151 void convertToONNX(onnx::ModelProto& model, std::string name)
const;
153 inline std::size_t lastOut()
const{
157 return layers_.back()->getOut();
162template <std::
size_t in, std::
size_t out>
164 std::size_t last_out = lastOut();
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());
171 LOG(
"Added FullyConnected Layer");
175template<
TLayer layer,
typename... Args>
177 layers_.push_back(std::make_unique<layer>(std::forward<Args>(
args)...));
180template<ErrorFunction err>
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++){
196template<ErrorFunction err>
198 for (
const std::unique_ptr<Layer> &
layer : layers_) {
202 for (
auto it = layers_.rbegin();
it!=layers_.rend(); ++
it){
#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