added the dense layer with a forward functions. I also made the add functions for most cases.
112 lines
4.2 KiB
C++
112 lines
4.2 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_dense.hpp
|
|
* Revision: 0.1.0
|
|
* Date: 23-06-2026
|
|
* Author: Michelle Bausager
|
|
*
|
|
* Description:
|
|
* Defines the dense layers 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/layer.hpp> // for base layer struct
|
|
#include <tensor/vector.hpp>
|
|
#include <tensor/matrix.hpp>
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// DEFINE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// TYPE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::neural_network::layer_dense
|
|
//
|
|
// Description:
|
|
// Dense/fully-connected neural network layer.
|
|
//
|
|
// Inputs:
|
|
// Samples x input size const panic::tensor::real_matrix&
|
|
//
|
|
// Weight shape:
|
|
// input_size x neuron_count
|
|
//
|
|
// Bias shape:
|
|
// 1 x neuron_count
|
|
//
|
|
// Output shape:
|
|
// samples x neuron_count panic::tensor::real_matrix
|
|
//
|
|
// Notes:
|
|
// forward(input) calculates:
|
|
//
|
|
// outputs = inputs * weights + biases
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
namespace panic{
|
|
namespace neural_network{
|
|
|
|
struct layer_dense : public layer{
|
|
|
|
panic::tensor::real_matrix weights;
|
|
panic::tensor::real_vector biases;
|
|
panic::tensor::real_matrix outputs;
|
|
|
|
// Empthy contructor
|
|
layer_dense();
|
|
|
|
// Contructor with layer size
|
|
layer_dense(panic::types::uint_t input_size, panic::types::uint_t neurons);
|
|
|
|
// Decontructor (All internal variables has decontructors, so we can just use default)
|
|
~layer_dense() = default;
|
|
|
|
// Forward function for forward pass
|
|
bool forward(const panic::tensor::real_matrix& inputs);
|
|
|
|
// Backward pass
|
|
bool backward(const panic::tensor::real_matrix& dinpus);
|
|
};
|
|
|
|
} // namespace tensor
|
|
} // namespace panic
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// VARIABLE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// FUNCTION PROTOTYPE
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|