Ways to Write Models in Keras

Learn via video courses
Topics Covered

Overview

There are two main ways to write and define models in Keras: the Sequential model and the Functional API. The Sequential model is a simple, linear stack of layers that allows users to create simple models easily. The Functional API, conversely, is a more flexible, expressive way to define models that can have multiple inputs and outputs and be used to create more complex, non-linear models. Of course, both methods have advantages and limitations, and the right choice of approach depends on the specific requirements of the built model.

Introduction

Keras is a popular deep-learning framework that provides high-level APIs for building and training machine-learning models. In Keras, there are two main ways to define and create models: the Sequential model and the Functional API.

The Sequential model is a simple, linear stack of layers used for creating simple, feed-forward neural network models. To create a Sequential model, you can add layers to it one by one using the add() method. The layers added to a Sequential model are organized in a linear stack, with the output of one layer feeding as the input to the next layer.

The functional API is a more flexible and expressive way to define models in Keras. With the functional API, you can define models with multiple inputs and outputs, and you can use more advanced features such as shared layers and layer branching. In addition, the Functional API allows you to create complex, and non-linear models that cannot be easily defined using the Sequential model.

Sequential API

The Sequential API is one of the two main ways to define and write models in Keras, the other being the functional API. The Sequential API is a simple, linear stack of layers that allows users to create simple, feed-forward neural networks easily. It is called "Sequential" because it allows users to define their model as a sequence of layers, with the output of one layer as the input to the next layer. To define a Sequential model, users can create an instance of the Sequential class and then add layers to it using the add() method. The Sequential API is easy to use and understand but limited to creating simple, linear models.

structure-of-sequential-api

Implementation of Sequential API

This example creates a Sequential model and then adds three layers: an input layer, a hidden layer, and an output layer. The input layer has 100 units and specifies the input dimension of the model. The hidden layer has 64 units and uses the ReLU activation function. Finally, the output layer has 10 units and uses the softmax activation function. This model can then be compiled and trained using the standard Keras methods.

One advantage of using the sequential API in Keras is that it allows you to easily create models by stacking layers on each other. This can make it easier to quickly experiment with different model architectures and iterate on your designs. Additionally, the sequential API can make your code more concise and readable, enabling you to specify your model architecture in a single line of code.

Functional API

The functional API is the other main way to define and write models in Keras, the other being the Sequential API. It is a more flexible and expressive way to define models. It allows users to define models as directed acyclic graphs (DAGs) of layers, with the ability to specify multiple inputs and outputs and create complex, non-linear models. To define a model using the functional API, users can create instances of the Input and Layer classes and then use these to define the model's structure. The functional API is more flexible than the Sequential model but can also be more complex. Nevertheless, the functional API provides a powerful way to define and create Keras models.

structure-of-functional-api

Implementation of Functional API

This example creates the feed-forward neural network using the functional API. First, it defines the input layer with a shape of (100,), which specifies the input dimension of the model. It then defines the hidden layer with 64 units and the ReLU activation function, the output layer with 10 units, and the softmax activation function. Finally, it creates the model using the Model class, which can then be compiled and trained using the standard Keras methods.

One advantage of using the functional API in Keras is that it allows you to create more complex model architectures, such as models with multiple inputs and outputs or shared layers. This can be useful for creating advanced models that require a more flexible design. Additionally, the functional API can make it easier to work with layers with multiple outputs or create models with a complex internal structure. Again, this can be useful for creating more powerful and expressive models.

Model Subclassing

Model subclassing is a way of defining and creating models in TensorFlow and Keras by creating a subclass of the Model class and implementing the model's forward pass as a method in this subclass. This allows users to define their custom models more flexibly and more powerfully than the Sequential model or the functional API. For example, to define a model using subclassing, users can create a subclass of the Model class and then implement the model's forward pass in the subclass's call() method. This approach allows users to define custom models with multiple inputs and outputs, which can be used to create complex, non-linear models. Model subclassing provides a powerful way to define and create custom models in TensorFlow and Keras.

structure-of-model-subclassing

Implementation of Model Subclassing

This example defines the MyModel class, which extends the Model class and implements the model's forward pass in the call() method. It defines two dense layers, one with 64 units and the ReLU activation function and the other with 10 units and the softmax activation function. It then creates an instance of the MyModel class, which can be compiled and trained using the standard Keras methods.

One advantage of using model subclassing in Keras is that it allows you to define your custom layers, which can be useful for implementing new types of layers unavailable in the Keras core library. This can be especially useful for research and experimentation, as it allows you to create and incorporate new layer architectures into your models. Also, model subclassing can make working with complex model architectures that require a more customized design easier. This can be useful for creating models with a unique structure or requiring custom behavior.

Implementation of MobileNet via Functional API

Model Summary

Conclusion

In this article, we will see many ways to create the model in Keras.

  • We understood where Sequential API, Functional API, or Model Subclassing could be used.
  • We also demonstrated three different ways to create a model using Keras.
  • We also implemented the MobileNet model using Functional API.
  • We also discussed in which situation you can use Sequential API, Functional API, or Model Subclassing.