MLask 1.0.0
A custom c++ deep learning library
Loading...
Searching...
No Matches
Public Member Functions | List of all members
mlask::Model Class Reference

Class representing a neural network model. More...

#include <Model.hpp>

Public Member Functions

 Model (std::size_t in, std::size_t out, std::size_t size=0, std::size_t epochs=0, bool log=false)
 Constructs a model.
 
void addLayer (std::unique_ptr< Layer > layer)
 Adds a layer to the model(Layer is an abstract class, see Layer.hpp for more details)
 
template<TLayer layer, typename... Args>
void addLayer (Args &&... args)
 Adds a layer to the model(Layer is an abstract class, see Layer.hpp for more details)
 
template<std::size_t in, std::size_t out>
void addFullyConnectedLayer ()
 creates and adds fullyConnectedLayer with given in and out neurons
 
void addLambdaActivationFunction (actfunc func, actfunc derv)
 creates and adds ActivationFunction layer with give function form and derived form
 
template<ErrorFunction err>
void backprop (vectorIn input, vectorOut expected)
 performs backpropagation algorithm
 
void fit (float_t learning_rate)
 Fits the entire model(every layer added)
 
vectorOut forward (vectorIn input) const
 calculates the output for given input
 
template<ErrorFunction err>
float_t error (vectorIn input, vectorOut expected) const
 calculates an error of a model
 
template<ErrorFunction err>
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
 
const LayergetLayer (std::size_t index) const
 gets the layer at a given index
 
const Layeroperator[] (std::size_t index) const
 
std::string str () const
 Returns a string representation of the model.
 
void exportToONNX (std::filesystem::path path, std::string name="MLask Model") const
 exports the model to a file in ONNX format
 

Detailed Description

Class representing a neural network model.

Constructor & Destructor Documentation

◆ Model()

mlask::Model::Model ( std::size_t  in,
std::size_t  out,
std::size_t  size = 0,
std::size_t  epochs = 0,
bool  log = false 
)

Constructs a model.

Parameters
innumber of input neurons
outnumber of output neurons
sizenumber of layers in the model (optional for optimization)
epochsnumber of epochs to train the model (optional for logging)
logwhether to log the training process (optional for logging)

Member Function Documentation

◆ addFullyConnectedLayer()

template<std::size_t in, std::size_t out>
void mlask::Model::addFullyConnectedLayer ( )

creates and adds fullyConnectedLayer with given in and out neurons

Template Parameters
innumber of input neurons
outnumber of output neurons
Exceptions
ArchitectureErrorif the input size of the layer does not match the output size of the previous layer

◆ addLambdaActivationFunction()

void mlask::Model::addLambdaActivationFunction ( actfunc  func,
actfunc  derv 
)

creates and adds ActivationFunction layer with give function form and derived form

Parameters
funcThe activation function
dervThe derivative of the activation function

◆ addLayer() [1/2]

template<TLayer layer, typename... Args>
void mlask::Model::addLayer ( Args &&...  args)

Adds a layer to the model(Layer is an abstract class, see Layer.hpp for more details)

Template Parameters
layerThe layer to add, a class needs to derive from layer abstract class, see layer.hpp and TLayer concept

This one doesn't check if in and out are valid

◆ addLayer() [2/2]

void mlask::Model::addLayer ( std::unique_ptr< Layer layer)

Adds a layer to the model(Layer is an abstract class, see Layer.hpp for more details)

Parameters
layerThe layer to add
Exceptions
ArchitectureErrorif the input size of the layer does not match the output size of the previous layer

◆ backprop()

template<ErrorFunction err>
void mlask::Model::backprop ( vectorIn  input,
vectorOut  expected 
)

performs backpropagation algorithm

Parameters
inputthe input vector for the model
expectedthe expected output vector for the given input
Template Parameters
errErrorFunction used to calculate error, notice that this should be derived version

◆ error()

template<ErrorFunction err>
float_t mlask::Model::error ( vectorIn  input,
vectorOut  expected 
) const

calculates an error of a model

Parameters
inputThe input vector for the model
expectedThe expected vector of values
Template Parameters
errthe error function (see ErrorFunction.hpp or include/error_functions)
Returns
the error based on an error function

◆ exportToONNX()

void mlask::Model::exportToONNX ( std::filesystem::path  path,
std::string  name = "MLask Model" 
) const

exports the model to a file in ONNX format

Parameters
pathThe path to the file where the model will be saved
nameThe name of the model to be used in the ONNX file (optional, default is "MLask Model")
Exceptions
throwsExportError if the export failed

◆ fit()

void mlask::Model::fit ( float_t  learning_rate)

Fits the entire model(every layer added)

Parameters
learning_rateThe learning rate for the model

◆ forward()

vectorOut mlask::Model::forward ( vectorIn  input) const

calculates the output for given input

Parameters
inputThe input vector for the model
Returns
The output vector for the given input

◆ getLayer()

const Layer * mlask::Model::getLayer ( std::size_t  index) const
inline

gets the layer at a given index

Parameters
indexThe index of the layer to get
Returns
A pointer to the layer at the given index

◆ operator[]()

const Layer * mlask::Model::operator[] ( std::size_t  index) const
inline

◆ str()

std::string mlask::Model::str ( ) const

Returns a string representation of the model.

◆ whole_error()

template<ErrorFunction err>
float_t mlask::Model::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

Parameters
inputThe input matrix for the model
expectedThe expected matrix of values
Template Parameters
errthe error function (see ErrorFunction.hpp or include/error_functions)
Returns
the error based on an error function

The documentation for this class was generated from the following files: