[CLPR] C + + implementations of convolutional neural networks

Source: Internet
Author: User

Article translated from: Http://www.codeproject.com/Articles/16650/Neural-Network-for-Recognition-of-Handwritten-Digi

How to implement a neural network class in C + +? There are four different classes that we need to consider:

    1. Floor-Layers
    2. Neurons in the layer-neurons
    3. Connections between neurons-connections
    4. Weighted value of the connection-weights

These four classes are embodied in the following code, and are applied centrally on the fifth class-neural network (neural networks). It is like a container for interfacing with external communication. The following code uses a large number of STL vectors.

//simplified View:some members had been omitted,//and some signatures have been altered//helpful typedef ' stypedef std::vector< nnlayer* >vectorlayers;typedef std::vector< nnweight* >vectorweights;typedef std::vector< nnneuron* >vectorneurons;typedef std::vector< nnconnection >vectorconnections;//Neural Network classclassNeuralnetwork { Public: Neuralnetwork (); Virtual~neuralnetwork (); voidCalculate (Double*Inputvector, UINT ICount,Double* Outputvector = NULL, UINT ocount =0 ); voidBackpropagate (Double*Actualoutput,Double*Desiredoutput, UINT count); Vectorlayers m_layers;};//Layer classclassnnlayer{ Public: Nnlayer (lpctstr str, Nnlayer* Pprev =NULL); Virtual~Nnlayer (); voidCalculate (); voidBackpropagate (std::vector<Double>& DERR_WRT_DXN/*inch*/, Std::vector<Double>& DERR_WRT_DXNM1/* out*/,         Doubleetalearningrate); Nnlayer*M_pprevlayer;    Vectorneurons m_neurons; Vectorweights m_weights;};//Neuron classclassnnneuron{ Public: Nnneuron (LPCTSTR str); Virtual~Nnneuron (); voidaddconnection (UINT Ineuron, uint iweight); voidAddconnection (nnconnectionConst&conn); Doubleoutput; Vectorconnections m_connections;};//Connection classclassnnconnection{ Public: Nnconnection (UINT neuron= Ulong_max, UINT weight =Ulong_max); Virtual~nnconnection ();    UINT Neuronindex; UINT Weightindex;};//Weight classclassnnweight{ Public: Nnweight (lpctstr str,Doubleval =0.0 ); Virtual~nnweight (); Doublevalue;};

The class Neuralnetwork stores a pointer array that points to each layer in the NN, the Nnlayer. There is no special function to add layers, just use std::vector::p ush_back (). The Neuralnetwork class provides two basic interfaces, one for getting output (Calculate) and one for training (backpropagete).

Each nnlayer holds a pointer to the forward layer, which can be used to get the output of the previous layer as input. It also holds a vector of pointers, each pointing to the neurons in this layer, namely Nnneuron, and of course, the weights of the connections nnweight. Similar to Neuralnetwork, the increase in neurons and weights is performed through the std::vector::p ush_back () method. The Nnlayer layer also contains the function calculate () to calculate the output of the neurons, as well as backpropagate () to train them. In fact, the Neuralnetwork class simply invokes these functions on each layer to implement the 2 identically named methods described in the previous section.

Each nnneuron holds an array of connections that allow neurons to get input. Use Nnneuron::addconnection () to add a connection, enter the label of the Neuron and the label of the weight, thereby creating a Nnconnection object and push_back it () into the array of connections that the neuron holds. Each neuron also holds its own output value (double). The Nnconnection and Nnweight classes store some information separately.

You may wonder why the weights and connections are defined separately? According to the above principle, each connection has a weight, why not directly put them in a class?

The reason: weights are often shared by the connection.

In fact, the weighted value of the shared connection is in the convolutional neural network. So, for example, if a layer might have hundreds of neurons, the weights could be only dozens of. By separating the two concepts, this sharing can be easily implemented.

[CLPR] C + + implementations of convolutional neural networks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.