Keras Modeling | Sequential vs Functional API

The Keras deep learning library helps to develop the neural network models fast and easy. There are two ways to create Keras model such as sequential and functional.

The sequential API develop the model layer-by-layer like a linear stack of layers. It seems to be very easy to build a network. But the sequential API has few limitations that it doesn’t allow us to build models that share layers or have multiple inputs or outputs.

The functional API is an alternative way to build a neural network. It provides more flexibility to develop a very complex network with multiple inputs or outputs as well as a model that can share layers.

This tutorial has explained both methods to build deep learning model with a demonstration in Python. Here we will use iris flowers dataset for example. The iris dataset is standard dataset which used to learn and practise the Machine Learning and Deep Learning models.

sequential API 

sequential API is very easy to create deep learning model in most of the case. The implementation of the model is straightforward. Let’s see an example.

Load a Data

Scikit-Learn library provides the iris dataset. Let’s load the dataset as follows:

In [1]:
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target

Here, the data classified into 3 classes such as Setosa, Versicolour, and Virginica. Whenever the model is built for a multi-class classification problem, it is best to practise to apply one-hot encoding to the target label.

Keras API provides the utility function to_categorical() for one-hot encoding.

In [2]:
from keras.utils import to_categorical
trainY = to_categorical(y)

Let’s define a sequential model and run it on data.

In [3]:
from keras.models import Sequential
from keras.layers import Dense

def define_model():
    model = Sequential()
    model.add(Dense(50, input_dim=4, activation='relu'))
    model.add(Dense(12, activation='relu'))
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='categorical_crossentropy',optimizer='adam', metrics=['accuracy'])
    return model

model = define_model()
model.fit(X, trainY, epochs=30, batch_size=20)

_, accuracy = model.evaluate(X, trainY)
print('Accuracy: %.2f' % (accuracy*100))

Out[3]:
Accuracy: 97.33

.     .     .

Functional API

Keras functional API provides a more flexible way to build a deep learning model. It allows sharing layers and also allows to define multiple input and outputs to model.

Unlike the sequential API, we need to define the standalone Input layer that specifies the shape of input data. we also need to define the model using Keras Model class which requires the input and output layers.

Let’s look at how to create a model using Keras functional API.

In [4]:
from keras.models import Model
from keras.layers import Input, Dense

def define_model_by_functional_api():
    input1 = Input(shape=(4,))
    hidden1 = Dense(50, activation='relu')(input1)
    hidden2 = Dense(12, activation='relu')(hidden1)
    output = Dense(3, activation='softmax')(hidden2)
    model = Model(inputs=input1, outputs=output)
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = define_model_by_functional_api()
model.fit(X, trainY, epochs=30, batch_size=20)

_, accuracy = model.evaluate(X, trainY)
print('Accuracy: %.2f' % (accuracy*100))

Out[4]:
Accuracy: 97.33

.     .     .

Leave a Reply

Your email address will not be published. Required fields are marked *

Computer Vision Tutorials

Prepare COCO dataset of a specific subset of classes for semantic image segmentation

YOLOV4: Train a yolov4-tiny on the custom dataset using google colab.

Video classification techniques with Deep Learning

Keras ImageDataGenerator with flow_from_dataframe()

Keras ImageDataGenerator with flow_from_directory()

Keras ImageDataGenerator with flow()

Keras ImageDataGenerator

Keras fit, fit_generator, train_on_batch

Save and Load Keras Model

Convolutional Neural Networks (CNN) with Keras in Python

Transfer Learning for Image Recognition Using Pre-Trained Models

An introduction to Transfer Learning

Keras ImageDataGenerator and Data Augmentation

Introduction to Computer Vision

Study Machine Learning