- May 15, 2020

DEVPSU Project DetailsArtificial Neural Network ScriptThe first step to creating some complex Artificial Intelligence programs is to first understand how to program a basic Artificial Neural Network (ANN)! ANN’s are brain-inspired systems which replicate the way in which humans learn. They do this by using a series of layers and functions that help the program get to the final predicted answer. For this project we will be using James Loy’s example of an ANN and predict the outcome of an AND logic gate.Let’s get started!RequirementsYou will be creating an ANN from scratch that should:Contain all components of a Neural NetworkGet the expected outputInstallationFirst make sure you have installed Python 3 or higher.You will also need a text editor (ex. Sublime Text) or you can use the python shell. Download the boiler code (NeuralNetwork.py)If you have windows, you might have a problem using pip…’pip’ is not recognized as an internal or external commandAll you need to type in the command prompt is -python -m pip python3 -m pipThe only package we will be using for this project will be NumPy. If you do not have NumPy already installed, go into your Terminal (Mac) or Command Prompt (Windows) and type in:pip install –upgrade pip pip install numpyOr depending on the Python installation you have: pip3 install –upgrade pippip3 install numpyIt’s best to try both sets of commands (it won’t cause problems if you run all 4 commands, even if the pip3 commands don’t work)You now have everything you need to create your Neural Network!InstructionsFirst, we must import our numpy library as np, so that we can use Numpy for calculations later in the program.We now need to define our activation function that will be used to predict our output. In the sigmoid(x) function, you should return 1.0 / (1+ np.exp(-x)). Remember that np is just an alias for numpy, so np.exp(-x) is essentially just e-x.Our sigmoid_derivative function will be used to fix our predicted output to become more accurate. We will simply return the derivative of sigmoid(x) (x*(1.0-x))We will now create our actual NeuralNetwork (class/object)! First, we must initialize some datain the init (self, x, y) constructor of the class: our input (x), our randomized weights, theexpected output (y), and the actual final output of our network:self.input = xself.weights1 = np.random.rand(self.input.shape[1],4) self.weights2 = np.random.rand(4,1)self.y = yself.output = np.zeros(self.y.shape)Next, we will program our feedforward(self) propagation. This will take the dot product of our class’s input and first weights1 to create our first hidden layer (using the np.dot function). It will then take the dot product of our hidden layer1 (which was just calculated) and second weights2 to create our final output (using the np.dot function):self.layer1 = sigmoid(np.dot(insert_arg_1, insert_arg_2)) self.output = sigmoid(np.dot(insert_arg_1, insert_arg_2))Now, we have our final output. Yay! However, this will not be very close to our expected output so here is where we use backprop(self). This will take our output and adjust the weights in order to get closer to our expected output. This has been written for you since it’s a little complicated. If you want to learn more about how it works, feel free to ask!Time to see if our ANN will predict the correct output for an AND gate. Here’s a table that shows outputs given two inputs:In our main statement, we need to set the variable X equal to our 4 input variations. Thearray is currently empty. You will HAVE to fill all the variations into the array: 0, 00, 11, 01, 1The variable y will be our expected outputs for the AND gate which are 0, 0, 0, and 1. You willHAVE to fill these in.Next, we will create a Neural Network (the class we just created) and run it for 1000 iterations. Finally, we print our final result! (this is done for you)Your printed output should be close to the actual output of 0, 0, 0, 1, but it might not be exactly correct.Congratulations, you completed your first project and wrote a Neural Network that can predict the outputs of an AND gate!If you are looking for an extra challenge:See what happens when you increase the number of trials from 1000Try using the same Network but for an OR gate and NAND gateTry using the same Network for some custom state machineSubmissionPlease submit your finished code (NeuralNetwork.py), as well as your output from the terminal. If successful, your code should output numbers close to, or the same as, the actual output. Try running it multiple times.