Keras API functional and sequential

Table of Contents

  1. Introduction
  2. Sequential API
  3. Functional API

1. Introduction

  1. Model selection is an important part of data analysis and planning.
  2. Our main focus is to choose a specific model for a deep learning problem let’s start the session we already know models are one of the major components of Keras architecture.

We have two choices sequential and functional API in both cases we use core and some specialized layers like a convolution, pooling, recurrent network to create the solution for a specific problem.

2. sequential API

The core idea of sequential API is simply arranging the Keras layers in a specific order by adding new layers.

  1. We can use sequential API in case of single input and single output deep learning models in this specific option data flows from one layer to another in a given order until the data finally reaches the output layers
  2. The sequential API is not very flexible it does not allow the creation of models that share layers or have multiple input and outputs.
  3. A sequential model can be created by simply calling sequential API as specified in step 1 then we have added a dense layer of eight neurons mentioning the input shape of two and in the final layer we have added another dense layer with two neurons so we have three layers first the input layer then dense layer and finally another dense layer as out this flow diagram with tell the same thing so we have three layers first one is input layer then dense layer and finally another dense layer.

Sequential model in the Jupyternotebook :

  1. Firstly we will import sequential from Keras model then import Dense and activation layers from Keras.
  2. To plot our models we have to import plot_model module from Kerasutils 

Now we will define our model as sequential just calling this sequential API in the next line we are going to add a dense layer with eight nodes as this is the first layer we have to specify the input shape in this case the input dimension is 2 and we are using relu as activation function using this final line we are going to add a dense layer with two neurons.


Model. summary() gives details of the model has three layers, the first one is the default input layer then two dense layers in the first dense layer the input dimension is 2, and the output dimension is 8.

Counting Trainable parameters in model:

  1. we can calculate param using input-output and bias features the param is showing 24.
  2. 16 is from weights which we got multiplying input 2 with output 8 and 8 is from the biases as we had 8 nodes there will be also 8 biases so in total 16 plus 8 equals to 24 
  3. now in the final output layer, the input is 8 and the output will be 2 as we have defined two nodes in this second dense layer the total param will be 18 the 
  4. Total trainable parameter will be 42 == 24 +18 
  5. let’s generate the flow diagram first layer is the input layer with input and output dimension 2 now in the case of the first dense layer the input dimension is 2 and s the dense layer has 8 nodes the output dimension will be 8 in the case of the final layer the input dimension is 8 and output dimension is 2.

Convolution layers with sequential API:

  1. Now let’s look into a complex sequential model with convolution pooling followed by dense and flatten layers first we are going to import our required model layers and modules from Keras. 
  2. This model has an input layer consisting of images of size 28 by 28 with one channel a convolution layer with 32 filters with kernel size 5×5 a second conversation layer with the same 32 filters with kernel size 5×5 after that a max pool layer with the pool size 2 x2 are flattened layer finally our dense output with two nodes.
  3. Let’s execute the model summary to know about the overall model.
  1. We already know that the input shape is 28 by 28 with one channel the output shape of the first convolution layer is 24 x 24×32.
  2.  This 32 is coming from that number of filters in first layer we get this 24 subtracting the filter size 5 from the input dimension 28 so it will be 28 minus 5 and adding plus 1 equals to 24 
  3. Similarly for the second conversation layer the output shape will be 24 -5 plus 1 which means 20
  4. Now in max pool 2d layer we are using a pool size of 2 by 2 so the output shape will be half of the input so output will be 10 x10 x32
  5. Then this will send to a flattened layer that will flat in the input so the output will be a 3200 dimension vector This passed to the output dense layer of size 2.

2. Functional API 

Functional API is an alternative approach of creating more complex models it especially allows to define of multiple inputs or outputs model and also models that share layers


It’s working principle a little bit different first we have to specify an input layer and proceed with it this is an example of functional API in this model these two conversation layers are sharing the input layer again after max pool 2d and flatten layers they will be merged and continue as a single layer.

Functional API in Jupyter notebook:

  1. Firstly we import all the required modules models and layers from Keras.
  2. As input layer of 28 by 28 with the channel 1 then we have 2 branches both of them contains convolution max pooling and flatten layers this convolution layers share the same input layer this input_data part means conf 1 is connected to input data which is the input layer, in the same way, this call 1 is connected to pool1 and pool1 is connected to flat 1 and this is also organized in the same way so both conv1 and conv2 are sharing the same input_data.
  3. This concatenate layer will concatenate fatten layers from two different branches then we have defined two dense layers first one contains ten nodes and the final layer contains only one node and this will be treated as the output layer.
  4.  Then we are going to generate our model this model function will help us to define a functional model we have to mention the input and output layers finally we will plot our functional model this model starts with an input layer both these convolution layers are using this input layer for this convolution layer.
  5. The input shape is the same and the output shape will be 24x24x32 this 32 is coming from the number of filters from this faster convolution layer and we can get this output by subtracting this kernel size 5 from the input size I mean 28 minus 5 plus 1 so it will be 24.
  6. The next layer of the first branch was max-pooling 2d with the pool size 2 by 2 the shape of at the output will be half of the input so the output will be 12X12X32 and in the flatten layer this input will be flattened in the output we will get 4608 just multiplying 12X12X32.
  7. The second convolution layer number of a filter is 16 and kernel size is 7 so the output of this convolution layer will be 22 X 22 X 16 we can get this output just by subtracting this 7 from 28 plus 1 the next layer in that branch is max pooling 2d the output will be half of the input and this flattened layer will flatten the input shape so it will be 1936 now in this concatenate layer it will merge these two flatten layers so the output shape will be 6544 so it just added the inputs in the next dense layer.
  8. we have 10 nodes so the input will be the same shape as the output of concat layer and the output will be the dimension of 10 and in the output layer we have only one node so the output dimension will be 1 so in this specific example our convolution layers is using our input layer so this is a classic example of functional API.

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *