Keras Model Sequential API VS Functional API

Rahmat Faisal
Analytics Vidhya
Published in
3 min readOct 17, 2020

Keras Deep Learning library helps to develop the neural network models fast and easy. There are two ways to build a model in Keras — Sequential and Functional. Let me explain.

Photo by Kevin Ku on Unsplash

Sequential API

Sequential API allows you to create models layer-by-layer by stacking them. It is limited in that it does not allow you to create models that share layers or have multiple inputs or outputs.

Example Code:

Result

And now lets plot your model using keras utils.

Keras Plot Model

As you can see, Sequential API create model and stacking them together layer by layer. Sequential API is very easy to create deep learning model in most of case.

Functional API

Keras functional API provides a more flexibility as you can easily defines models where layers connect to more than just the previous and next layers, and you can connect layers to any other layers. As a result, you can create complex network such as Residual Network.

Example Code:

Result

Plot functional model using keras utils.

Keras Plot Model

As you can see, Functional API let us to make layers more flexible.

Model Subclassing

If you are familiar with Object Oriented Programming (OOP), then this method is the most efficient way. By subclassing the Model class: in that case, you should define your layers in __init__ and you should implement the model's forward pass in call.

Example Code :

Model Subclassing Example

Conclusion

Sequential API

PROS :

  1. Basic, and simple to use.
  2. allows you to create models layer-layer by stacking them.

CONS :

  1. Dificult to make models with multi-input, multi-output, or shared-layers. Not flexible for network that need Merge Layers, Concatenate Layers, Add Layers.

Functional API

PROS :

  1. Flexible model architecture (each layer can be connected in a pairwise fashion).
  2. Can create complex network such as Residual Network.

CONS :

  1. Needs a Standalone “Input” layer at the top to specify input shape.

Model Subclassing

PROS:

  1. More Flexible than Functional or Sequential.
  2. Can create multiple layer blocks.

CONS:

  1. Familiar with OOP.

THANK YOU

--

--