Create a Simple Neural Network in Python from Scratch - Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back so as you might remember last time we made a barely usable version of a perceptron it's what they look like that cuz I could get the outputs given three inputs now in this video we're gonna use a skeleton and basically make it into a more usable format and for that we'll use a Class A Class is basically the the object version in Python and it can store a bunch of functions and variables so again we'll start by importing non-time which is always needed you're working with neural networks and then we'll define a class class name is always written in camel case by the way and not snake case as usual item so a class consists of a bunch of functions and the first function is by definition almost always our initialize function you might wonder what it's underscore are well basically that's just a convention and these functions are called dunder methods when they have like - or data model method is basically a better name for them can look it up if you want to they always every function inside of a class takes in self as a standard variable and the self is a namespace in which you can solve variables that can later be used outside and inside of the neural network with different functions so in the initialization function will set the random seed to one and stuff like that it can be found in the previous video in which I tried to explain everything as thoroughly as possible then we'll use the self.namespace to store our synaptic weights and those are made like this and random and we're given a 3 by 1 matrix all right so now these are all values this is a 3 by 1 matrix with all values between minus 1 and 1 with a mean of 0 next up is our sigmoid function stake in itself as well and X and this returns our sigmoid function of X obviously so all right next up is the Sigma derivative so also take since of an X and returns the derivative of the six-month function okay now the next two functions train and think are basically the core of our object they do the most of the work so we'll start with train station itself our training inputs which will define later same with the training outputs and our training iterations now this is a core of training our neural network so for iteration in range training iterations which will be able to adjust ourselves output would be will be solved thing which is another function of our neural network that will design later so solve that think with our training inputs all right next up we have to define the error so the error equals the training outputs we gave our neural networks our neural network - the outputs produced these errors are needed for back propagation again if you want to know more about it check out my first view and then we'll define the adjustments which is dot product basically its matrix multiplication of our training inputs transposed and with our error times our soft segment Sigma derivative of the output right and then we'll make the adjustment so such as the synaptic weights according to the both to the weight of the fault basically that's all back propagation now so plus equals adjustments all right the next function is think these are just arbitrarily chosen names would they make a lot of sense to me but yeah you can choose them differently if you wanted so takes in self and inputs okay so yes since we're doing a dot product again and our inputs are basically integers and our synaptic weights are floats and you can't you cannot take a dot product of those who so we're gonna have to convert our inputs to floats which is another problem just use the s-type method float all right and then the output equals the sigmoid input times the synaptic weights alright and this returns the output okay so our class is done next up we'll have to make it a usable ya command line program and this is done like this you might have seen this already gets used quite a lot weight main I mean all right so first we'll initialize a neural network like this basically called like a function and let's do some recording so first the random synaptic weights and because of our self variable we can access functions and variables like this basically calling a method on the neural network class all right next up yeah we're going to take the same training data as in the previous video I'll just copy that all right and then we'll have to train it so again calling off calling the training function of the object and giving it the training inputs and the training iteration so you might remember we defined that here you can also use a custom input if you want to but we're not going to do that I'm not going to do that and then we'll print the synaptic weights after training weight all right next up we'll ask the user which is us to provide our custom inputs to test the neural network on these need to be converted back to a string by the way so input 1 B equals a string of inputs 2 and then C equals 3 of input tree all right so no situation would be input data to be a B and C and then print the output data it should be correct so we'll use it we'll use the tag function again think with inputs being a B and C all right so let's give that a go Oh synaptic weights and that should be okay so she remember or might remember the first input is 1 the output should be 1 and otherwise should be a 0 so I hope this works and yeah as close to one is we can get with 10,000 iterations my friend got up would you'll never get to a 1 because of yeah the properties of a sigmoid function but this is close enough so I yeah I was it guys thanks for watching and how the next video is probably going to be nothing with to do with neural networks but with JavaScript and node.js Express in MongoDB so we're going to make a mean stack app so if you want to see more of that please consider subscribing and shall like the video leave a comment and a like and I'll see you guys next
Info
Channel: Polycode
Views: 98,906
Rating: undefined out of 5
Keywords: python, neural, network, programming, coding, web, development, artificial, intelligence, tutorial, machine, learning
Id: Py4xvZx-A1E
Channel Id: undefined
Length: 11min 43sec (703 seconds)
Published: Tue Aug 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.