Dense Layer, Add

added the dense layer with a forward functions. I also made the add functions for most cases.
This commit is contained in:
2026-06-25 19:43:07 +02:00
parent 9b20278395
commit 189d605bfa
27 changed files with 1657 additions and 902 deletions
+16 -75
View File
@@ -22,13 +22,13 @@
*
* Project Name: PANIC
* Module Name: neural_network
* File Name: layer_dense.hpp
* File Name: layer.hpp
* Revision: 0.1.0
* Date: 23-06-2026
* Author: Michelle Bausager
*
* Description:
* Defines the dense layers used in neural network
* Defines the base layers struct used in pther layers in neural network
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
@@ -36,6 +36,7 @@
// 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)
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
@@ -46,94 +47,34 @@
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// Type Name : panic::neural_network::layer_dense
// Type Name : panic::neural_network::layer
//
// Description:
// A dense layer to use in neural networks
// A base layer to use in neural networks
//
// Member Variables:
// length panic::uint_t
// Number of elements in the vector.
//
// data panic::real_t*
// Pointer to the allocated vector data.
// None.
//
// Notes:
// This vector uses dynamic allocation with new[] and delete[].
// Copying performs a deep copy.
// 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 derviced object NEEDS to have these functions to work.
//
//---------------------------------------------------------------------------------------------------------------------------
namespace panic{
namespace tensor{
namespace neural_network{
template <typename T>
struct vector{
panic::uint_t length;
T* data;
struct layer{
// empty contructor
vector();
virtual ~layer() = default;
// contructor with size allocation
vector(panic::uint_t size);
// contructor with size allocation and sets it all to a value
vector(panic::uint_t size, T value);
// copy-contructor
// vector a(3);
// vector b = a;
vector(const vector& other);
// de-contructor, releases memory
~vector();
// copy-assignment
//vector a(5);
//vector b(3);
//b = a;
vector& operator=(const vector& other);
// function returns size.
// const tells compiler that the fuction don't edit the objert.
panic::uint_t size() const;
// function to resize data vector (I need a new that don't save the vlaues)
bool resize(panic::uint_t new_size);
// fill data with value
bool fill(T value);
// lets you read and write v[index]
T& operator[](panic::uint_t index);
// lets you read v[index] from a const vector
const T& operator[](panic::uint_t index) const;
// lets you read and write v.at(index)
T& at(panic::uint_t index);
// lets you read v.at(index) from a const vector
const T& at(panic::uint_t index) const;
virtual bool forward(const panic::tensor::real_matrix& inputs) = 0;
virtual bool backward(const panic::tensor::real_matrix& dinputs) = 0;
};
//---------------------------------------------------------------------------------------------------------------------------
// TYPE ALIASES
//---------------------------------------------------------------------------------------------------------------------------
typedef vector<panic::real_t> real_vector;
typedef vector<panic::int_t> int_vector;
typedef vector<panic::uint_t> uint_vector;
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
extern template struct vector<panic::real_t>;
extern template struct vector<panic::int_t>;
extern template struct vector<panic::uint_t>;
} // namespace tensor
} // namespace panic