Neural Networks w/ JAVA – Tutorial 04


here we have three input x y and z and a bias input that is always 1 and we want to find the equation
of a plane that separates the 0s and the 1s and given that this is the equation of
a plane so we start by randomly initializing the weights in this case the
weights are w0 w1 w2 and w3 and then we go into a loop where we first
calculate the weighted sum so ws is equal to w0 plus w1 times x plus w2
times y plus w3 times z and 2nd we apply an activation function and in this case we have a step function so if ws is bigger than the
threshold of the step function then the result will be 1 and it will be 0
otherwise and 3rd we determine the error and
the error will be the target result minus the result and 4th we adjust the
weights so w1 will be equal to the learning rate times the error times x
plus the original w1 and w2 will be equal to the learning rate times the
error times y plus the original w2 and same thing for w3 and the learning
rate here is the rate at which the neural network learns and it ranges from 0 to 1
and we repeat steps 1 through 4 for all vectors in the training data for all
those vectors and this is one epoch which is one pass
through all training vectors and then we outer loop through additional epochs
until the error is equal to zero for all training vectors in the final epoch and
the objective is to determine what weights w0 w1 w2 and w3 would lead to
the target result equaling to the result for all vectors in the training data so
what we do is we set ws is equal to the threshold so w0 plus w1 times x
plus w2 times y plus w3 times z is equal to the threshold which gives us
this equation and since the equation of a plane is ax + by + cz + d=0 this means that a is equal to w1 and b is equal to w2 and c is equal
to w3 and d is equal to w0 – threshold this is a prebuilt version of
the application so if we go ahead and run it we went from epoch 0 to epoch 3 and
we discovered this plane equation that separates four points in
three-dimensional space x y and z so x0 here is the bias which is always 1 and we discovered w0 equal 0.19
and w1 equal 0.53 w2 equals 0.54 and w3 equals 0.28 and the target result
equal to the result for all four vectors and we started in epoch 0 with the
target result different from the result in two vectors next I went ahead and created a new
project with two classes a Driver class with a main method and a Perceptron
class and the code here will be the same as in the previous tutorial the
difference is that we have a bias here that is always 1 and 4 points
in a three dimensional space and this is target result for each one of those
points and this is w0 this is w1 w2 and w3 now going to the Driver
class we’ll have two utility methods a print heading method that will print
the heading for each one of the epochs and a print vector method that will
print the data below the heading so I will start here by picking up the
training data and in main let’s go ahead and and pick up
the initial weights and I will instantiate a driver and a perceptron and
we will start from epoch number 0 and i will define an error flag a
boolean error flag and i will start it with true and in error start it as
0 and let’s define the adjusted weights array and go into a a while loop while the
error flag is true we will print the heading for the epoch and we will go
into a for loop to print the data below the heading so for each one of the
vectors in the data will first pick up the weighted sum by calling calculate
weighted sum on the perceptron instance and then we will pick up the result by
applying the activation function to the weighted sum that we picked up here so
perceptron dot apply activation function and before the for loop we will
reinitialize the error flag and the error for each one of the epochs so this
will be set initially to false and the error to zero and in the for loop we will pick
up the error for this specific vector in the for loop so the error will be the
target result minus the result and if we have an error then we set the error flag
to true so that we stay in the while loop and then we will pick up the adjusted
weights so we call perceptron dot adjust weights and we will print this vector and
set the weights to the adjusted weights array that we picked up over here and after finishing will print out the
plane equation so w1 x1 and w2 times y and w3 times z and w0 times 1 which
is w0 minus the threshold minus perceptron dot threshold and this will
equal to 0 and finally let’s go ahead and test run the application so
we went from epoch 0 where the target result is different than the result in those
two vectors we went to epoch 2 and the target result is equal to the result in
all vectors and this is the plane equation that we discovered that
separates the 1s from the 0s in three-dimensional space so the discovered w0 is 0.48 w1 0.14 w2
0.22 and w3 0.57

Leave a Reply

Your email address will not be published. Required fields are marked *