we have a prebuilt version of the application so if I go ahead and run it and
run the network
so we are using the XOR training data
and there is a big difference between the target result and the actual result
so let's go ahead and do some training
and run the network
so now the target result and the actual result are very close so
as we do more training
the target and actual result gets closer
now the network have two input neurons
So I stands for
input and we have two neurons in the hidden layer H stand for hidden and
we have one neuron in the output layer so O stand for output and
for the input neurons we display the output for each one of those neurons
so we have 0 and 0    0 and 1
1 and 0 and 1 and 1 and for the hidden and
output neurons we display the layer type
and then the weights weight 1 and weight 2 and the threshold and
the output so for the output neuron this is the output
which is the same as the output from the network
i will start by creating a new project
and here we will have
an application driver class with a main method
And a
neural network class
where running the network and
back propagating the error
logic will be
and a
neuron class representing a neuron in the network
and
the neuron can belong to one of three layer types
Either the input layer or the hidden layer or the output layer
now the neuron will have a
layer type and
a threshold
that we randomly initialize and
two weights that we also randomly initialize and an output and
an error
and get and set methods
and
a toString method
that prints out information about this neuron and
this method applies the Sigmoid
transfer function
and calculates the output
and
this method calculates the derivative, so it is the output that we calculated here
times 1 minus the output
and we will have a constructor with the layer type passed in
this should do it for this class now the neural network
will have two inputs neurons and two
hidden neurons and
one output
neuron and
we will have a constructor that
instantiates those neurons
and put them in this array
and this neurons will have a get method
and
we will have a toString method
that
prints out information about neurons that are contained in this network
 
and this method will run the network
given an input that is passed in
and this method will backpropagate the error
and it takes in the target result
and this the a code that runs the network so we go through each one of the neurons and
if it is an input neuron
then we set its output to be the input that is
coming in and if it is a hidden neuron then we calculate its weighted sum
which would be the threshold for this neuron
plus
weight zero for this neuron times the output coming out of neuron zero in the input layer
plus weight one for this neuron times the output coming out of
neuron one in the input layer and
We use that
weighted sum to pass it to the apply activation function in order to calculate the
output for this neuron
and if this neuron is an output neuron then we do the same thing
we calculate the weighted sum by adding the threshold for this neuron plus
weight zero for this neuron times the output coming out of neuron two in the hidden layer plus
weight one for this neuron times the output coming out of
neuron three in the hidden layer, and we use that
calculated weighted sum to pass it to the apply activation function in order to calculate the output
for this neuron
and
let's define
this learning rate which controls how fast the network learns
and this is the code that back propagate the error
now this is the network that we implemented so neuron 4 is the output
neuron and it has a threshold and the two weights w0 and w1
and neuron 2 and neuron 3 are in the hidden layer, and they each have a threshold and
two weights
we first calculate and
set the error on
neuron 4 in the output
layer so that error would be the target result minus the output from this neuron
multiplied by the derivative of this neuron and
then we use that
error in order to calculate and
set the threshold and the 2 weights on this neuron and then we back propagate
that error to both neuron 3 so here we have neuron 4 dot get error and
to neuron 2
same thing here we have neuron 4 dot get error and
both of those neurons are in the hidden layer and
then after calculating
and setting the error for neuron 3 we use that error in order to calculate and
set the threshold and the two weights on neuron 3
and we do the same thing for neuron 2
this should do it for this class
now in the driver class
i'll define the number of epochs that this application will run
and
we will be using the XOR as the training data so 0 and 0     and    1 and 1     will give us 0
and    0 and 1    and    1 and 0     will give us 1
and
i will define this
static
method that prints the result of running the network
and
this is the code that drives the application
i will start by
prompting the user for what they want to do and they can either run, train, or exit and
if they want to run then I will go through all the training data and
forward propagate and
then get the output from the output neuron and
populate the result array and then print out the results of running this
network, and if the user selected to train
then I will train for the number of epochs that are specified
so for each epoch. We'll go through all the training data and
forward
propagate and then back propagate the error
and we'll print out this statement when we are done training
next let's go ahead and test run the application
so let's try running first and
there is a big difference between the target result and the actual result that we obtained
so let's try doing some training
and running again
So now the target result and the actual result are very close
