...

/

Keras Sequential API

Keras Sequential API

Learn to create DL models using the Sequential API of Keras.

Keras offers different ways to create DL models. Let’s focus on the sequential Keras model.

Building DL models

The different ways to build DL models in Keras include:

  • The Sequential model class: This allows us to define a DL model in a single line of code.

  • The Functional interface: This can create complicated model architectures.

  • The Model subclass: This uses object-oriented concepts to allow us to reuse models.

The Sequential model class

We use the Sequential model class to create a DL model consisting of a sequence of layers arranged one after the other. When each model layer has only one input tensor and one output tensor, we use the Sequential model. The following lines import the Sequential model.

import tensorflow as tf
from tensorflow.keras import Sequential
Importing the Sequential model from the Keras library

It’s quite easy to stack various neural network layers using the Sequential model once we import it. For instance, we can build a neural network with the following layers.

The following code imports the Sequential model and builds the above-mentioned network architecture.

Press + to interact
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, Flatten, Dense
model = Sequential([
Input(shape=(32,32,3)),
Conv2D(filters=10, kernel_size=(7,7), padding="same", activation="relu"),
MaxPool2D(pool_size=(2,2)),
Conv2D(filters=16, kernel_size=(7,7), padding="same", activation="relu"),
MaxPool2D(pool_size=(2, 2)),
Flatten(),
Dense(units=100, activation="relu"),
Dense(units=10, activation="softmax")
])
print (model.summary())
  • Line 5: We use the Sequential model constructor to pass an array of the network layers.

  • Line 16: We observe the overall model architecture by printing the model.summary().

We explain the role of each layer, their output tensor shapes, and the number of learnable parameters next.

Computing the number of learnable parameters

We analyze each network layer to explain their output tensor sizes and the number of learnable parameters as follows:

  • Line 6: This creates an Input layer that has an input of size (None, 32, 32, 3) (RGB images). A None dimension implies that it can be any number. This dimension doesn’t change the size of the network. Therefore, we can use this model to infer any number of samples (batch size) of the input during the testing of our trained model.

  • Line 7: This creates a two-dimensional convolutional layer, Conv2D, that uses 10 convolutional filters of size (7, 7) to have an output tensor of the shape (None, 32, 32, 10). We compute the number of trainable parameters for this layer by using the formula: [(filter width×filter height×channels)+bias term]×number of filters.[ ( \text{filter width} \times \text{filter height} \times \text{channels} ) + \text{bias term} ] \times \text{number of filters}. ...