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.MaxPooling2D(),
   layers.Conv2D(32, 3, padding='same', activation='relu'),
   layers.MaxPooling2D(),
   layers.Conv2D(64, 3, padding='same', activation='relu'),
   layers.MaxPooling2D(),
   layers.Flatten(),
   layers.Dense(128, activation='relu'),
   layers.Dense(num_classes)
])

Code credit: https://www.tensorflow.org/tutorials/images/classification

Output

Sequential model is being created

Explanation

  • 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.

Updated on: 20-Feb-2021

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements