Implement A Simple Neural Network in C# .NET - Part 1
Implement A Simple Neural Network in C# .NET - Part 1
NET —
Part 1
Deepak Battini Follow
Jul 9, 2018 · 5 min read
Last article “function of a neuron“, we saw how an artificial neuron is functioning with
manual training on AND gate data. We saw that after 6th iteration a simple network
learned and is now capable of predicting. Let’s put together the learning in the form of
code and do it programmatically. I will divide this post into two parts:
The idea of this building from scratch is to get to know more via code and not the
intention to build another deep learning library. There are many libraries which do the
job for you, and we will talk about those later by building more complex network and
train with lots of data (CSV and images). The idea of this post of to make the concept
more clear which will help build more knowledge with the already available libraries for
deep learning.
Before going into the code and divide it into various classes and function, let see the
below diagram again. We will use some notion from the below diagram while building
your own first neural network program.
class Pulse
{
public double Value { get; set; }
}
Dendrite
A simple projection of a neuron that receives the pulse input and feeds into the neuron
itself for computation. I have defined three properties for a Dendrite
SynapticWeight: The connection strength between the dendrite and synapsis of two
neurons.
Learnable: Just a bool used during training to adjust the SynapticWeight value
{
public Pulse InputPulse { get; set; }
Neuron
The neuron itself combined with its core function “Fire” and Dendrite. The workflow of
a neuron is it receives the input values, do the weighted sum of all the input signals from
all dendrites and pass them to the activation function which in this case is Step
activation function. The output of the Activation is assigned to OutputPulse which will
travel through the Axon of the neuron.
public Neuron()
{
Dendrites = new List<Dendrite>();
OutputPulse = new Pulse();
}
OutputPulse.Value = Activation(OutputPulse.Value);
}
return computeValue;
}
NeuralLayer
As you see the below image, we stack a bunch of neuron in the form of layers to build a
complex network, I have created another class which will create a layer first.
Weight = initialWeight;
Name = name;
}
NetworkModel
The NetworkModel is the complete architecture with layers of neuron and has Train
Read more stories this month when you
functionality.
create a free Medium account.
class NetworkModel
{
public List<NeuralLayer> Layers { get; set; }
public NetworkModel()
{
Layers = new List<NeuralLayer>();
}
if (Layers.Count > 0)
{
dendriteCount = Layers.Last().Neurons.Count;
}
i++;
}
}
foreach
Read more stories (var when
this month element
you in Layers)
{
create a free Medium account.
DataRow row = dt.NewRow();
row[0] = element.Name;
row[1] = element.Neurons.Count;
row[2] = element.Weight;
dt.Rows.Add(row);
}
Invoke the NetworkModel from the Main method in a Console Project like this below.
This will create a network with three layers with initial weights.
model.Build();
model.Print();
The code itself is self-explanatory, so I don’t bored you guys with lots of text. Next article
will implement the training method using the backpropagation algorithm and run a
sample training.