In this article (hopefully, the first of a small series), we'll see how to implement a neural network in Visual Basic .NET, i.e. a model capable of processing input data and adjust its internal mechanics to learn how to produce a desired result. We'll see more on this later. The present article will focus on generic definitions about neural networks and their behaviors, offering a simple implementation for the reader to test. In the final paragraph, we will code a small network capable of swapping two variables.
Introduction
A first definition
The term "neural network" is typically used as a reference to a network or circuit constituted by neurons. We can differentiate two types of neural networks: a) biological and b) artificial. Obviously, speaking about software development, we are here referring to artificial ones, but those kind of implementations get their basic model and inspiration from their natural counterparts, so it can be useful to briefly consider the functioning of what we intend when we speak of biological neural networks.
Natural neural networks
Those are networks constituted by biological neurons, and they are typical of living creatures. The neurons/cells are interconnected into the peripheral nervous system or in the central one. In neurosciences, groups of neurons are identified by the physiological function they perform.
Artificial neural networks
Artificial networks are mathematical models, which could be implemented through an electronic medium, which mime the functioning of a biological network. Simply speaking, we will have a set of artificial neurons apt to solve a particular problem in the field of artificial intelligence. Like a natural one, an artificial network could "learn", through time and trial, the nature of a problem, becoming more and more efficient in solving it.
Neurons
After this simple premise, it should be obvious that in a network, being it natural or artificial, the entity known as "neuron" has a paramount importance, because it receive inputs, and is somewhat responsible of a correct data processing, which end in a result. Think about our brain: it's a wonderful supercomputer composed by 86*10^9 neurons (more or less). An amazing number of entities which constantly exchange and store information, running on 10^14 synapses. Like we've said, artificial models are trying to capture and reproduce the basic functioning of a neuron, which is based on 3 main parts:
- Soma, or cellular body
- Axon, the neuron output line
- Dendrite, the neuron input line, which receives the data from other axons through synapses
How a network learns
Typically, the neural network theory identifies 3 main methods through which a network can learn (where, with "learn", we intend - from now on - the process through which a neural network modifies itself to being able to produce a certain result with a given input). Regarding the Visual Basic implementation, we will focus only on one of them, but it's useful to introduce all the paradigms, in order to have a better overview. For a NN (neural network) to learn, it must be "trained". The training can be supervisioned, if we possess a set of data constituted by input and output values. Through them, a network could learn to infer the relation which binds a neuron with another one. Another method is the unsupervisioned one, which is based on training algorithms which modifies the network's weights relying only on input data, resulting in networks that will learn to group received information with probabilistic methods. The last method is the reinforced learning, which doesn't rely on presented data, but on exploration algorithms which produce inputs that will be tested through an agent, which will check their impact on the network, trying to determine the NN performance on a given problem. In this article, when we'll come to code, we will see the first presented case, or the supervisioned training.
Supervisioned training
So, let's consider the method from a closer perspective. What it means to training a NN with supervision? As we've said, it deals primarily of presenting a set of input and output data. Let's suppose we want to teach our network to sum two numbers. In that case, following the supervision training paradigm, we must feed our network with input data (lets say [1;5]) but also telling it what we expect as a result (in our case, [6]). Then, a particular algorithms must be applied in order to evaluate the current status of the network, adjusting it by processing our input and output data. The algorithm we will use in our example is called backpropagation.
Backpropagation
The backpropagation of errors is a technique in which we first initialize our network (typically with random values regarding the weights on single neurons), proceding in forwarding our input data, matching results with output data we expect. Then we calculate the deviation of the obtained values from the desired one, obtaining a delta factor which must be backpropagated to our neurons, to adjust their initial state according to the entity of error we've calculated. Through trials and repetitions, several sets of input and output data are presented to the network, each time repeating the match between the real and the ideal value. In a certain time, that kind of operation will produce increasingly precise outputs, calibrating the weight of each network's components, and ultimately refining its ability to process the received data. For an extensive explanation about backpropagation, please refer to the Bibliography section.
Creating a neural network
Now that we saw some preliminar concepts about neural networks, we should be able to develop a model which responds to the discussed paradigms. Without exceeding in mathematical over-explanations (which aren't really needed, unless you want to understand better what we will see), we will proceed step by step in coding a simple yet functional neural network, that we'll test when completed. The first thing we need to consider is the structure of a neural network: we know it's organized in neurons, and the neurons themselves are interconnected. But we don't know how.
Layers
And that's where the layers comes into scene. A layer is a group of neurons which share a more or less common function. For example, think about the entry point of input data. That will be the input layer, in which resides a group of neurons which share a common functionality, in that case limited to receiving and forward information. Then we'll surely have an output layer, which will group those neurons that receive the result of previous processing. Among those layers could reside many layers, typically called "hidden", because an user won't have direct access to them. The number of those hidden layers, and the number of neurons each of them will contain, depends heavily on the nature and complexity of the problem we want to solve. To sum up, each network will be constituted of layers, each of which will contain a certain predetermined number of neurons.
Neurons and dendrites
"Artificially" speaking, we could conceive a neuron like an entity which exposes a certain value, adjusted by trial iterations, and bind to other neurons through dendrites, which in our case will be represented by sub-entities possessing an initial random weight. The training process will consist in feeding the input layer neurons, which transmit their value through dendrites to an upper layer, which will do the same thing until the output layer is reached. Finally, we calculate a delta between the current output and the desired one, recurring through the network and adjusting the dendrites weights, the neuron values and each deviation value, to correct the network itself. Then we start again with another training round.
Preparing network classes
Having saw how a network is structured, we could sketch down some classes to manage the various entities of a network. In the following snippet, i will outline Dendrite, Neuron and Layer classes, which we will use together in the implementation of a NeuralNetwork class.
Dendrite class
- Public Class Dendrite
- Dim _weight As Double
- Property Weight As Double
- Get
- Return _weight
- End Get
- Set(value As Double)
- _weight = value
- End Set
- End Property
- Public Sub New()
- Me.Weight = r.NextDouble()
- End Sub
- End Class
Neuron class
- Public Class Neuron
- Dim _dendrites As New List(Of Dendrite)
- Dim _dendriteCount As Integer
- Dim _bias As Double
- Dim _value As Double
- Dim _delta As Double
- Public Property Dendrites As List(Of Dendrite)
- Get
- Return _dendrites
- End Get
- Set(value As List(Of Dendrite))
- _dendrites = value
- End Set
- End Property
- Public Property Bias As Double
- Get
- Return _bias
- End Get
- Set(value As Double)
- _bias = value
- End Set
- End Property
- Public Property Value As Double
- Get
- Return _value
- End Get
- Set(value As Double)
- _value = value
- End Set
- End Property
- Public Property Delta As Double
- Get
- Return _delta
- End Get
- Set(value As Double)
- _delta = value
- End Set
- End Property
- Public ReadOnly Property DendriteCount As Integer
- Get
- Return _dendrites.Count
- End Get
- End Property
- Public Sub New()
- Me.Bias = r.NextDouble()
- End Sub
- End Class
Layer class
- Public Class Layer
- Dim _neurons As New List(Of Neuron)
- Dim _neuronCount As Integer
- Public Property Neurons As List(Of Neuron)
- Get
- Return _neurons
- End Get
- Set(value As List(Of Neuron))
- _neurons = value
- End Set
- End Property
- Public ReadOnly Property NeuronCount As Integer
- Get
- Return _neurons.Count
- End Get
- End Property
- Public Sub New(neuronNum As Integer)
- _neuronCount = neuronNum
- End Sub
- End Class
NeuralNetwork class
Our NeuralNetwork can be seen as a list of layers (each of which will inherit the underlying layer properties, i.e. neurons and dendrites). A neural network must be launched (or put in running state) and trained, so we will likely have two methods which can be used for that means. The network initialization must specify, among other properties, a parameter that we will call "learning rate". That will be a variable we'll use in weights recalculation. As the name implies, the learning rate is a factor which determines how fast a network will learn. Since it's a corrective factor, the learning rate must be chosen accurately: if its value is too large, but there is a large multitude of possible inputs, a network may not learn too well, or at all. Generally speaking, a good practice is to set the learning rate to a relatively small value, increasing it if the effective recalibration of our network becomes too slow.
Lets see an almost complete NeuralNetwork class:




RakeshPosted Sep 9, 2015, 4:56 AM
Good Explain sir
Ankit BansalPosted Sep 9, 2015, 4:30 AM
thanks for sharing..
Karthikeyan KPosted Sep 8, 2015, 11:20 PM
Good one sir...Thanks for sharing
Syed ShanuPosted Sep 8, 2015, 8:17 PM
Nice article with detaild explanations.I was remembering my college days.I like the way you have used the VB.Net, as everyone is willing to work on C#.I love both VB.NET and C# not find much differance in that.
Pankaj Kumar ChoudharyPosted Sep 8, 2015, 7:54 PM
Nice Explain Sir........
Santhakumar MunuswamyPosted Sep 8, 2015, 3:20 PM
Good Article
Mohammed IbrahimPosted Sep 8, 2015, 3:12 PM
nice share