Beginner Intro to Neural Networks 4 - First Neural Network in Python

Activating the Environment

To start using the environment set up in the previous video, we need to type "source activate" followed by the name of the environment, which was "NN series" in this case. The command is then followed by hitting return.

Opening Jupiter Notebook and Creating a New Python Environment

We are going to create a new notebook on our desktop and open it up to create a new Python environment called "NN". In Jupiter notebook, we can do this by typing "new Python - NN" and hitting enter. This will allow us to use the functions and libraries available in the "NN" environment.

Importing Necessary Libraries

Before we can define our neural network as a function, we need to import the necessary library, which is NumPy. We can do this by typing "import numpy" and hitting return.

Defining the Neural Network Function

Now that we have imported the necessary library, we can define our neural network function. The function will take two measurements, m1 and m2, and weight them with two weights, w1 and w2, as well as a bias number. The function will then calculate an intermediate value called Z by multiplying m1 with w1, adding m2 times w2, plus the bias.

The function will then output a number between 0 and 1 using the sigmoid function, which is not a built-in function in NumPy. We need to define the sigmoid function ourselves before we can use it in our neural network. The sigmoid function takes one input X and returns 1 divided by 1 plus the exponential of negative X.

Defining the Sigmoid Function

To define the sigmoid function, we need to type "def sigmoid(X): return 1 / (1 + numpy.exp(-X))" and hit return. This will allow us to use the sigmoid function in our neural network.

Running the Neural Network Function with Random Connections

Now that we have defined both the neural network function and the sigmoid function, we can run the neural network function using random connections for W1 and W2, as well as a bias value. We can do this by typing "W1 = numpy.random.rand(); W2 = numpy.random.rand(); B = numpy.random.rand()" and hitting enter.

Running the Neural Network Function with Specific Measurements

To test our neural network, we need to provide it with specific measurements for m1 and m2, as well as the random parameters W1, W2, and bias. We can do this by typing "m1 = 3; m2 = 1.5" and hitting enter, followed by "nn.m1 = m1; nn.W1 = W1; nn.W2 = W2; nn.B = B". Then we can run the function by typing "print(nn.predict(m1, m2))".

Interpreting the Results

The output of our neural network function is a number between 0 and 1. To interpret this result, we need to consider what the measurements m1 and m2 represent. In this case, they are from a flower, with m1 representing the length of the stem and m2 representing the diameter of the flower.

The computer is interpreting these measurements as input values for its neural network function. The output number is a probability value that the flower is red or blue, based on the inputs provided to the neural network function.

Predictions from the Neural Network

To further test our neural network, we can run it with different measurements and see what predictions it makes. We can do this by typing "m1 = 3; m2 = 8" and hitting enter, followed by "print(nn.predict(m1, m2))". The output is a number between 0 and 1 that the computer interprets as a probability value for the flower being red or blue.

The predictions from our neural network are completely random at this point, which is not surprising since we have randomly generated the connections W1 and W2. In the next video, we will take a step back and look at something called a cost function and a bit of calculus to get us moving in the right direction to find the right set of parameters for our neural network.

"WEBVTTKind: captionsLanguage: enin the last video we got Python setup on our computer in this video we're going to write our neural network in Python and see some predictions so to get started let's open up Jupiter notebook to do so we need a terminal window so we'll hit command space type terminal hit return now let's activate our environment we set up in the previous video so to do so we'll type source activate and then the name of the environment mine was NN series hit return let's open up Jupiter notebook I'm going to create this notebook on my desktop and we'll go to new Python - let's call it NN now I'm going to use stuff from numpy so I'm going to say import numpy so we have access to that now let's define our neural network as a function so to do so we'll type d EF the name of the function and n it's going to take two measurements m1 and m2 it's going to weight them by two weights called w1 and w2 and it's going to add a bias number to that now let's calculate an intermediate value in our neural network called Z so Z is going to be m1 times w1 plus m2 times w2 plus the bias and our neural network outputs number is between 0 and 1 and we know what's grade for that sigmoid so let's return sigmoid of z it returned a few times and before we define this let's define sigmoid itself it's not a built-in function so let's BEF sigmoid it takes one input X and it returns 1 over 1 plus numpy dot exponential of negative x so let's shift return to find that we have our neural network function to run it we need some data and we also need some weights we need some values for W 1 W 2 and B and we also need some values for M 1 and M 2 so that when we plug them in that all these get replaced with actual numbers so they can turn into something like 2 times 3 plus 3 times 4 plus negative 7 and then you can simplify that take sigmoid of it and you'll get out a number so we need some values for all these inputs to our neural network well for the waist it's kind of easy we can start with random connections so to do so we'll use a built in thing from numpy called R and n it's a function so let's type num PI dot random dot R and and and I can run this repeatedly by holding ctrl and tapping return and you see we get numbers that are all pretty close to 0 they're negative and positive and this is sampling from a normal distribution so we have some random numbers for our connections let's actually assign them into variables so W 1 is equal to a random number W 2 is equal to a random number and our bias is a random number to run that let's take a look at them W 1 is 1 point 4 3 2 is minus point 4 1 and the bias is one point seven four etc etc ok so to run our neural network now and get a prediction we need our measurements from the farmer and our random parameters so let's type the name of the function and n we'll put in a measurement let's try 3 and 1.5 and our random parameters and run it okay point 9 so those were from a red flower and we're going to interpret this point 9 as meaning the computer is thinking this is very much so likely a red flower so the ha good job let's try some different measurements okay point 9 8 for these but these measurements were from a blue flower so it's not looking so good for our computer let's try one more ok it seems to like high values here 0.98 so these measurements were from another blue flower but it's still telling us red so that's not too good but it makes sense the connections in the neural network are completely random right now so the responses we're going to get have no relation to the measurements at all so how would we even expect the computer to know which measurements go to which flour type we shouldn't expect it to them let's actually hear a few more predictions from our computer just for fun hi blue looks like red looks like blue ok so those were some pretty random guesses and also pretty musical guesses I like the singing it makes sense you know our computer has no idea right now but we're going to teach it so in the next video we're going to take a step back look at something called a cost function and a bit of calculus and that will get us moving in the right direction to find the right set of parameters for our neural network the right w1 w2 and bias that will make it more accurate in its predictions instead of just singing these wrong answers as entertaining as that may be so subscribe to see the next video and like or dislike this one leave a comment any feedback I'd really love the feedback I've been getting it's super motivating for me I love you guys out there for that and I'll see you in the next video thanks againin the last video we got Python setup on our computer in this video we're going to write our neural network in Python and see some predictions so to get started let's open up Jupiter notebook to do so we need a terminal window so we'll hit command space type terminal hit return now let's activate our environment we set up in the previous video so to do so we'll type source activate and then the name of the environment mine was NN series hit return let's open up Jupiter notebook I'm going to create this notebook on my desktop and we'll go to new Python - let's call it NN now I'm going to use stuff from numpy so I'm going to say import numpy so we have access to that now let's define our neural network as a function so to do so we'll type d EF the name of the function and n it's going to take two measurements m1 and m2 it's going to weight them by two weights called w1 and w2 and it's going to add a bias number to that now let's calculate an intermediate value in our neural network called Z so Z is going to be m1 times w1 plus m2 times w2 plus the bias and our neural network outputs number is between 0 and 1 and we know what's grade for that sigmoid so let's return sigmoid of z it returned a few times and before we define this let's define sigmoid itself it's not a built-in function so let's BEF sigmoid it takes one input X and it returns 1 over 1 plus numpy dot exponential of negative x so let's shift return to find that we have our neural network function to run it we need some data and we also need some weights we need some values for W 1 W 2 and B and we also need some values for M 1 and M 2 so that when we plug them in that all these get replaced with actual numbers so they can turn into something like 2 times 3 plus 3 times 4 plus negative 7 and then you can simplify that take sigmoid of it and you'll get out a number so we need some values for all these inputs to our neural network well for the waist it's kind of easy we can start with random connections so to do so we'll use a built in thing from numpy called R and n it's a function so let's type num PI dot random dot R and and and I can run this repeatedly by holding ctrl and tapping return and you see we get numbers that are all pretty close to 0 they're negative and positive and this is sampling from a normal distribution so we have some random numbers for our connections let's actually assign them into variables so W 1 is equal to a random number W 2 is equal to a random number and our bias is a random number to run that let's take a look at them W 1 is 1 point 4 3 2 is minus point 4 1 and the bias is one point seven four etc etc ok so to run our neural network now and get a prediction we need our measurements from the farmer and our random parameters so let's type the name of the function and n we'll put in a measurement let's try 3 and 1.5 and our random parameters and run it okay point 9 so those were from a red flower and we're going to interpret this point 9 as meaning the computer is thinking this is very much so likely a red flower so the ha good job let's try some different measurements okay point 9 8 for these but these measurements were from a blue flower so it's not looking so good for our computer let's try one more ok it seems to like high values here 0.98 so these measurements were from another blue flower but it's still telling us red so that's not too good but it makes sense the connections in the neural network are completely random right now so the responses we're going to get have no relation to the measurements at all so how would we even expect the computer to know which measurements go to which flour type we shouldn't expect it to them let's actually hear a few more predictions from our computer just for fun hi blue looks like red looks like blue ok so those were some pretty random guesses and also pretty musical guesses I like the singing it makes sense you know our computer has no idea right now but we're going to teach it so in the next video we're going to take a step back look at something called a cost function and a bit of calculus and that will get us moving in the right direction to find the right set of parameters for our neural network the right w1 w2 and bias that will make it more accurate in its predictions instead of just singing these wrong answers as entertaining as that may be so subscribe to see the next video and like or dislike this one leave a comment any feedback I'd really love the feedback I've been getting it's super motivating for me I love you guys out there for that and I'll see you in the next video thanks again\n"