Backup Save

This commit is contained in:
2026-08-06 10:22:52 +02:00
parent 642bba1198
commit 7f92e6884d
6 changed files with 81 additions and 13 deletions
+17 -1
View File
@@ -323,7 +323,7 @@ struct model{
* *
* *
*/ */
bool add_optimizer_sgd(const panic::types::real_t learning_rate = static_cast<panic::types::real_t>(1e-3)); bool add_optimizer_sgd(const panic::types::real_t learning_rate = static_cast<panic::types::real_t>(1));
@@ -337,6 +337,22 @@ struct model{
*/ */
bool finalize(); bool finalize();
/**
* @brief Optimizes the model with parameters on trainable layers
*
* Computes:
* @code
* model.optimize()
* @endcode
*
* @return true if optimization is done correctly.
*
*/
bool optimize();
/** /**
* @brief Trains the model with input data * @brief Trains the model with input data
* *
@@ -24,7 +24,7 @@
* Module Name: neural_network * Module Name: neural_network
* File Name: optimizer.hpp * File Name: optimizer.hpp
* Revision: 0.1.0 * Revision: 0.1.0
* Date: 23-06-2026 * Date: 06-08-2026
* Author: Michelle Bausager * Author: Michelle Bausager
* *
* Description: * Description:
@@ -52,7 +52,6 @@ struct optimizer_sgd: optimizer{
panic::types::real_t learning_rate; panic::types::real_t learning_rate;
/** /**
* @brief Constructor * @brief Constructor
* *
+10 -6
View File
@@ -705,7 +705,7 @@ int main(void) {
panic::tensor::real_matrix X; panic::tensor::real_matrix X;
panic::tensor::uint_vector y; panic::tensor::uint_vector y;
panic::types::uint_t samples = 10; panic::types::uint_t samples = 100;
panic::types::uint_t classes = 3; panic::types::uint_t classes = 3;
@@ -716,7 +716,7 @@ int main(void) {
panic::neural_network::model mymodel; panic::neural_network::model mymodel;
// Create Dense layer with 2 input features and 3 output values // Create Dense layer with 2 input features and 3 output values
if (!mymodel.add_layer_dense(2,3)){ if (!mymodel.add_layer_dense(2,64)){
return false; return false;
} }
@@ -726,7 +726,7 @@ int main(void) {
} }
// Create a second dense layer with 3 inputs and 3 outputs // Create a second dense layer with 3 inputs and 3 outputs
if (!mymodel.add_layer_dense(3, 3)){ if (!mymodel.add_layer_dense(64, 3)){
return false; return false;
} }
@@ -741,14 +741,18 @@ int main(void) {
return false; return false;
} }
if (! mymodel.add_optimizer_sgd()){
return false;
}
if (!mymodel.finalize()){ if (!mymodel.finalize()){
return false; return false;
} }
//mymodel.add_optimizer_sgd();
panic::types::uint_t epochs = 10;
panic::types::uint_t print_every = 1; panic::types::uint_t epochs = 10000;
panic::types::uint_t print_every = 100;
if (!mymodel.train(X, y, epochs, print_every)){ if (!mymodel.train(X, y, epochs, print_every)){
std::cout << "Training failed" << std::endl; std::cout << "Training failed" << std::endl;
+35 -1
View File
@@ -606,6 +606,38 @@ bool model::finalize(){
} }
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::model::optimize
//
// Description:
// Optimizes the model with parameters on trainable layers
//--------------------------------------------------------------------------------------------------------------------------
bool model::optimize(){
if (optimizer_function == 0){
return false;
}
for(panic::types::uint_t i = 0; i < trainable_layer_count; ++i){
// Validate the stored layer pointers before using them.
if (trainable_layers[i] == 0){
return false;
}
if (! optimizer_function->update_params(*trainable_layers[i]) ){
return false;
}
}
return true;
}
//-------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::model::train // Function Name : panic::neural_network::model::train
@@ -668,7 +700,9 @@ bool model::train(const panic::tensor::real_matrix& X_train,
} }
//optimize(); if (! optimize()){
return false;
}
} }
@@ -50,7 +50,7 @@
* Small vectors and matrices are kept serial because the overhead of starting * Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself. * worker threads can be larger than the work itself.
*/ */
static const panic::types::uint_t optimizer_sgd_omp_min_size = 500; static const panic::types::uint_t optimizer_sgd_omp_min_size = 250;
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION // INPLEMENTATION
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
@@ -77,21 +77,36 @@ optimizer_sgd::optimizer_sgd(const panic::types::real_t learning_rate) {
//-------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------
bool optimizer_sgd::update_params(trainable_layer& layer) { bool optimizer_sgd::update_params(trainable_layer& layer) {
panic::tensor::real_matrix weight_updates;
panic::tensor::real_vector bias_updates;
if (!panic::math::mul(layer.weights, -learning_rate, weight_updates)){ // Gradients must match their corresponding parameters.
if (layer.weights.rows() != layer.dweights.rows() || layer.weights.cols() != layer.dweights.cols() || layer.biases.size() != layer.dbiases.size()){
return false; return false;
} }
if (layer.weights.rows() == static_cast<panic::types::uint_t>(0) || layer.weights.cols() == static_cast<panic::types::uint_t>(0) || layer.biases.size() == static_cast<panic::types::uint_t>(0) ){
return false;
}
panic::tensor::real_matrix weight_updates;
panic::tensor::real_vector bias_updates;
// weight_updates = -learning_rate * dweights
if (!panic::math::mul(layer.dweights, -learning_rate, weight_updates)){
return false;
}
// weights += weight_updates
if (!panic::math::add(layer.weights, weight_updates, layer.weights)){ if (!panic::math::add(layer.weights, weight_updates, layer.weights)){
return false; return false;
} }
if (!panic::math::mul(layer.biases, -learning_rate, bias_updates)){ // bias_updates = -learning_rate * dbiases
if (!panic::math::mul(layer.dbiases, -learning_rate, bias_updates)){
return false; return false;
} }
// biases += bias_updates
if (!panic::math::add(layer.biases, bias_updates, layer.biases)){ if (!panic::math::add(layer.biases, bias_updates, layer.biases)){
return false; return false;
} }