c++ - What is the most effective data structure to use in my Neural Network program? Does my program require dynamic allocation -
i have background in java , trying learn c++. trying write neural network program, struggling fundamental concepts regards memory allocation. question concerned network class.
in network constructor need initialize array of pointers arrays of neuron objects , pass network class variable layers. not believe need use dynamic memory allocation (i.e. vectors) because know size of arrays @ compile time (obviously correct me if i'm wrong here). should declaring arrays in constructor , extending scope unique_ptr? or there way initialize arrays class variable , somehow define sizes in constructor.
any suggestions on other parts of code welcome. trying learn as can here.
also can find resources on stuff? of c++ resources have found cover basics.
network.h
class network { public: size_t numlayers; size_t* layersizes; //array of ptrs array of neurons /*here need 2 dimensional array of neurons each column layer (array) of neurons in network*/ vector<unique_ptr<neuron[]>> layers; network (void){}; //input nodes network (size_t[], size_t); };
network.cpp
network::network (size_t structure[], size_t size) { /*structure array of values indicating size of each layer i.e xor nnet structure equal {2,2,1} 2 input nodes 2 hiden nodes , 1 output node */ //total number of layers numlayers = size; //number of nodes in each respective layer layersizes = structure; (size_t l = 1; l < size; l++) { size_t arraysize = structure[l]; neuron temp[arraysize]; //initialize array default neurons (size_t n = 0; n < arraysize; n++) { temp[n] = neuron(/*array of neurons in previous layer, array of random weights, size, threshold*/); } }
}
neuron.h
class neuron { public: //ptr array of connecting neurons neuron** synapse; //equal sized array of corresponding weights double* weights; //length of synapse , weight arrays; size_t size; double threshold, value; neuron (void); //input nodes void initialize (neuron*[], double[], size_t, double); int propagate(void); };
neuron.cpp
//default constructor neuron::neuron (void) {} void neuron::initialize (neuron* connects[], double initial_weights[], size_t arraysize, double thresh) { synapse = connects; weights = initial_weights; size = arraysize; threshold = thresh; } int neuron::propagate (void) { double inputsignal = 0.0; //sum weights*values of each connecting node for(size_t = 0; < size; i++){ inputsignal += *(weights+i) * (**(synapse+i)).value; } inputsignal += (-1 * threshold); value = 1.0/(1.0 + exp(-inputsignal)); return 0; }
vectors can dynamically sized, doesn't mean using vectors commits dynamic memory allocation. rule, stl vector superior alternative raw c-style array, , should preferred in absence of reasons otherwise. can take advantage of many ways in vectors can initialized.
in particular, if introduce explicit class model layer, can have code this, initialization of neurons in network taken care of automatically. (this illustrative only).
#include <string> #include <vector> #include <cmath> class neuron { public: neuron(); void propagate( std::vector<neuron> const& layer, std::vector<double> const& weights, double thresh ); private: double value_; }; void neuron::propagate( std::vector<neuron> const& layer, std::vector<double> const& weights, double thresh ) { ( size_t index{0}; index < layer.size(); ++index ) { thresh -= layer[index].value_ * weights[index]; } value_ = 1.0 / ( 1.0 + std::exp( thresh ) ); } class layer { public: layer(size_t size); void propagate( layer const& previous, double thresh ); private: std::vector<neuron> neurons_; std::vector<double> weights_; }; void layer::propagate( layer const& previous, double thresh ) { ( auto& neuron : neurons_ ) { neuron.propagate( previous.neurons_, previous.weights_, thresh ); } } class network { public: network(size_t structure[], size_t size); private: std::vector<layer> layers_; }; neuron::neuron() : value_(0) {} layer::layer(size_t size) : neurons_(size), weights_(size, (1.0 / size)) {} network::network(size_t structure[], size_t size) : layers_(structure, structure + size) {} int main(void) { size_t sizes[] = { 2, 2, 1 }; network _network(sizes, 3); return 0; }
here, 3 different ways initialize vectors have been used. can read them here
note network "matrix" consists of neurons, not of pointers neurons. don't need level of indirection.
Comments
Post a Comment