Keras Model Sequential API VS Functional API
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.
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:
And now lets plot your model using keras utils.
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:
Plot functional model using keras utils.
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 :
Conclusion
Sequential API
PROS :
- Basic, and simple to use.
- allows you to create models layer-layer by stacking them.
CONS :
- 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 :
- Flexible model architecture (each layer can be connected in a pairwise fashion).
- Can create complex network such as Residual Network.
CONS :
- Needs a Standalone “Input” layer at the top to specify input shape.
Model Subclassing
PROS:
- More Flexible than Functional or Sequential.
- Can create multiple layer blocks.
CONS:
- Familiar with OOP.