How can Tensorflow be used to create a sequential model using Python?

A sequential model can be created using the ‘Sequential’ API that uses the ‘ layers.experimental.preprocessing.Rescaling’ method. The other layers are specified while created the model.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

print("Sequential model is being created")
num_classes = 5
model = Sequential([
   layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
   layers.Conv2D(16, 3, padding='same', activation='relu'),
   layers.Conv2D(32, 3, padding='same', activation='relu'),
   layers.Conv2D(64, 3, padding='same', activation='relu'),
   layers.Dense(128, activation='relu'),

Code credit:


Sequential model is being created


  • The model contains three convolution blocks and a max pool layer in each of them.
  • It also has a fully connected layer with 128 units on top of it.
  • This gets activated by a relu activation function.
  • This model isn't tuned for high accuracy.
  • A sequential model with three layers is created.