Keras is an open-source neural network library written in python. It allows to easily build, train, evaluate and execute all sorts of neural network.

There are two ways you can use Keras to build deep learning models:

  • sequential API,
  • functional API.

The sequential API allows you to create models layer-by-layer for most problems. However, the is one important limitation — it does not allow you to create models that share layers or have multiple inputs or outputs.

The functional API in Keras is an alternate way of creating models that offers a lot more flexibility, including creating more complex models.

After reading this article you will know:

  • what is the difference between the Sequential and Functional APIs,
  • how to build Convolutional Neural Network, and LSTM models using the functional API.

What is the difference between the Keras Sequential API and Functional API

Let’s discuss the main differencies between the Keras Sequential API and Functional API

Keras Sequential API

The Sequential model API allows to create deep learning models where an instance of the Sequential class is created and model layers are created and added to it.

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation=’relu’),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=’softmax’)
])

Althought, in most cases it is very useful to develop deep learning models it has some limitations, e.g. it is difficult to to define models that may have multiple different input sources or models that re-use layers.

Keras Functional API

The Keras functional API is more flexible in terms of defining models.

Keras Functional API allows to define multiple input or output models as well as models that share layers.

Unlike the Sequential model, you must create and define a standalone Input layer that specifies the shape of input data.

The input layer takes a shape argument that is a tuple that indicates the dimensionality of the input data

The layers in the model are connected pairwise. This is done by specifying where the input comes from when defining each new layer.

After creating all of your model layers and connecting them together, you must define the model

#functional-api #sequential-api #deep-learning #tensorflow #keras

How to Use Functional Keras API for Deep Learning
24.65 GEEK