# DEVPSU Project Details

## Artificial Neural Network Script

The 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!

### Requirements

You will be creating an ANN from scratch that should:

Contain all components of a Neural Network

Get the expected output

### Installation

First 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 command

All you need to type in the command prompt is -

python -m pip python3 -m pip

The 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 numpy

Or depending on the Python installation you have: pip3 install --upgrade pip

pip3 install numpy

It’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!

### Instructions

First, 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 data

in the __ __

### init (self, x, y) constructor of the class: our input (x), our randomized weights, the

expected output (y), and the actual final output of our network:

self.input = x

self.weights1 = np.random.rand(self.input.shape[1],4) self.weights2 = np.random.rand(4,1)

self.y = y

self.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. The

array is currently empty. You will **HAVE **to fill all the variations into the array: 0, 0

0, 1

1, 0

1, 1

The variable y will be our expected outputs for the AND gate which are 0, 0, 0, and 1. You will

### HAVE 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 1000

Try using the same Network but for an OR gate and NAND gate

Try using the same Network for some custom state machine

### Submission

Please 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.