Let’s continue from day 14 which we explained the 3 Keras API types and compare them
Understanding Sequential vs. Functional API in Keras with a Simple Example
When building neural networks in Keras, there are two main ways to define models: the Sequential API and the Functional API. In this post, we’ll explore the differences between these two approaches using a simple mathematical example.
Sequential API
The Sequential API in Keras is a linear stack of layers. It’s easy to use but limited to single-input, single-output stacks of layers. Here’s a simple example to illustrate how it works.
Objective:
- Multiply the input $x$ by 2.
- Add 3 to the result.
Let’s implement this using the Sequential API:
from keras.models import Sequential
from keras.layers import Lambda
# Define a simple sequential model
model = Sequential()
model.add(Lambda(lambda x: 2 * x, input_shape=(1,)))
model.add(Lambda(lambda x: x + 3))
model.summary()
Functional API
The Functional API in Keras is more flexible and allows for the creation of complex models with multiple inputs and outputs. We’ll use the same mathematical operations to illustrate how it works.
Objective:
- Multiply the input $x$ by 2.
- Add 3 to the result.
Mathematical Operations:
$y_1 = 2 \cdot x$
$y_2 = y_1 + 3$
Let’s implement this using the Functional API:
from keras.models import Model
from keras.layers import Input, Lambda
# Define the input
input_ = Input(shape=(1,))
# Define the operations
y1 = Lambda(lambda x: 2 * x)(input_)
y2 = Lambda(lambda x: x + 3)(y1)
# Create the model
model = Model(inputs=input_, outputs=y2)
model.summary()
Summary
Both the Sequential and Functional APIs can achieve the same result in this simple example. However, the Sequential API is limited to linear stacks of layers, while the Functional API allows for more complex architectures.
Let’s even compare them in an image