117 lines
3.5 KiB
C++
117 lines
3.5 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: layer.hpp
|
|
* Revision: 0.1.0
|
|
* Date: 23-06-2026
|
|
* Author: Michelle Bausager
|
|
*
|
|
* Description:
|
|
* Defines the base layers 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 <tensor/matrix.hpp> // panic::tensor::real_matrix (uint_matrix, int_matrix)
|
|
|
|
|
|
namespace panic{
|
|
namespace neural_network{
|
|
|
|
|
|
|
|
/**
|
|
* @brief Base layer for the rest of the neural network library to use
|
|
*
|
|
* This base layer should be used in all layers/activations that have a forward and backward function
|
|
* 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 layer{
|
|
|
|
/**
|
|
* @brief Emphty output matrix to store layer input
|
|
*
|
|
* Output shape:
|
|
* samples x neuron_count
|
|
*/
|
|
panic::tensor::real_matrix inputs;
|
|
|
|
/**
|
|
* @brief Emphty output matrix to store layer output
|
|
*
|
|
* Output shape:
|
|
* samples x neuron_count
|
|
*/
|
|
panic::tensor::real_matrix outputs;
|
|
|
|
/**
|
|
* @brief Emphty matrix to store output for backward pass
|
|
*
|
|
*/
|
|
panic::tensor::real_matrix dinputs;
|
|
|
|
/**
|
|
* @brief Default de-constructor
|
|
*
|
|
*/
|
|
virtual ~layer() = 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 forward(const panic::tensor::real_matrix& inputs) = 0;
|
|
|
|
/**
|
|
* @brief Virtual backward function for derivative layers
|
|
*
|
|
* @param dinputs Data matrix input for backward function.
|
|
*
|
|
* @Note It's equal to 0 because it make the derivative
|
|
* object NEEDS to have these function to work.
|
|
*/
|
|
virtual bool backward(const panic::tensor::real_matrix& dinputs) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tensor
|
|
} // namespace panic
|
|
|
|
|
|
|