Deep Learning Models using Keras Functional API

Sequential API and Functional APIs are the two primary ways in which a Deep Learning Model can be built in Keras. Squential API allows you to build a model step by step, layer by layer. However, it does not allow your to make a model which has multiple input or multiple output. This is something which the Keras Functional API can handle

Sequential Model in Keras:

Sequential Model

Model with Multiple Inputs and/or outputs

Functional Model

Functional APIs can handle such models and provide much more flexibility in building model or merging the outputs of multiple models into one model, and can handle complex models

Sequential Model Example: Code

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(10, input_dim=4, activation='relu'))
model.add(Dense(1,activation='sigmoid')

Functional Model

A Functional Model allows you to define specific multiple input or output models as well as models that share layers. More than that, it allows you to define ad hoc acyclic network graphs.

Models are defined by creating instances of layers and connecting them directly to each other in pairs, then defining a Model that specifies the layers to act as the input and output to the model.

Building a Functional Model

1. Defining Input

A standalone Input Layer needs to be defined which specifies the shape of input data. The shape arguement is a tuple that indicates the dimensionality of input data.

For one-dimensional input data such as Multi Layer Perceptron, the shape must explicitely leave room for the shape of the mini-batch size used when spliting the data while trainiang. For example (2,)

from keras.layers import Input
visible = Input(shape=(2,))

2. Connecting layer

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

from keras.layers import Input, Dense
visible = Input(shape=(2,))
hidden = Dense(2)(visible)

3. Creating the Model

“Model” Class from Keras - need to specify the inpit and output layers

from keras.models import Model
from keras.layers import Input, Dense
visible = Input(shape=(2,))
hidden = Dense(2)(visible)
model = Model(inputs=visible, outputs = hidden)

Sample Models - Complete MPL and a complete CNN

MLP

from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
visible = Input(shape=(10,))
hidden1 = Dense(10, activation='relu')(visible)
hidden2 = Dense(20, activation='relu')(hidden1)
hidden3 = Dense(10, activation='relu')(hidden2)
output = Dense(1, activation='sigmoid')(hidden3)
model = Model(inputs=visible, outputs=output)
# summarize layers
print(model.summary())

CNN

from keras.models import Model
from keras.layers import Input
from keras.layers import Dense
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
visible = Input(shape=(64,64,1))
conv1 = Conv2D(32, kernel_size=4, activation='relu')(visible)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
hidden1 = Dense(10, activation='relu')(pool2)
output = Dense(1, activation='sigmoid')(hidden1)
model = Model(inputs=visible, outputs=output)
# summarize layers
print(model.summary())

Other models with Multiple Input and Outputs are shared in a code on the Github Repo

This post is heavily influenced from the awesome blog by Jason Brownlee

Written on October 2, 2018
]