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:
@@ -35,8 +35,10 @@
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <tensor/vector.hpp>
|
||||
#include <neural_network/layer/layer_dense.hpp>
|
||||
#include <config/omp.hpp>
|
||||
#include <math/matmul.hpp>
|
||||
#include <math/add.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// DEFINE DESCRIPTION
|
||||
@@ -49,97 +51,37 @@
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
static const panic::uint_t vector_omp_min_size = 10000;
|
||||
static const panic::types::uint_t layer_dense_omp_min_size = 10000;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::vector::vector
|
||||
// Constructor Name : panic::neural_network::layer_dense
|
||||
//
|
||||
// Description:
|
||||
// Creates an empty vector.
|
||||
// Creates an empty layer.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
vector<T>::vector() {
|
||||
length = 0;
|
||||
data = 0;
|
||||
layer_dense::layer_dense() {
|
||||
weights.resize(0,0);
|
||||
biases.resize(0);
|
||||
outputs.resize(0,0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::vector::vector
|
||||
// Constructor Name : panic::neural_network::layer_dense
|
||||
//
|
||||
// Description:
|
||||
// Creates a vector with size allocation and initializes all values to zero.
|
||||
// Creates an empty layer.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
vector<T>::vector(panic::uint_t size){
|
||||
length = size;
|
||||
|
||||
if (length == 0){
|
||||
data = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < size; ++i){
|
||||
data[i] = static_cast<T>(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::vector::vector
|
||||
//
|
||||
// Description:
|
||||
// Creates a vector with size allocation and initializes all values.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
vector<T>::vector(panic::uint_t size, T value){
|
||||
length = size;
|
||||
|
||||
if (size == 0){
|
||||
data = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data = new T[size];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < size; ++i){
|
||||
data[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::vector::vector
|
||||
//
|
||||
// Description:
|
||||
// Copy-contructor, makes a deep copy of another vector like this:
|
||||
// vector a(3);
|
||||
// vector b = a;
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
vector<T>::vector(const vector& other){
|
||||
length = other.length;
|
||||
|
||||
if (length == 0){
|
||||
data = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = other.data[i];
|
||||
}
|
||||
layer_dense::layer_dense(panic::types::uint_t input_size, panic::types::uint_t neurons) {
|
||||
weights.resize(input_size, neurons);
|
||||
biases.resize(neurons);
|
||||
outputs.resize(0,0);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
@@ -148,182 +90,56 @@ vector<T>::vector(const vector& other){
|
||||
// Description:
|
||||
// Deletes the data and releases the memory.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
vector<T>::~vector(){
|
||||
delete[] data;
|
||||
|
||||
data = 0;
|
||||
length = 0;
|
||||
}
|
||||
//template <typename T>
|
||||
//vector<T>::~vector(){
|
||||
// delete[] data;
|
||||
//
|
||||
// data = 0;
|
||||
// length = 0;
|
||||
//}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::operator=
|
||||
// Function Name : panic::neural_network::layer_dense.forward
|
||||
//
|
||||
// Description:
|
||||
// Copy-assignment. Copies from another vector like this:
|
||||
// vector a(5);
|
||||
// vector b(3);
|
||||
// b = a;
|
||||
// Calculated the forward pass:
|
||||
// outputs = inputs * weights + biases
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
vector<T>& vector<T>::operator=(const vector& other){
|
||||
if (this == &other){
|
||||
return *this;
|
||||
bool layer_dense::forward(const panic::tensor::real_matrix& inputs){
|
||||
|
||||
if (inputs.cols() != weights.rows()){
|
||||
return false;
|
||||
}
|
||||
|
||||
T* new_data = 0;
|
||||
|
||||
if (other.length > 0){
|
||||
new_data = new T[other.length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < other.length; ++i){
|
||||
new_data[i] = other.data[i];
|
||||
}
|
||||
if (!outputs.resize(inputs.rows(), weights.cols())){
|
||||
return false;
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
data = new_data;
|
||||
length = other.length;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::resize
|
||||
//
|
||||
// Description:
|
||||
// Returns the length/size of the vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::uint_t vector<T>::size() const{
|
||||
return length;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::resize
|
||||
//
|
||||
// Description:
|
||||
// Resizes the vector to new length and keeps old values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool vector<T>::resize(panic::uint_t new_size){
|
||||
if (new_size == length){
|
||||
return true;
|
||||
if (!panic::math::matmul(inputs, weights, outputs)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (new_size == 0){
|
||||
data = 0;
|
||||
length = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
T* new_data = new T[new_size];
|
||||
panic::uint_t copy_size = length;
|
||||
|
||||
if (new_size < length){
|
||||
copy_size = new_size;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(copy_size > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < copy_size; ++i){
|
||||
new_data[i] = data[i];
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(copy_size - new_size > vector_omp_min_size)
|
||||
for (panic::uint_t i = copy_size; i < new_size; ++i){
|
||||
new_data[i] = static_cast<T>(0);
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
data = new_data;
|
||||
length = new_size;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::fill
|
||||
//
|
||||
// Description:
|
||||
// Fills te vector with a value
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool vector<T>::fill(T value){
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = value;
|
||||
if (!panic::math::add_rowwise(outputs, biases, outputs)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::operator[]
|
||||
// Function Name : panic::neural_network::layer_dense.backward
|
||||
//
|
||||
// Description:
|
||||
// Lets you read and write v[index]
|
||||
// Calculated the backward pass:
|
||||
// ??
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T& vector<T>::operator[](panic::uint_t index){
|
||||
return data[index];
|
||||
bool layer_dense::backward(const panic::tensor::real_matrix& dinputs){
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::operator[]
|
||||
//
|
||||
// Description:
|
||||
// Lets you read v[index] from a const vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
const T& vector<T>::operator[](panic::uint_t index) const{
|
||||
return data[index];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::at
|
||||
//
|
||||
// Description:
|
||||
// Lets you read and write v.at(index) with index bounse
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T& vector<T>::at(panic::uint_t index){
|
||||
|
||||
if (index >= length){
|
||||
return data[length-1];
|
||||
}
|
||||
|
||||
return data[index];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::vector::at
|
||||
//
|
||||
// Description:
|
||||
// Lets you read v[index] from a const vector with index bounse
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
const T& vector<T>::at(panic::uint_t index) const{
|
||||
if (index >= length){
|
||||
return data[length-1];
|
||||
}
|
||||
return data[index];
|
||||
}
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
template struct vector<panic::real_t>;
|
||||
template struct vector<panic::int_t>;
|
||||
template struct vector<panic::uint_t>;
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
} // namespace panic
|
||||
|
||||
Reference in New Issue
Block a user