A Practical Guide to Machine Learning with TensorFlow 2.0 & Keras Creating a 2D Image Model
Transcript from the "Creating a 2D Image Model" Lesson
>> Next, what we need to do is come up with the model we want to use. And the most easiest way to start is to use Keras' APIs sequentials which I will explain in a second. So, we need to first import a couple of things, so we can say from, tensorflow.keras.
[00:00:31] And then, we need to, it's actually a question right in the Keras, so we can say from tensorflow.keras import Sequential. So Sequential is the class which will describe the type of the models and why it's called sequential? Because our layers will be edit sequentially, alternatively you can have different models.
[00:01:03] Recurrent neural networks will require almost like a recurrence into itself. So we're not gonna go there yet, so we're just gonna use sequential models, and now we need to also import layers. So from TensorFlow, [LAUGH] Okay, it's easier to just type without have to completion. So Keras and I believe it's called layers, we can import a particular class called Dense and Flattened.
[00:01:42] So Dense, that's our fully connected layer, meaning that with the Dense layer, if you have, for instance, two layers right? And one of them is Dense, pretty much all the neurons from the previous layers will be connected to all the neurons of this layer. And Flatten, we need it because we want to simply have our two dimensional image.
[00:02:12] Remember, we have 28 by 28 pixels right now, we want to actually flatten it out into one dimensional array. Because that's gonna be our one dimensional array feed into the model, does it make sense or? So, basically from two dimensional image, we are simply taking the second row and truncating.
[00:02:35] Or editing it to the first 28 pixels we got from the first row and that's it. So we simply creating this one long, specifically 784 pixels long, one array. And that's gonna be our input we will be providing to our neural network, fully connected neural network, all right.
[00:02:58] So, we import those things, so what we need to do next is to actually specify our model. So model =, and we can just say sequential, so we're creating the instance of this sequential class. And what we can do we can say model, let's use add functions, so now we adding layers to our model.
[00:03:24] When I just said model sequential, it's just clarified what type of model I will be having. But nothing about how it's actually organized inside, so by using model.add for instance, just saying I want to first, Flatten. And yes, I need to provide input_shape. Oops, shape=, and here it will be adjust the resolution of my original image, so 28 by 28 pixels.
[00:03:59] And the result of this operation, if I, for instance, just execute this right now and do something like model.summary. It'll tell me what exactly happening here, so a sequential model, right? In this model, I have only one layer, a layer called flatten. And in the input, it will take 28 by 28, but as the output, it will return simply one array with 784 pixels.
[00:04:35] And the thing is we're not using any additional parameters, in this layer we don't have any weights or biases. It just doing the flattening of the two dimensional array, that's it what's happening here. Right now, we're not working with data, I'm just specifying the model and input_shape is telling this layer what to expect.
[00:05:01] What kinda input data, in which structure we should expect data to come into the model, doesn't make sense?
>> Yeah, it does.
>> Okay, and the thing is the next layer's, for instance, the one we're gonna be creating. It doesn't even need to know what the previous layer will be printing out, because it will do automatically behind the scenes for you.
[00:05:26] So with the sequential model, when we print out the summary. You'll see that we just simply did the transformation of this original image, 28 by 28 pixels. But the thing is we haven't provided even the images yet, we're just saying that that's the input you should expect. Some just images 28 by 28, two dimensional images, but our first flattened layer will simply produce this one dimensional array.
[00:05:52] If you multiply 28 by 28, 784, that's exactly how many elements you will have. And the thing is we can add more layers, for instance, the next one, let's say is gonna be fully connected neural networks. So we can, say, use Dense and to the Dense we then provide how many neurons we want to have in this particular layer.
[00:06:16] So, for Dense, let's say we want, I don't know, 256, so, what that means and by the way, we can also specify additional parameters. So, units, that's exactly how many neurons you want to have, right? So I can exclusively instead of specifying 256 here, I can say that's the number of units or neurons I want to have in my layer.
[00:06:49] And there are additional parameters you can specify, for instance, activation function. So, activation function, remember I said that when you get all of those inputs. Multiplied them by the weights and sum that to the result, then that's the argument to your function. And for instance, you can say that activation function should be sigmoid.
[00:07:10] And that will simply flatten the distribution to something between 0 and 1.