designing and implementing a neural network library for handwriting detection, image analysis etc

20 415 0
designing and implementing a neural network library for handwriting detection, image analysis etc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Articles » General Programming » Algorithms & Recipes » Neural Networks Designing And Implementing A Neural Network Library For Handwriting Detection, Image Analysis etc The BrainNet Library - Full Code, Simplified Theory, Full Illustration, And Examples By Anoop Madhusudanan, 21 Oct 2009 Download source files and projects - 300 Kb This article will explain the actual concepts of Backward Propagation Neural Networks - in such a way that even a person with zero knowledge in neural networks can understand the required theory and concepts very easily. The related project demonstrates the designing and implementation of a fully working 'BackProp' Neural Network library, i.e, the Brain Net library as I call it. You can find the theory, illustration and concepts here - along with the explanation of the neural network library project - in this article. Also, find the full source code of the library and related demo projects (a simple pattern detector, a hand writing detection pad, an xml based neural network processing language etc) in the associated zip file. Contents 1. Overview 2. Before We Begin. 3. Understanding Neural Networks 3.1 Biological Neurons 3.2 Artificial Neurons 4. How A Neural Network Actually Works 4.1. Training Phase 4.2. Running The Network 5. Designing BrainNet Neural Network Library 5.1. The UML Model 4.75 (85 votes) 5.2. A Neuron In BrainNet Library 5.3. The Strategy Of A Neuron 5.4. A Neural Network In BrainNet library 5.5. Training The Network 5.6. Running The Network 5.7. Creating A Network What is Next Appendix A: Small Dose Of Spiritual Programming!! 1. Overview Solution Architect: "Well, you learned something about neural networks?" (Dumb?) Developer: "No, I'm smart enough. I love using other's code." Solution Architect: "But, if you don't understand the concepts, how you can optimize and re-use other's code?" (Dumb?) Developer: "Err I feel that most others can code better than me, so why should I optimize?" In my previous article, the focus was on what a neural network can do. In this article, we will see what a neural network is, and how to create one yourself. I will go a little deeper. After reading this article, you will be able to Understand the basic theory behind neural networks (backward propagation neural networks in particular) Understand how neural networks actually 'work' Understand in more detail, the design and source code of BrainNet library. Understand in more detail, how to use BrainNet Library in your projects. Think about new possibilities of neural network programming Put forward some concepts to optimize and generalize BrainNet library. Now, let me answer some questions I got in past. Q) Why you selected an object oriented programming model for this Neural Network Library? Answer - The focus is on the understandability of basic concepts, not on performance. Q) Is this neural network library fully optimized? Answer - Not yet, we are still in the beta stage. The focus is on readability, so the code is flattened so that even a beginner can understand it. Suggestions and modifications are always welcome. Send your modifications, hacks and suggestions to amazedsaint@gmail.com Q) Whether this library can be used in projects? You can use it - as long as your usage confronts to the specifications in the associated license notice (see the source code). Anyway, I request you to send me a notification (and the modified code), if you hack it or use it in any of your projects. 2. Before We Begin. This article is complete by itself. It explains what is a neural network, and how to create one your own. How ever, to get an idea regarding what a neural network can do, and to get a user level experience - please read the first part of this article. The first article in this article series is titled "BrainNet Neural Network Library - Part I - Learn Neural Network Programming step by step And Develop a Simple Handwriting Detection System". You can read it and download the source code, [ Click Here ]. If you are really a beginner, it will help you a lot, and may provide you a step by step approach towards understanding neural networks. This is my second article about Neural Networks in general and the BrainNet Neural Network Library in particular. This article explains Neural Networks and their working in more detail, and in a very simple way. Then I will explain the design concepts of BrainNet library. Tip - In this article, the theory about neural networks is explained in the most simplest (human readable) manner, so that even a person with zero background in neural networks can understand it. So, if you already know some theory about neural networks, you may consider skipping the theory part, and go ahead to the designing part which describes the design concepts of BrainNet library. 3. Understanding Neural Networks One fascinating thing about artificial neural networks is that, they are mainly inspired by the human brain. This doesn't mean that Artificial Neural Networks are exact simulations of the biological neural networks inside our brain - because the actual working of human brain is still a mystery. The concept of artificial neural networks emerged in its present form our very limited understanding about our own brain ("I know that I know nothing"). Brain Net Neural Network library is designed and implemented using Object Oriented Concepts. Alert - You can visit the articles section of my website here - http://amazedsaint.blogspot.com/ - for reading other articles in this series. You can also see there a full tutorial about OOP concepts in .NET and VB.NET programming. This may help you to understand this article better - especially if you don't have a good understanding regarding the Object Oriented Programming concepts. Also, read about design patterns there. Before understanding how neurons and neural networks actually work, let us revisit the structure of a neural network. As I mentioned earlier, a neural network consists of several layers, and each layer has a number of neurons in it. Neurons is one layer is connected to multiple or all neurons in the next layer. Input is fed to the neurons in input layer, and output is obtained from the neurons in the last layer. Fig: A Fully Connected 4-4-2 neural network with 4 neurons in input layer, 4 neurons in hidden layer and 2 neurons in output layer. An artificial neural network can learn from a set of samples. For training a neural network, first you provide a set of inputs and outputs. For example, if you need a neural network to detect fractures from an X-Ray of a born, first you train the network with a number of samples. You provide an X- Ray, along with the information that whether that particular X-Ray has a fracture or not. After training the network a number of times with a number of samples like this (probably thousands of samples), it is assumed that the neural network can 'detect' whether a given X-Ray indicates a fracture in the born (This is just an example). The concept of training a network is detailed in my first article. Later, in this article, we will discuss the theory behind network learning. As we already discussed, the basic component in a neural network is a neuron. First of all, let us have a very brief look towards biological neurons, and their corresponding artificial models. 3.1 Biological Neurons First of all, let us have a look at a biological neuron. Frankly, I don't have much knowledge regarding the actual structure of a biological neuron - how ever, the following information is more than enough at this stage for us to get in to the groove. A biological neuron will look some what similar to this. The four basic components of a biological neuron are Dendrites - Dendrites are hair like extensions of a neuron, and each dendrite can bring some input to the neuron (from neurons in the previous layer). These inputs are given to the soma. Soma - Soma is responsible for processing these inputs, and the output is provided to other neurons through the axon and synapses. Axon - The axon is responsible for carrying the output of soma to other neurons, through the synapses Synapses - Synapses of one neuron is connected to the dendrites of neurons in the next layer. The connections between neurons is possible because of synapses and dendrites. A single neuron is connected to multiple neurons (mostly, all neurons) in the next layer. Also, a neuron in one layer can accept inputs from more than one neuron (mostly, all neurons) in the previous layer. 3.2 Artificial Neurons Now, let us have a look at the model of an artificial neuron. An artificial neuron consists of various inputs, much like the biological neuron. Instead of Soma and Axon, we have a summation unit and a transfer function unit. The output of one neuron can be given as input to multiple neurons. Please note that for an artificial neuron, we have a weight value associated with each input. Now, let us have a look at the working of a neuron. Summation Unit When inputs are fed to the neuron, the summation unit will initially find the net-value. For finding the Net Value, the product of each input value and corresponding connection weight is calculated. i.e, input value x(i) of each input to the neuron is multiplied with the associated connection weight w(i). In simplest case, these products are summed and fed to the transfer function. See the pseudo code below, it is simpler to understand. Also, a neuron has a bias value, which affects the net value. A bias of a neuron is set to a random value, when the network is initialized. We will change the connection weights and bias of all neurons in the network (other than neurons in the input layer), during training phase. I.e, if x is the input, and w is the associated weight, then pseudo code for net value calculation is as follows. netValue=0 for i=0 to neuron.inputs.count-1 netValue=netValue + x(i) * w(i) next netValue=netValue + Bias Transfer Function Transfer function is a simple function, that uses the net value to generate an output. This output is then propagated to the neurons in the next layer. We can use various types of transfer functions as shown below. Hard Limit Transfer Function: For example, a simple hard limit function will output 1 if net value is greater than 0.5, and will output 0 if the net value is lesser than 0.5 - as shown. if (netValue<0.5) output = 0 else output = 1 Sigmoid Transfer Function: Another type of transfer function is a sigmoid transfer function. A sigmoid transfer function will take a net value as input and produce an output between 0 and 1 as shown. output = 1 / (1 + Exp(-netValue)) The implementation of summation unit and transfer function unit may vary in different networks. This, a neural network is constructed from such basic models, called neurons, arranged together in layers, and connected to each other as explained earlier. Now let us see how all these neurons work together, inside a neural network. 4. How A Neural Network Actually 'Works' Working with a neural network includes Training the network - by providing inputs and corresponding outputs. In this phase, we train a neural network with samples to perform a particular task. Running the network - by providing the input to obtain the output. In this phase, we will provide an input to the network, and obtain the output. The output may not be accurate always. Generally speaking, the accuracy of the output during running phase depends a lot on the samples we provided during the training phase, and the number of times we trained the network. 4.1. Training Phase This section explains how the training takes place, in a back ward propagation neural network. In a backward propagation neural network, there are several layers, and each neuron in each layer is connected to all neurons in the next layer. For each connection, a random weight is assigned when the network is initialized. Also, a random bias value is assigned to each neuron during initialization. Training is the process of adjusting the connection weights and bias of all neurons in the network (other than neurons in the input layer), to enable the network to produce expected output for all input sets. Now, let us see how the training actually happens. Consider a small 2-2-1 network. Now, we are going to train this network with AND truth table. As you know, AND truth table is AND TRUTH TABLE A B Output 0 0 0 0 1 0 1 0 0 1 1 1 Fig: A 2-2-1 Neural Network and Truth Table Of AND In the above network, N1 and N2 are neurons in input layer, N3 and N4 are neurons in hidden layer, and N5 is the neuron in output layer. The inputs are fed to N1 and N2. Each neuron in each layer is connected to all neurons in next layer. We call the above network a 2-2-1 network, based on the number of neurons in each layer. Tip - The concepts we are going to discuss here is largely biased towards a commonly used neural network model called Backward Propagation Neural Networks. How ever, you should understand that various other models also exist - like Counter Propagation Neural Networks, Kohanen's Self Organizing Maps etc. The above diagram will be used to illustrate the process of training. First, let us see how we train our 2-2-1 network, the first condition in the truth table, i.e, when A=0, B=0 then output=0. Step 1 - Feeding The Inputs Initially, we will feed the inputs to the neural network. This is done by simply setting the output of neurons in Layer 1, as the input values we need to feed. I.e, as per the above example, our inputs are 0,0 and output is 0. we will set the output of Neuron N1 as 0, and the output of N2 is set to 0. Have a look at this pseudo code, and it will make things clear. Inputs is the input array. The number of elements in Input array should match the number of neurons in input layer. i = 0 For Each neuron In InputLayer someNeuron.OutputValue = Inputs(i) i = i + 1 Next Step 2 - Finding the output of the network We have already seen how we calculate the output of a single neuron. As per our above example, the output of neurons N1 and N2 will act as the inputs of N3 and N4. Finding the output of neural network involves, calculating the outputs of all hidden layers and output layer. As we discussed earlier, a neural network can have a number of hidden layers. 'Find output of all neurons in all hidden layers For each layer in HiddenLayers For Each neuron In layer.Neurons neuron.UpdateOutput() Next Next 'Find output of all neurons in output layer For Each neuron In OutputLayer.Neurons neuron.UpdateOutput() Next UpdateOutput() function of a single neuron works exactly as we discussed earlier. First, net value is calculated by the summation unit, and then it is provided to a transfer function to obtain the output of the neuron. Pseudo code is again shown below. Summation Unit works like this: Dim netValue As Single = bias For Each InputNeuron connected to ThisNeuron netValue = netValue + (Weight Associated With InputNeuron * _ Output of InputNeuron) Next I.e, as per our above example, let us calculate the net value of neuron N3. We know that N1 and N2 are connected to N3. Net Value Of N3 = N3.Bias + (N1.Output * Weight Of Connection From N1 to N3) + (N2.Output * Weight Of Connection From N2 to N3) Similarly, to calculate the net value of N4, Net Value Of N4 = N4.Bias + (N1.Output * Weight Of Connection From N1 to N4) + (N2.Output * Weight Of Connection From N2 to N4) Activation Unit Or Transfer Unit: Now, let us see how we are generating the output, using Transfer unit. Here, we are using the sigmoid transfer function. This is exactly as we discussed earlier. Output of Neuron = 1 / (1 + Exp( - NetValue ) Now, the output of N3 and N4 will be passed to each neuron in the next layer as inputs. This process of propagating the output of one layer as the input to the next layer is called forward propagation part in the training phase. Thus, after step 2, we just found the output of each neuron in each layer - starting from the first hidden layer to the output layer. The output of the network is simply the output of all neurons in the output layer. Step 3 - Calculating The Error or Delta In this step, we will calculate the error of the network. Error or Delta can be stated as the difference between the expected output and the obtained output. For example, when we find the output value of the network for the first time, most probably the output will be wrong. We need to get 0 as the output for inputs A=0 and B=0. But the output may be, some other value like 0.55, based on the random values assigned to the bias and connection weights of each neuron. Now let us see, how we can calculate the error. Let us see how to calculate the error or delta of each neuron in all the layers. First we will calculate the error or delta of each neuron in the output layer. The delta value thus calculated will be used to calculate the error or delta of neurons in the previous layer (i.e, the last hidden layer) The delta value of all neurons in the last hidden layer is used to calculate the error or delta of all neurons in the previous layer (i.e, second last hidden layer) previous layer (i.e, second last hidden layer) This process is continued, till we reach the first hidden layer (delta of input layer is not calculated). Please note one interesting point. In Step 2, we are propagating values forward - starting from the first hidden layer to the output layer, for finding the output. In Step 3, we are starting from the output layer, and propagating the error values backward - and hence, this neural network is called as a Backward Propagation neural network. Time to see how things actually work. The general equation for finding the delta of a neuron is Neuron.Delta = Neuron.Output * (1 - Neuron.Output) * ErrorFactor Now, let us see how the error factor is calculated for each neuron. The Error Factor of neurons in output layer can be calculated directly (since we know the expected output of each neuron in output layer). For a neuron in output layer, ErrorFactor Of An Output Layer Neuron = _ ExpectedOutput - Neuron's Actual Output i.e, with respect to our above example, if the output of N5 is 0.5 and the expected output is 0, then error factor = 0 - 0.5 = - 0.5 For a neuron in hidden layer, error factor calculation is some what different. To calculate the error factor of a neuron in hidden layer, First the delta of each neuron to which this neuron is connected is multiplied with the weight of this connection These products are summed up together to obtain the error factor of a hidden layer neuron Simply speaking, a neuron in a hidden layer is using the delta of all connected neurons in next layer, along with the corresponding connection weights, to find the error factor. This is because, we don't have any direct parameters for calculating the error of neurons in the hidden layer (as we did in the output layer neurons). Remember - To calculate the output of a neuron, we used the outputs of connected neurons in previous layer, along with the corresponding connection weights. 'Calculating the error factor of a neuron in a hidden layer For Each Neuron N to which ThisNeuron Is Connected 'Sum up all the delta * weight errorFactor = errorFactor + (N.DeltaValue * _ Weight Of Connection From ThisNeuron To N) Next To illustrate this, consider a neuron x1 (ThisNeuron), which is a hidden layer neuron. X1 is connected to neurons y1, y2, y3 and y4 - and these are neurons in next layer. i.e, to make things simple, Error Factor of X1 = (Y1.Delta * Weight Of Connection From X1 To Y1) + (Y2.Delta * Weight Of Connection From X1 To Y2) + (Y3.Delta * Weight Of Connection From X1 To Y3) + (Y4.Delta * Weight Of Connection From X4 To Y4) Now, as we discussed earlier, the Delta of a X1 can be calculated as, X1.Delta = X1.Output * (1 - X1.Output) * ErrorFactor Of X1 Thus, after finishing step 3, we have the Delta of all neurons. Step 4 - Adjusting The Weights and Bias After calculating the delta of all neurons in all layers, we should correct the weights and bias with respect to the error or delta, to produce a more accurate output next time. Connection Weights and Bias, together are called free parameters. Remember that a neuron should update more than one number of weights - because, as we already discussed, there is a weight associated with each connection to a neuron. See the pseudo code for updating the free parameters of all neurons in all layers 'Update free parameters of all neurons in hidden layer For each layer in HiddenLayers For Each neuron In layer.Neurons neuron.UpdateFreeParams() Next Next 'Update free parameters of all neurons in output layer For Each neuron In OutputLayer.Neurons neuron.UpdateFreeParams() Next UpdateFreeParams() function simply does two things. Find the new bias of a neuron, based on the delta we calculated above Update the connection weights based on the delta we calculated above Finding the new bias value of a neuron is pretty simple. See the pseudo code. If Learning Rate is a constant (for e.g, Learning Rate=0.5) New Bias Value = Old Bias Value + _ LEARNING_RATE * 1 * Delta Now let us see how to update the connection weights. The new weight associated with an input neuron can be [...]... interface A Neural Network should implement the INeuralNetwork interface A Neuron has a strategy, and a strategy should implement the INeuronStrategy interface We have a concrete implementation of INeuronStrategy, called BackPropNeuronStrategy (for a backward propagation neural network) A Neural Network is initialized and connections betweens layers are made by a neural network factory A Factory should... the INetworkFactory interface We have a concrete implementation of INetworkFactory, called BackPropNetworkFactory, for creating Backward Propagation neural networks The major interfaces in the model are briefed below INetworkFactory An interface to define a neural network factory INeuron The interface for defining a neuron INeuronStrategy The interface for defining the strategy of the neuron INeuralNetwork... a Neural Network consists of various Neuron Layers, and each Neuron Layer has various Neurons A Neuron has a strategy - which decides how it should perform tasks like summation, activation, error calculation, bias adjustment, weight adjustment etc To brief the UML diagram above, INeuron, INeuronStrategy, INeuralNetwork and INetworkFactory are interfaces A Neuron should implement the INeuron interface... interface for defining a neural network The major classes in the model are briefed below BackPropNeuronStrategy A backward propagation neuron strategy This is a concrete implementation of INeuronStrategy The class is to help the user to initialize and train the network It maintains a list of NetworkHelper training data elements NeuralNetwork A generic neural network This is a concrete implementation... bnCnetaes) n.oncLyr( 'o tentoki ray d ohrsufhr Nw h ewr s ed, o te tf ee EdFnto n ucin Or better, you can use the BackPropNetworkFactory class to create a network easily Have a look at the BackPropNetworkFactory class It has two overloaded CreateNetwork functions, for creating a neural network Some notes This article is much like a 'Developers Guide' of BrainNet neural network library Have a look at my... create, build or assemble a working program from his abstract concepts about a system The quality of this transformation depends a lot on how well he understand the system At this point, I would like to mention that Brain Net Library is actually not designed after a complete and thorough understanding of various existing neural network models and emerging possibilities in the area of neural networks Hence,... be trained with sufficient samples (and sufficient number of times), to obtain desired results Anyway, it is almost impossible to say that the output of a neural network will be 100% accurate for any input Now, let us see how these concepts are implemented in BrainNet Neural Network Library 5 Designing BrainNet Neural Network Library The fundamental challenge for any solution developer is to create,... aes) s ernaeCleto 'estefrt(nu)lyr Gt h is ipt ae RaOl Poet Iptae( A NuoLyr edny rpry nuLyr) s ernae 'estels (upt lyr Gt h at otu) ae RaOl Poet OtuLyr )A NuoLyr edny rpry uptae( s ernae EdItrae n nefc There are two interesting functions, TrainNetwork and RunNetwork, for training and running the network The input to the TrainNetwork function is an object of TrainingData class The TrainingData class has... described earlier It is pretty easy to understand 5.4 A Neural Network In BrainNet library Now, let us see how the Neural Network is implemented Any concrete neural network should implement the INeuralNetwork interface INeuralNetwork interface is shown below Pbi ItraeIerlewr ulc nefc NuaNtok 'ehdt tanantok Mto o ri ewr SbTanewr(ya tA TannDt) u riNtokBVl s riigaa 'hsfnto cnb ue frcnetn tonuostgte Ti ucin a e... library, and try to optimize it a little bit, or even better, create a neural network yourself using this as an example In my next article, I will explain how to create an XML based language yourself, for creating, training and processing neural networks Explain the concept of some classes in the framework that I haven't mentioned in this article (like NXML interpreter, NetworkSerializer etc) There are . Articles » General Programming » Algorithms & Recipes » Neural Networks Designing And Implementing A Neural Network Library For Handwriting Detection, Image Analysis etc The BrainNet Library. article about Neural Networks in general and the BrainNet Neural Network Library in particular. This article explains Neural Networks and their working in more detail, and in a very simple way section explains how the training takes place, in a back ward propagation neural network. In a backward propagation neural network, there are several layers, and each neuron in each layer is connected

Ngày đăng: 28/04/2014, 10:10

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan