‫Now, let's create the structure of our first artificial neural network model before starting.
‫Let's just set random seed 242 using these two statements.
‫Random seed is used to replicate the same result every time.
‫You can use any number instead of 42 if you use that number in future when you are running the same
‫code.
‫You will get the same output as we have discussed in the theory.
‫There are multiple occasions where our neural network generates random number, such as assigning the
‫initial weights using random seed will help you to reproduce the same result.
‫Using the same initial weights every time. Let's just on this
‫So for our problem, we have observations in the form of 28 into 28 pixels.
‫Observations are in the form of 2D array.
‫And as an output, we want ten categories.
‫These categories are exclusive.
‫That means a single image can either be a t shirt or a top or a boot.
‫This is what we are planning to do.
‫We are first converting our 2d observations into a flat 1d observations.
‫So instead of a 2d array of 28 into 28 pixel.
‫We want 784 pixel in our input layer.
‫Then we are going to create two hidden layers.
‫The activation function, which we are going to use for hidden layers, will be relu, as discussed in
‫the theory lecture.
‫We always prefer RELU for classification models.
‫And in the output since this 10 categories are exclusive.
‫and this is a classification model.
‫We will be using softmax activation.
‫We have already discussed this activation types in our theory lecture.
‫That's why we are not going to discuss it here.
‫Now let's start creating this neural network using sequential api's of keras.
‫First, we will need to create an model object.
‫So our object variable name is model.
‫And we are just creating it using this function.
‫Keras.models.sequential.
‫In this sequential object, we can add different layers.
‫We will start with our input layer.
‫We'll move on to hidden layer one, then to hidden layer two.
‫And then to the output layer.
‫So first, for the input layer, we can write like this model dot add.
‫And then keras dot layers.
‫And then since we want to convert this 2d array of 28 into 28 pixels to 784 pixel in a single
‫Array, we are using flatten.
‫And then we need to provide the input shape of over X variables, since our X variable is a 2d
‫Array of 28 into 28 pixel.
‫We are using input shape equal to then we are providing a list of two variables that is 28
‫Comma 28.
‫Then our second layer is the hidden layer.
‫So in the next step, we are adding another layer that is model dot add then keras dot layer.
‫And since this is a dense layer, will write dense.
‫And here we need to mention the number of neurons we want in this layer.
‫So in hidden layer one, we need 300 neurons.
‫That's why we are writing 300.
‫And then we want RELU activation function.
‫That's why we are writing activation equal to Relu in the next step.
‫We want another hidden layer.
‫So we are following the same process.
‫That is, we are writing model.add .
‫And in bracket we are writing keras dot layer dot dense.
‫And in this layer we want hundred neurons.
‫That's why we are writing.
‫Hundred and then activation equal to Relu since this is also a hidden layer.
‫We want activation function to be relu. in the next output layer.
‫We want 10 different categories.
‫That's why we have to add 10 neurons.
‫Into this layer.
‫And since the classes are exclusive.
‫That's why we have to use softmax activation.
‫So we'll write model dot add.
‫And then keras dot layers, dot dense.
‫And then the number of neurons, which is 10 and activation, equal to softmax.
‫I hope you remember what relu and Softmax are. relu is zero for all the negative numbers and equal to
‫the input for all the positive inputs.
‫Whereas softmax equates the sum of all the class probability to one
‫in case if you want any additional hidden layer , you can always add additional layer between any of
‫these layers in the later part of the course.
‫We will see how to choose the number of neurons in each layer.
‫Let's run this after creating this model structure.
‫You can look at it using summary method.
‫So if you write your object name, that is model and if you write dot summary.
‫The model summary method displays all the model layers, including each layer's
‫Names its output shape and the number of parameters.
‫So these are the layer names.
‫Second is the output shape.
‫This the number of output.
‫And this is the beth size of the input.
‫Since we are passing all our data.
‫That's why this is none.
‫None means no limit on input data.
‫And next is the number of trainable parameters, since our input have 784 variables and we are passing
‫each of these variables into 300 different neurons.
‫We have individual weights for each of these linkages.
‫So total number of weights is 784 into 300 plus
‫There are other 300 bias variables that are associated with each of these neurons.
‫So 784 into 300 plus 300 will give you this number.
‫Our neural network is trying to optimize this many parameters for this layer.
‫Similarly, these are the trainable parameters for this layer.
‫Again, this will be three hundred into 100.
‫There are three hundred into hundred linkages between these two layers.
‫And each of that linkage will have associated weights and each of the neuron in this layer.
‫That is, hundred neurons have hundred different Bias's values.
‫So 300 into 100 plus 100.
‫So thirty thousand one hundred trainable parameters are associated with this layer.
‫Similarly, 1010 trainable parameters are associated with this layer.
‫So at the bottom, you get the summary of total number of trainable parameters.
‫In this neural network.
‫So our neural network will try to optimize this many parameters to get the best result.
‫Now, if you want to look at our neural network, you can do
‫that using pydot.
‫So you have to import pydot.
‫And if pydot is not installed in your system, you can install it using PIP space, install space pydot
‫or conda Space, install space pydot in your command prompt.
‫So if you just write keras.utilities, dot plot, underscore model and then give your object name.
‫And if you run this, you will get the structure of your neural network.
‫So here we have input layer.
‫Then we are flightening the 2d array into a 1d array
‫So that's why we have a flatten layer.
‫And then we have 2 dense hidden layer
‫And we have an output layer which is giving us the class probabilities.
‫So after creating the structure of your neural network, you can also visualize this structure using
‫this
‫Command
‫As I said earlier, our model is trying to optimize weights and biases that are represented by this number
‫to get the output.
‫And if you remember in theory lecture, we have discussed that weights are assigned randomly for initialization
‫to get the information of those weights and biases.
‫There is a get underscored weight method that you can use to get information of those weights and biases.
‫So I can write my object name.
‫That is model and then the layer number for the second layer.
‫I can write layers.
‫And then one since the location of object two is one.
‫And then I can use get weights
‫Method.
‫I'm storing this information into two new variables, weights and biases.
‫So if I just output the weights, you can see this are the randomly generated weights
‫Are 784 into 300 such weights in this layer.
‫So if you just view the shape, you can see that.
‫There are 784 rows and 300 columns in our weights these all weights are randomly assigned for
‫Initialization
‫Similarly, we can also look at the biases values. Biases are initialized as zero.
‫And if you just check the shape of biases, this should be 300.
‫You can see that there are 300 Bias's. In the next video.
‫We will compile and train our model.
‫Thank you.