this is a prebuilt version of the application so
if I go ahead and run it and let's try
3 neurons in the hidden layer and
try running the Network and the first input
is the bias and we are using XOR
training data and the target result is
very different from the actual result so
let's try to do some training
and run it again and now they are very
close and they will get the closer as we
do more training
now those 3 neurons are the input
neurons
since we have 3 features and we are
displaying here the layer type and the
output from each one of the input
neurons and we have 3 neurons in the
hidden layer and each one of those
neurons have 3 weights and an output
and the output neuron has also 3
weights and the output and the output from this
neuron will be the same as the output from the
network so the number of neurons in the
hidden layer will be specified by the
user and there will be only one neuron
in the output layer and the number of
neurons in the input layer is data
dependent I will start by creating a new
project
and here we will have four classes the
neural network class where the logic for
back propagating the error and running the
network will be and a layer class
representing a layer in the network and
a neuron class representing a neuron in the network
and we will have an application driver
class with a main method now we have
three types of layers input, hidden and
output and a neuron will have
weights and an output and an error and
will belong to a specific layer and we
will have get and set methods for those and a toString method
that displays information about this neuron
and we will have a constructor
that initializes the layer type of this
neuron and if it belongs to the hidden
or the output layers then we randomly
initialize the weight for this neuron and we will
be using the sigmoid transfer function
so this method applies the activation
function and calculate the output and
this method uses the output in order to
calculate the derivative this should do
it for this class now before doing the
Layer class let's go to the Driver and define
the training data so we are using the XOR
training data so 0 & 0  and 1 & 1 will
give us 0 and 0 & 1 and 1 & 0 will give us 1
and this is always 1
and is used for the bias and here let's
define the number of input neurons which
will end up being 3 since we have 3
feature input and the number of output
neurons is 1 and the number of hidden neurons is specified by the
user now a layer will have an array of
neurons that belong to it and a it will
be of a layer type
and we'll have get methods for those and
the toString method will return
information about this layer and we
will have a constructor that takes in
the neural network that this layer
belongs to and it initializes the layer type
and depending on what layer we are in
we will either populate the neurons
array with input neurons or hidden
neurons or the output neurons this should do it
for this class now going to the neural
network it has an array of the different
layers that belong to this network and a
get method and the toString method will
return information about this network
and we will have a constructor that
initializes the number of hidden neurons
and populate this layers array with an
input and a hidden and an output layers
and we'll have two methods a forward
propagate method that runs the
network takes in an input vector and a back
propagate error method that takes in the
target
result and back propagates the error and this is
the code that runs the network so we go
through all the layers in the network
and if this is an input layer then we go
through all the neurons in that layer
and we set their output to be the input
that is coming in and if this is the
hidden layer then we also go through all
the neurons in that layer and we end up
calculating the weighted sum and passing
it to the apply activation function in
order to calculate the output for each
neuron in this layer and if this is the
output layer then we calculate the
weighted sum for the output neuron and we
pass that weighted sum to the apply
activation function for this output neuron
and we end up calculating the output for
this neuron
and finally this is the code that
back propagates
the error
so let's define the learning rate here
which is how fast the network learns
so here we pick up all the input neurons
and the hidden neurons and the output neuron
and we calculate the error on the output
neuron and we use that error in order to
calculate the weight for this output neuron
and we back propagate that error to the
hidden neurons and use it to calculate
the error on the hidden neurons and use
the hidden neurons errors in order to
calculate the weights on the hidden
neurons this should do it for this class
so next let's go to the driver class and
let me first define this method that prints out the result of running the network
and this is the code that drives the
application
and let me define the number of epochs
that the application will run
so we first prompt the user to enter the
number of neurons they want in the
hidden layer and based on that we
construct the network and then we go
into an always true loop where we prompt
the user to run or train or exit and if
they want to run then we go through all
the training data and we forward
propagate and get the outputs and put
them in the result array
and pass it to the print result static
method and if the user wants to train
then we go through all the epochs and
for each epoch we go through all the
training data and we forward propagate
then we back propagate the error and we
print out this statement when we are done
training this should do it for this
class next let's go ahead and test run
the application so let's try with four
neurons in the hidden layer and try running
the network and we have a big
difference between the target and the
actual result so let's do some training
and run the network again so now the target
and the actual results are very close so
we ended up having three input neurons
and four hidden neurons and one
output neuron and the output neuron has four
weights since we have four hidden
neurons and each one of the hidden
neurons have three weights
since we have three inputs and the
hidden and the output neurons have an output
each and the outputs on the output
neuron will be the same as the outputs
for the network
 
