Machine Learning Overview

Sequential vs Functional Keras API Part 2 explanation _ Day 15

Keras API Example

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:

  1. Multiply the input $x$ by 2.
  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:

  1. Multiply the input $x$ by 2.
  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

Sequential API:
• Diagram: Shows a simple linear stack of layers. • Input Layer: Receives input features (e.g., x1, x2, x3).
• 1st Hidden Layer: Processes the input layer.
• 2nd Hidden Layer: Processes the output from the 1st hidden layer.
• Output Layer: Produces the final prediction (y). • Explanation:
• Sequential API: Simple linear stack of layers, suitable for straightforward models.


Functional API:
• Diagram: Demonstrates a more complex model with branching.
• Input Layer: Receives input features (e.g., x1, x2, x3). • 1st Hidden Layer: Processes the input layer.
• 2nd Hidden Layer: Processes the output from the 1st hidden layer.
• Branched Hidden Layer: Creates a branch for more complex processing.
• 1st Output Layer: Produces the first output (y1).
• 2nd Output Layer: Produces the second output (y2). • Explanation:
• Functional API: Allows complex architectures with multiple inputs, outputs, and shared layers.

This diagram and the accompanying explanations should help illustrate the specific features and use cases of each API more effectively.

don't miss our new posts. Subscribe for updates

We don’t spam! Read our privacy policy for more info.