IKH

Sequential vs Functional API

As seen earlier, three kinds of the model building process. Among them, the most used and common is the Sequential and Functional API. They both are user friendly and allows us to work on simple and traditional use-cases.

In the next video, let’s quickly revise the Sequential API approach which you have used in the earlier modules.

Sequential API: Here layers are put in sequential order and the flow of data takes place between layers in one direction. It is the most common type of model to create simple neural network architectures where we have a linear stack of layers.

This is the best approach for a beginner as it is user-friendly and you can create models by simply plugging together building blocks. The only disadvantage of using the Sequential API is that it doesn’t allow us to build keras models with multiple inputs. It is limited to just 1 input tensor and 1 output tensor.

Using a sequential() function, you can build your model by passing a list of layers.

Sequential models can also be created incrementally using the add() method.

You may have already worked with some of the models that utilise Sequential approaches like VGGNet, LeNet and AleNet.

So now that you have understood how we implement the Sequential function for our simple models,let’s understand the next model building processes, i.e. through the use of functional APIs.

Functional API: This type of approach allows you to build neural network models with multiple inputs/outputs that also possess shared layers. It can do all the work which you could do with the sequential API and on top of it provides more control over how the layers should interact with each other.

Here layers are appended to each other by passing input to them. As seen in the video, x acted as the input variable to subsequent layers. So compared to the previous approach, here you have a decent degree of customisation which you can add to your model.

Functional API uses the concept of legos which once you have built, can be fit anywhere to shape your final model. A model might have many blocks in it to perform different operations on the built-in layers.  Imagine these blocks as legos that can be shaped/stacked in any manner to define the architecture of your model.

Once these blocks are built, they can be used any number of times, thus providing us with the benefit of reusability. Let’s take an interesting example to understand how we can build blocks and reuse them for defining our model architecture.

Imagine you want to create a CNN block that applies both the convolutional operation and Batch Normalisation. you also want to apply a ReLU activation function to the output of both the above operations.

Here is the code for implementing the block above.

Now, let’s say you want to create an inception block that utilises the earlier created CNN block multiple times and produces the output by merging the results from the CNN blocks.

Here is the code for implementing the block above.

Here,the inception_block uses two branches of the CNN_Block. The outputs coming from these two branches are then merged together by passing two instances of CNN_block as a list to the layers.concatenate layer.

This feature is called Shared layers, where you have reused the same block again inside our new block/lego. By sharing the information across two or more different inputs, shared layers allow you to train a model even on lesser data.

Now that have defined all the blocks that would be used for the model, you can go ahead and build the architecture for the same. Consider the model that you want to build is the following.

Here is the code for implementing the architecture above.

The summary of the model is as follows.

You can also plot the model as a graph using the following command.

To learn more about Functional API, read the guide.

Coming Up

In the next segment, you will learn about the third approach – Model Subclassing.

Report an error