Dropout and Flatten

In this section, we'll actually construct the neural network model and use Dropout and Flatten in order to create a complete neural network.

We'll start off by using the functional Keras model to actually assemble neural networks, looking at the input and layer stacks in order to assemble a neural network end to end. Then, we'll explain why we have Dropout and Flatten, and what effect they have on your model. Finally, we'll show a model summary: This is a way that you can visualize the total number of parameters and layers in a machine learning model.

Here, we're using what is known as the functional model of Keras. You can think of a neural network as a series of layers, with each one of those layers being defined by a function. The function passes a set of parameters to configure the layer, and then you hand it, as a parameter, to the previous layer in your network to chain them all together. This tiny block of code, as shown in the following screenshot, is actually a complete neural network:

Functional model of Keras

We start with an input layer that's shaped in the same way as one of our input samples. In our case, we have picked one of our training images, which we know from our prior lesson has the dimensions of 28x28 pixels. Now, we pass this through a stack. A dense layer is followed by dropout_1, followed by a dense layer followed by dropout_2, which we ultimately turn into softmax activation to turn it over to the output layer. Then, we combine these together as inputs and outputs into our model. Then, we print summary, which will look like this:

Model summary output

So, you can see from this that the parameters are passed initially to the layers, and then the layers themselves are passed to form a chain. So, what about these Dropout and Flatten layers? The Dropout parameter is essentially a trick. When we set the Dropout parameter (and here, it's 0.1) what we're telling the neural network to do is randomly disconnect 10% of the activation in each training cycle. What this does is it gets the neural network to learn to generalize; this is true learning, rather than simply memorizing the input data. The Flatten layer deals with the dimensions. Because we have a two-dimensional 28x28 pixel input image, we use Flatten to turn this into a long, single-dimensional string of numbers for 784. This gets fed to the output softmax layer.

Printing out the summary of the model is a great way to figure out the size and dimension of your parameters. This ends up being one of the trickier parts of using Keras, such as when you have a set of input samples—in our case, the 28x28 images—and you need to turn them into a single array of ten possible output values by the time you get to softmax. You can see how the shape changes as we pass it through each one of the layers. Then finally, Flatten turns it down to a single dimension for each sample, which then gets turned into a single dimension with ten possible values for the output.

All right, now it's time to run the model. Now that we understand how to put a model together, including the Dropout and Flatten layers, we'll move on to solvers, which are what we use to actually execute a machine learning model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.107.193