I've created the optimizer class
This commit is contained in:
2026-08-04 18:55:30 +02:00
parent 11da534fd6
commit b52c128496
15 changed files with 1207 additions and 48 deletions
+131 -2
View File
@@ -40,8 +40,14 @@
#include <tensor/matrix.hpp> // panic::tensor::real_matrix
#include <neural_network/layer/layer.hpp> // Base layer struct
#include <neural_network/layer/trainable_layer.hpp>
#include <neural_network/layer/layer_dense.hpp> // fully connected dense layer
#include <neural_network/loss/loss.hpp>
#include <neural_network/optimizers/optimizer.hpp>
#include <neural_network/optimizers/optimizer_sgd.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// TYPE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
@@ -77,6 +83,22 @@ struct model{
*/
layer** layers;
trainable_layer** trainable_layers;
panic::types::uint_t trainable_layer_count;
optimizer* optimizer_function;
/**
* @brief a pointer the loss function
*
*
* @note The model owns these layers and deletes them in clear().
*
*/
loss* loss_function;
// Number of layers currently stored in the model.
/**
* @brief Stores the number of layers
@@ -86,6 +108,8 @@ struct model{
// model output (may be deleted and also used for debug)
panic::tensor::real_matrix outputs;
// model dinputs (may be deleted and also used for debug)
panic::tensor::real_matrix dinputs;
/**
* @brief Empthy constructor
@@ -116,7 +140,21 @@ struct model{
* @note it adds an already-inplemented layer to the model.
*
*/
bool add(layer* new_layer);
bool add_layer(layer* new_layer);
/**
* @brief Helper function for adding layers
*
* Computes:
* @code
* layer_dense* new_layer = new layer_dense(3, 4);
* add(new_layer)
* @endcode
*
* @note it adds an already-inplemented layer to the model.
*
*/
bool add_trainable_layer(trainable_layer* new_layer);
/**
* @brief Adds a dense layer to the model.
@@ -187,8 +225,99 @@ struct model{
*/
bool forward(const panic::tensor::real_matrix& inputs);
// Delete all layers and reset the model.
/**
* @brief Loops over all layers backward function
*
* Computes:
* @code
* model.bacward(dvalues_data_matrix)
* @endcode
*
* @param dvalues diput data.
*
* @return true looped over every layer.
*
*
*/
bool backward(const panic::tensor::real_matrix& dvalues);
/**
* @brief Add loss for categorical crossentropy.
*
* Computes:
* @code
* model.add_loss_categorical_crossentropy();
* @endcode
*
*
* @return true if loss is added
*
* @note This function is convenient, but it allocates a new layer.
*/
bool add_loss_categorical_crossentropy();
/**
* @brief Adds activation softmax AND loss for categorical crossentropy.
*
* Computes:
* @code
* model.activation_softmax_loss_categorical_crossentropy();
* @endcode
*
*
* @return true if activation and loss is added
*
* @note This function is convenient, but it allocates a new layer.
*/
bool activation_softmax_loss_categorical_crossentropy();
/**
* @brief Adds optimizer_sgd to the model
*
* Computes:
* @code
* model.optimizer_sgd(1e-4)
* @endcode
*
* @param learning_rate Learning rate for update_param (default 1e-3).
*
* @return true If looped and optimized every trainable layer.
*
*
*/
bool add_optimizer_sgd(const panic::types::real_t learning_rate = static_cast<panic::types::real_t>(1e-3));
/**
* @brief Trains the model with input data
*
* Computes:
* @code
* model.backward(input_data_matrix)
* @endcode
*
* @param X_train Input X data for training.
* @param epochs Number of training iterations.
* @param print_every Prints every n iteration.
*
*
* @return true if training is done correctly.
*
*
*/
bool train(const panic::tensor::real_matrix& X_train,
const panic::tensor::uint_vector& y_train,
const panic::types::uint_t epochs,
const panic::types::uint_t print_every);
/**
* @brief Clears and deletes all layers and resets the model
*