Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C

Backpropagation Artificial Neural Network in C++

Rate me:
Please Sign up or sign in to vote.
4.88/5 (29 votes)
20 May 2008GPL38 min read 207K   10.2K   104  
This article demonstrates a backpropagation artificial neural network console application with validation and test sets for performance estimation using uneven distribution metrics.

#ifndef ANNeuron_h
#define ANNeuron_h



class ANeuron;

////////////////////////////////////ANN Link///////////////////////////////////////////////////
class ANLink
{
        friend class ANeuron;
        friend class ANNetwork;
public:
        ANLink(ANeuron *pinn, ANeuron *poutn = 0, float in = 1.0f, float w = 0.0f, float add = 0.0f);
        ~ANLink();        

        inline void set_add_term(float add);
        inline void set_weight(float weight);

private:
        ANeuron *pinput_neuron;     //neuron with this->Link connected to N input
        ANeuron *poutput_neuron;    //neuron with this->Link connected to N output

        float iadd;        //add term for input layer neurons
        float dwprv;       //wight change from previous run

        float w;           //weight
        float ival;        //input value on the link, passed to *pinput_neuron
};

inline void ANLink::set_add_term(float add)
{
        iadd = add;
}

inline void ANLink::set_weight(float weight)
{
        w = weight;
}
////////////////////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////ANN neuron///////////////////////////////////////////////////
class ANeuron
{      
        friend class ANNetwork;
public:
        enum FUNCTION {LINEAR, SIGMOID};

        ANeuron();
        ~ANeuron();        

        void add_bias();
        void add_input(ANeuron *poutn = 0);  //add input link               

        void input_fire();              //push data from input layer to hidden
        void fire();                    //process my inputs and pass oval to N connected to my output

        inline void set_function(enum FUNCTION func);
        inline int get_input_links_number() const;
        inline int get_output_links_number() const;
        inline ANLink *get_input_link(int i) const;
        inline ANLink *get_output_link(int i) const;

private:
        vector<ANLink *> inputs;    //bias link and input links  Every N knows what N connected to its inputs
        vector<ANLink *> outputs;   //Every N knows what N connected to its output

        float delta;                //delta
        float oval;                 //output value

        int function;               //Neuron function: LINIAR,SIGMOID,....

};

inline void ANeuron::set_function(enum FUNCTION func)
{
        function = func;
}
inline int ANeuron::get_input_links_number() const
{
        return (int)inputs.size();
}

inline int ANeuron::get_output_links_number() const
{
        return (int)outputs.size();
}

inline ANLink *ANeuron::get_input_link(int i) const
{
        if (i > get_input_links_number() - 1 || i < 0)
                return 0;
        return inputs[i];
}

inline ANLink *ANeuron::get_output_link(int i) const
{
        if (i > get_output_links_number() - 1 || i < 0)
                return 0;
        return outputs[i];
}
////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////fire////////////////////////////////////////////////////////
inline void ANeuron::input_fire()
{
        //input layer normalization
        oval = (inputs[0]->ival + inputs[0]->iadd) * inputs[0]->w;

        //single input for input layer neuron
        switch (function) {
        default:
        case LINEAR:
                break;        

        case SIGMOID:
                oval = 1.0f / (1.0f + exp(float((-1.0f) * oval)));
                break;
        }

        //transfer my output to links connected to my output
        for (int i = 0; i < get_output_links_number(); i++)
                outputs[i]->ival = oval;
}

inline void ANeuron::fire()
{
        //oval = SUM (in[]*w[])
        oval = 0.0f;

        //compute output for Neuron
        for (int i = 0; i < get_input_links_number(); i++)
                oval += inputs[i]->ival * inputs[i]->w;

        switch (function) {
        default:
        case LINEAR:
                break;

        case SIGMOID:
                oval = 1.0f / (1.0f + exp(float((-1.0f) * oval)));
                break;
        }

        //transfer my output to links connected to my output
        for (int i = 0; i < get_output_links_number(); i++)
                outputs[i]->ival = oval;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

#endif







By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Engineer
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

Comments and Discussions