106 lines
3.3 KiB
C++
106 lines
3.3 KiB
C++
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
*
|
|
* 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: trainable_layer.hpp
|
|
* Revision: 0.1.0
|
|
* Date: 23-06-2026
|
|
* Author: Michelle Bausager
|
|
*
|
|
* Description:
|
|
* Defines the base trainable_layer struct used in other layers 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/layer.hpp>
|
|
#include <tensor/matrix.hpp> // panic::tensor::real_matrix (uint_matrix, int_matrix)
|
|
|
|
|
|
namespace panic{
|
|
namespace neural_network{
|
|
|
|
|
|
|
|
/**
|
|
* @brief Base trainable_layer for the rest of the neural network library to use
|
|
*
|
|
* This base trainable_layer should be used in all layers/activations that have trainable variables
|
|
* This is done so it's easy to make a list of 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 trainable_layer:layer{
|
|
|
|
panic::tensor::real_matrix weights;
|
|
panic::tensor::real_vector biases;
|
|
|
|
panic::tensor::real_matrix dweights;
|
|
panic::tensor::real_vector dbiases;
|
|
|
|
panic::types::real_t weight_regularizer_l1;
|
|
panic::types::real_t weight_regularizer_l2;
|
|
|
|
panic::types::real_t bias_regularizer_l1;
|
|
panic::types::real_t bias_regularizer_l2;
|
|
|
|
/**
|
|
* @brief Previous parameter updates used by momentum SGD.
|
|
*
|
|
* These remain empty unless an optimizer using momentum
|
|
* initializes them.
|
|
*/
|
|
panic::tensor::real_matrix weight_momentums;
|
|
panic::tensor::real_vector bias_momentums;
|
|
|
|
/**
|
|
* @brief Previous parameter updates used by AdaGrad.
|
|
*
|
|
* These remain empty unless an optimizer using cache
|
|
* initializes them.
|
|
*/
|
|
panic::tensor::real_matrix weight_cache;
|
|
panic::tensor::real_vector bias_cache;
|
|
|
|
|
|
|
|
|
|
|
|
virtual ~trainable_layer() = default;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tensor
|
|
} // namespace panic
|
|
|
|
|
|
|