What is up guys, it's Adam back with another video and today we will be building a simple neural network in Python
But before we get started
I'd like to give a shout out to at I am Trask for writing the code used in this video
I'll give a link in the description to his blog post on neural nets without further ado. Let's get started
Alright guys, so I am currently an Adam text editor and we are going to start our Python file by importing numpy as MP
Next we're going to define our sigmoid function which in short if you don't understand what that is
It'll basically make all of our weights into one scale that goes from 0 to 1
all we are really doing is implementing the math that you see on screen right now, so
This function will have two parameters X and derivative
which will be set by default as false now if
Drivel to true we are going to return x times 1
Minus X and then outside of that if statement we will return 1 over 1 plus NP
exp for exponent 2 negative X
Now that our sigmoid function is defined. Let's write in the X and y components
For our neural net to train on so first we will set X as equal to NP or a open brackets
0 0 1 0
1 1
101 and
finally 1 1 1 so this is just going to be our input data and
now for the corresponding Y values y equals NPR a
0 0 1 1 dot T capital T that is for transpose so that we can use matrix
multiplication
So just to recap our neural net is trying to predict what the value of y is based on X
So if X is equal to 0 0 1 as it is in the first training sample
the output should be 0 because that is what the course why value for the x-component is
Next we are going to initialize a seed value with NP random
This will mean that the neural net will initialize the same random value every time
This is a good practice to always use and it will help a lot with debugging
Now we are going to make our synapse which if you know anything about neurons in our brain
It's something that connects two neurons to each other since we only have one hidden layer
We are only connecting the input to the output of our network
the input being our three x values and the output being our 1 Y value so
We will set a variable called sin 0 equal to 2 times MP dot random
3 comma 1
minus 1
This is for the 3 input X values and the 1 Y value
All right
so finally we are going to start training our network to do this start a for loop and iterate through it as many times as
You like a good number to start with for now will be?
10,000 if
This number is too low the network won't be able to update the weights enough for them to make the neural network work
Well, if it is too high, we will over fit our data making the neural net not work. Well with new data
Ok, so in our for loop layer 0 or the input layer is set to X and layer 1 or the output layer is set
to the sigmoid of NP dot dot which is for matrix multiplication l 0 sin 0
So basically we are multiplying the input layer by our synapse and setting it in a value from 0 to 1
Now, let's set a variable called L
1 underscore error
Which will compare our L 1 value to the actual Y value as we iterate through our loop this variable should get smaller and smaller
As our neural net gets more accurate
Next let's set a variable called l1 Delta to the l1 error times the sigmoid of l1
With the deriv equals true so that it runs that if statement we defined earlier
This will help all neural net not become too overconfident in its answers
Now, let's make synapse zero two plus equal NP
dot layer zero dot transpose and l1 Delta
This line will update the weights of our network
Finally outside of our for loop. Let's print out layer one
So our neural net is now basically done. Let's run it and see what the results are
You can see our network is pretty accurate
It thinks the first two X values are probably zero and then the last two are probably one
Congratulations, you just built your first neural network
So if you didn't understand anything that's going on in this code. That's okay
There's so much theory behind a lot of these concepts and it takes time to fully
Understand everything if you want to continue working with neural nets and machine learning
Please comment down below and I will also do a video on how I'm learning machine learning personally
Also, once again, thank you to at ion traps for writing the original code for this video
I think it's a really good introduction to neural nets. You can check them out below
