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:
@@ -32,10 +32,14 @@
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#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
|
||||
@@ -45,96 +49,54 @@
|
||||
// TYPE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// Type Name : panic::neural_network::layer_dense
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::layer_dense
|
||||
//
|
||||
// Description:
|
||||
// A dense layer to use in neural networks
|
||||
// Dense/fully-connected neural network layer.
|
||||
//
|
||||
// Member Variables:
|
||||
// length panic::uint_t
|
||||
// Number of elements in the vector.
|
||||
// Inputs:
|
||||
// Samples x input size const panic::tensor::real_matrix&
|
||||
//
|
||||
// data panic::real_t*
|
||||
// Pointer to the allocated vector data.
|
||||
// Weight shape:
|
||||
// input_size x neuron_count
|
||||
//
|
||||
// Bias shape:
|
||||
// 1 x neuron_count
|
||||
//
|
||||
// Output shape:
|
||||
// samples x neuron_count panic::tensor::real_matrix
|
||||
//
|
||||
// Notes:
|
||||
// This vector uses dynamic allocation with new[] and delete[].
|
||||
// Copying performs a deep copy.
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// forward(input) calculates:
|
||||
//
|
||||
// outputs = inputs * weights + biases
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
namespace neural_network{
|
||||
|
||||
template <typename T>
|
||||
struct vector{
|
||||
panic::uint_t length;
|
||||
T* data;
|
||||
struct layer_dense : public layer{
|
||||
|
||||
// empty contructor
|
||||
vector();
|
||||
panic::tensor::real_matrix weights;
|
||||
panic::tensor::real_vector biases;
|
||||
panic::tensor::real_matrix outputs;
|
||||
|
||||
// contructor with size allocation
|
||||
vector(panic::uint_t size);
|
||||
// Empthy contructor
|
||||
layer_dense();
|
||||
|
||||
// contructor with size allocation and sets it all to a value
|
||||
vector(panic::uint_t size, T value);
|
||||
// Contructor with layer size
|
||||
layer_dense(panic::types::uint_t input_size, panic::types::uint_t neurons);
|
||||
|
||||
// copy-contructor
|
||||
// vector a(3);
|
||||
// vector b = a;
|
||||
vector(const vector& other);
|
||||
// Decontructor (All internal variables has decontructors, so we can just use default)
|
||||
~layer_dense() = default;
|
||||
|
||||
// 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;
|
||||
// Forward function for forward pass
|
||||
bool forward(const panic::tensor::real_matrix& inputs);
|
||||
|
||||
// Backward pass
|
||||
bool backward(const panic::tensor::real_matrix& dinpus);
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user