Save
I've created the optimizer class
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: optimizer.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 23-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the base optimizer struct used in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
|
||||
#include <neural_network/layer/trainable_layer.hpp>
|
||||
|
||||
|
||||
namespace panic{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Base optimizer for the rest of the neural network library to use
|
||||
*
|
||||
* This base optimizer should be used in neural networks
|
||||
* This is done so it's easy to optimize the trainable layers in the model to loop over.
|
||||
* The virtual means it should use derived object's version when called with a pointer.
|
||||
* The =0 means the derivative object NEEDS to have these functions to work.
|
||||
*
|
||||
* The struct is used for PANIC neural_network library.
|
||||
*/
|
||||
struct optimizer{
|
||||
|
||||
/**
|
||||
* @brief Default de-constructor
|
||||
*
|
||||
*/
|
||||
virtual ~optimizer() = default;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Virtual forward function for derivative layers
|
||||
*
|
||||
* @param inputs Data matrix input for forward function.
|
||||
*
|
||||
* @Note It's equal to 0 because it make the derivative
|
||||
* object NEEDS to have these function to work.
|
||||
*/
|
||||
virtual bool update_params(trainable_layer& layer) = 0;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: optimizer_sgd.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 04-08-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the optimizer_sgd struct used in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <config/types.hpp>
|
||||
#include <neural_network/optimizers/optimizer.hpp>
|
||||
|
||||
|
||||
namespace panic{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief optimizer_sgd for the rest of the neural network library to use
|
||||
*
|
||||
*/
|
||||
struct optimizer_sgd: optimizer{
|
||||
|
||||
panic::types::real_t learning_rate;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructor
|
||||
*
|
||||
* @param learning_rate The learning rate for the optimization.
|
||||
*
|
||||
*/
|
||||
optimizer_sgd(const panic::types::real_t learning_rate = static_cast<panic::types::real_t>(1e-3));
|
||||
|
||||
|
||||
/**
|
||||
* @brief Default de-constructor
|
||||
*
|
||||
*/
|
||||
~optimizer_sgd() = default;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Updates weights and biases in trainable layers
|
||||
*
|
||||
* @param layer Trianable layer to update.
|
||||
*
|
||||
*/
|
||||
bool update_params(trainable_layer& layer) override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user