Introduction to Convolutional Neural Networks II Video Lecture Transcript This transcript was automatically generated, so there may be discrepancies between the video and the text. Hi, everybody. Welcome back in this video. We're gonna finish up introduction to Convolutional neural networks. This is part two of a two part series on Convolutional neural networks. And we're gonna see how to build such a network in Carris. So if you're just joining us, you may want to go back up and run this one code chunk uh from the part one where we're just importing things like pandas, NPA and Matt plot lib. Uh So now we're gonna go through and show you how you can build a convolutional neural network, which we talked about in the previous video as being these um you know, sliding sliding grids around taking smaller weighted sums doing that many times and then maybe pulling those hidden layers together. Um We talked about that in the theoretical foundations of the convolutional neural network and the previous video slash notebook or same notebook, previous video. So I'm gonna go ahead and load in my data and then preproce it. And by that, I mean, I'm just gonna make a t the train test split is already made for me and I'm gonna scale it by the maximum value of the grid So that's 2 55 for this data set. Let's remind ourselves of the shape of the data. For instance, in the training set, I have 60,000 observations of a 28 by 28 grid. So remember each of these represents one image in the M N I S T hand drawn uh digit database. So what we are gonna have to do is remember in feed forward networks, we reshaped our data so that they were a single column. So we would have had 60,000 by whatever 28 times 28 is in this notebook, we're going to reshape them so that we have uh 60,000 observations for the training set of a 28 by 28 grid. And each of those grid points contains a single color value meaning a gray scale all value. So convolutional neural networks and Kris are set up to also be able to take in R GB values. In which case, you'd have something like a 60,000 observations of a 28 by 28 grid where each grid point contains three values. But for us, it's gray scale. So it'll be one. So we have to reshape it so that it is 60,028 28 that's fine. But now we have to add an extra dimension which may seem silly. But in order for the car to work, we have to put a one here. So now we have to take our three dimensional arrays and turn them into the four dimensional arrays for the convolutional layers in the car package. Uh The test set, we have to do the same thing, but instead of 60,000, it's just 10,000. OK. Now, we could look if we wanted to and see what the shape is and it should be what we specified here. I'm just making a validation set. So remember when we train neural networks, they can take a long time. So instead of doing cross validation, we'll use a validation set for comparing models here. I'm just importing all the stuff I need from Carris and we'll use each of these along the way. So the first step in making a convolutional neural network is making the empty model object, which is models dot sequential. We saw that in the feed forward version of Caris. Now we're going to add a convolutional layer. So in order to do that, you do models dot add uh layers dot C O N V capital C two D, ... then we have to put in that our layer is a, we want a three by three sliding filter. Again, you could choose something other than three by three. So sometimes um people will choose five by five. But for this example, we're going with three by three. That's pretty common. We want our activation function to be a ray. ... Um What else do we need? Oh, I forgot at the beginning that we want our first hidden layer. So remember in the earlier video, we talked about giving the hidden layers depth. So we're gonna wanna do this filtering process capital D times for us, we're going to choose uh 32. Uh There's no specific reason for choosing 32 over any other number. Uh I just decided to choose 32. OK. Nothing special about the number 32. And then finally, we have to give our input size our input shape uh which is equal to 28 by 28. And then I think we just put a comma and if we, if that's wrong, then we'll see later and I'll have to come back and fix it. OK? Now we're gonna add after our first convolutional layer, we'll add our first pooling layer and this is gonna be maximum pooling. Uh It's possible to implement other types of pooling layers and cars, but you'll have to check the documentation to see how uh so max pooling two D and I forgot the layers dot ... And then here we just have to put in the size. So remember in car, it's gonna know what the input shape is from the previous. We don't have to set the size of the pulling layer because it's the same as the hidden layer. Uh And so we just have to put in the size of the grid that we want. And so we're gonna just gonna use the most common, which is two and we want our number of strides to also be equal to two. ... Uh So what did we do? Wrong? ... Input zero of layer com is not ended in. ... OK. So let's fix this ... and hopefully that works. And if it's wrong, we'll see it again later. So I think I just need to add the one here. Um So, because it should interpret the 60,000 from the training set. OK. So now we can look uh at our summary of the model. So we can see here. Uh So below, I added another two, I didn't go over that because it's the same exact um syntax just without the input shape for the conv convolutional layer. Uh So we can see here our first layer of convolutional two D. Uh It's 26 by 26 because that's what you get uh using the formula above for B minus F plus one and L minus F plus one. The maximum pooling then shrinks that by half. Uh And then the next convolutional layer is the same uh same size and then the maximum pulling shrinks that. And so you can see that now through this, we'll have 18,816 trainable parameters. Meaning those are the weights on the um on the, on the sums and the filters. OK. So now I I'm going to, this is gonna be my, you know, convolutional and pulling stuff. Now, I'm just going to add in um the dense layer before I can add the dense layer, I need to add a flattener layer. So what flatten does is it's going to take my data and then flatten it into uh a single vector. So that way the the dense layer can take it in. Then I go ahead and using the syntax we developed in notebook number four, I add a dense layer of size 64 with a ray activation. That's my hidden layer that the, the last part of the convolutional neural network. And then I add the output layer with a softmax activation uh which is what we're using for multi class classification. So here's what my, what my end model looks like. And you can see why did we want to do maximum pooling or why did we wanna do pulling? If you look at the number of inputs into uh look at the number of weights in that dense layer, it's 102,464. So had we not done this pooling? This would be even more. Uh And in, in general, like the larger the number of parameters, uh the one the more difficult it is to fit and then two uh uh the more likely you are to over fit. OK. So now we're gonna try and compile the network that works fine. And now this will be the step to make sure we're gonna check whether or not I had the right input shapes. Uh We're gonna train the model, we're only gonna train it for 10 epics because convolutional neural networks are much slower to train than feed forward networks. So I'm gonna hit shift enter. I didn't go over the other inputs here because they're exactly the same as notebook number four. OK. So here we can see just how slow this process is. All right. So it took uh about 5 to 10 seconds just to train through one uh epic or epoch. Uh So what I'm gonna do is I'll pause the recording and I'll come back when this is done training. So you don't have to just sit here and watch it. All right. Hi. Welcome back. It's done training. It did take about a couple of minutes. Uh So that's a couple of minutes. I saved you. Uh So the history stuff is just the same. So we still get a dictionary uh and history dot history that to track the accuracy and the validation er for the training set and validation set as well as the loss function on those two sets. Here, I plot both of them. So here is the accuracy on both the training set and the validation set. And it does look like maybe we've started to reach that point where after maybe six or seven epochs, we're starting to overtrain on the training data over fit on the training data. But in practice, we probably want to go for a few more uh epics or epochs um before we are happy and then here is the loss uh we could go through. So remember earlier, we talked about how with the uh convolutional neural networks, it pays more attention to the interior than the exterior of the images unless we add padding. So here, I'm gonna demonstrate how to add padding to your original neural network. And the car will take care of adding enough padding to make sure that by the end uh the grid size um is uh I don't wanna say exactly the same, it won't be exactly the same, but it takes care of adding the padding for you. So you don't have to calculate in your head like how many um uh how many paddings to add, how many rows or columns of zeroes you need to add. Uh So this is, let me change this. This is from an earlier version of the notebook and where I only where I had um two con three convolutional layers and I only wanna have uh two just so it's consistent where the only thing I'm changing from the first model to this model is that I'm adding padding. OK. So let me go ahead and run this, then we can see the summary. And so you can see that now the output from a convolutional neural network convolutional layer one is uh the same shape. The grids are the same shape as the input grid which is 28 by 28. Uh We could have added another padding argument here if we had wanted to, but we didn't. Um And then everything else is the same uh as the rest of the notebook except for the, you know, the dimensions are changed because we have a different shaped grid. So we're gonna go through and train this once again. I will pause the video while it's training, so you don't have to watch it. Uh You can see it's just as slow as before. All right. All right. Welcome back. Uh I have un paused my recording. You can see that this network took even longer to train than the last one uh because of the extra weights from the larger grid. Um And we can see we can compare if that adding that padding helped our model perform any better by looking at the different uh validation sets and their accuracy and loss. Uh So we can see here that it does appear that um perhaps if we had kept training, we would get a slight improvement based upon uh the addition of padding to the neural network uh in comparison to what we had with the model without padding, but we might have to train for a little bit longer to know whether or not it's uh a noticeable improvement. OK. So once you're finally happy with whatever architecture you're choosing, so like you'll go through, try some different ones, compare them on validation sets or if you have the time across validation, you uh you know, can get predictions on the test that just like we did before with model dot predict. Uh As a quick note, I think I said this before, but we have only used these networks for gray scale images, but they can also be used on color images. The only difference between the gray scale and the color is instead of having a 28 by 28 by one grid, you'll have a 28 by 28 by three grid or sorry three array. Uh where the depths of 33 means that you're gonna have three values one for uh a red color spectrum, a green color spectrum and a blue color spectrum. OK. And here are some references where you can learn more about convolutional neural networks theoretically, it's this text that I think I keep pointing back to deep uh uh neural networks and deep learning. In particular, the chapter on convolutional neural networks is chapter eight. Uh And then here is where you can learn more about how to make convolutional neural networks in Python with Caris. And they show you some nice tips and tricks for um you know, for instance, how do we build these sorts of networks when I have a very small number of images to train on. So here we had 60,000 in practice, you'll have far fewer uh if you're working at home. Um So they go through some tips and tricks on how you can generate new images from old images to help train your network. OK. So that's it for this video. I and that's it for convolutional neural networks. I hope you enjoyed learning about these networks. I enjoy teaching you about them and I hope you have fun trying to fit your own. All right, have a great rest of your day. Bye.