dimanche 30 août 2015

What is The Most Effective Data Structure To Use in My Neural Network program? Does my program require dynamic allocation

I have a background in Java and am trying to learn C++. I am currently trying to write a neural network program, but I am struggling with some fundamental concepts with regards to memory allocation. My question is primarily concerned with the Network class.

In the Network constructor I need to initialize an array of pointers to arrays of Neuron objects and pass it to the Network class variable layers. I do not believe I need to use any dynamic memory allocation (i.e. vectors) because I know the size of the arrays at compile time (obviously correct me if I'm wrong here). Should I be declaring the arrays in the constructor and then extending their scope with unique_ptr? or is there a way to initialize the arrays as a class variable and somehow define their sizes in the constructor.

Any suggestions on other parts of my code is also welcome. I am trying to learn as much as I can here.

Also where can I find resources on this stuff? All of the c++ resources I have found cover only the basics.

Network.h

class Network {
public:
    size_t numLayers;
    size_t* layerSizes;
    //array of ptrs to array of Neurons
    /*here I essentially need a two dimensional array of neurons
    where each column is a layer (array) of neurons in the 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 is an array of values indicating the size of each layer
i.e for an xor nnet structure would equal {2,2,1} for 2 input nodes
2 hiden nodes and 1 output node */

//total number of layers
numLayers = size;   
//number of nodes in each respective layer                  
layerSizes = structure;

for (size_t l = 1; l < size; l++) {
    size_t arraySize = structure[l];
    Neuron temp[arraySize]; //initialize array with default Neurons
    for (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 to array of connecting neurons
    Neuron** synapse;
    //equal sized array of corresponding weights    
    double* weights;    
    //length of the synapse and 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 the weights*values of each connecting node
    for(size_t i = 0; i < size; i++){
        inputSignal += *(weights+i) * (**(synapse+i)).value;
    }
    inputSignal += (-1 * threshold);
    value = 1.0/(1.0 + exp(-inputSignal));
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire