Click here to Skip to main content
12,300,379 members (53,523 online)
Click here to Skip to main content

Stats

125.6K views
7.7K downloads
101 bookmarked
Posted

Backpropagation Artificial Neural Network in C++

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

#ifndef ANNetwork_h
#define ANNetwork_h



class ANeuron;

////////////////////////////////////ANN layer///////////////////////////////////////////////////
class ANNLayer
{        
        friend class ANNetwork;
public:
        ANNLayer(int neurons_number);    
        ~ANNLayer();
        
        inline int get_neurons_number() const;
        inline ANeuron *get_neuron(int i) const;

private:
        wchar_t layer_name[_MAX_PATH];          //layer name
        int m_neurons_number;                   //number of neurons in layer
        vector<ANeuron *> neurons;              //array of Neuron classes
        
};

inline int ANNLayer::get_neurons_number() const
{
        return m_neurons_number;
}

inline ANeuron *ANNLayer::get_neuron(int i) const
{
        if (i > get_neurons_number() - 1 || i < 0)
                return 0;
        return neurons[i];
}
////////////////////////////////////////////////////////////////////////////////////////////////





///////////////////////////////////ANN network//////////////////////////////////////////////////
class ANNetwork
{
public:
        ANNetwork(int layers_number, int *neurons_per_layer);                   //number of layers, and neurons per layer
        ANNetwork(const wchar_t *fname);                                        //custom config from file
        ~ANNetwork();

        //init links for network [default SIGMOID activation function]
        void init_links(const float *avec = 0, const float *mvec = 0, int ifunc = 0, int hfunc = 1);               
        void randomize_weights(unsigned int rseed = 0);
                        
        bool train(const float *ivec, float *ovec, 
                            const float *dsrdvec, float error = 0.05);          //train network with input vector and desired output vector
        void classify(const float *ivec, float *ovec);                          //run network with in vector [ivec],  return in [ovec] network out        
        bool save(const wchar_t *fname) const;                          

        inline int status() const;        
        inline int get_layers_number() const;         
        inline ANNLayer *get_layer(int i) const;        


private:        
        int m_status;                   //status -1 err, 0-OK, 1-random weights
        int m_layers_number;            //number of layers in network
        vector<ANNLayer *> layers;      //array of layers

        float m_nrule;                  //learning rule  0.2
        float m_alpha;                  //momentum  0.7
        
        void backprop_run(const float *dsrdvec);
        void network_output(float *ovec) const;                               //get network out
};

inline int ANNetwork::status() const
{
        return m_status;
}

inline int ANNetwork::get_layers_number() const
{
        return m_layers_number;
} 

inline ANNLayer *ANNetwork::get_layer(int i) const
{
        if (i > get_layers_number() - 1 || i < 0)
                return 0;
        return layers[i];
}
////////////////////////////////////////////////////////////////////////////////////////////////


#endif ANNetwork_h

/*
     ANNetwork

     1 2 3 4           layers,  ANNLayer
    ------------
    |N|N|N|N| | ....   neurons N,  ANeuron
    ------------
    |N|N|N| | | ....
    ------------
    |N| | | | |
    ...
    ...
    ...

                    */







/*
  file format
  3
  40 10 1
  
  0
  1                activation function 0-linear, 1-sigmoid
  
  [input norm]

  weights





 */

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
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 20 May 2008
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid