Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Backpropagation Artificial Neural Network in C++

, 20 May 2008
This article demonstrates a backpropagation artificial neural network console application with validation and test sets for performance estimation using uneven distribution metrics.
ann_demo.zip
bin
ann1Dn.exe
dat
red.dat
red.hea
iris.nn
setosa_versi.dat
virgi.dat
void
ann_src.zip
src
Lib
LibNN

#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)

Share

About the Author

Chesnokov Yuriy
Engineer
Russian Federation Russian Federation
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 May 2008
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid