Open In App

Sequential vs Functional API in Keras

Last Updated : 26 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Keras provides two main ways to build deep learning models: the Sequential API and the Functional API. Both are part of the Keras high-level API, but they differ in terms of flexibility and use cases. The Sequential API is best for models with a linear flow one layer after another. On the other hand the Functional API offers more flexibility making it ideal for building complex models like multi input/output networks or those with non linear layer connections.

Sequential-vs-Functional-API-in-Keras
Sequential vs Functional API in Keras

Sequential API in Keras

  • The Sequential API is the simplest way to create models in Keras. It allows you to build a neural network layer by layer where each layer has exactly one input tensor and one output tensor.
  • To define a model using the Sequential API you either pass a list of layers to the Sequential() constructor or add layers one at a time using the .add() method. The model starts with an input layer followed by hidden layers and ends with an output layer.
  • Once defined the model is compiled using the .compile() method, where you specify the optimizer, loss function and evaluation metrics. Then you can train the model using .fit(), evaluate it using .evaluate() and make predictions using .predict().
  • This API is ideal for most beginner level problems like image classification, sentiment analysis and basic regression tasks where data flows in a single path from input to output.

Implementation

  • This code creates a simple feedforward neural network using Keras's Sequential API with two layers: one hidden dense layer with ReLU activation and an output layer with softmax activation for multi class classification.
  • It prints the model summary and generates a prediction for a single random input of shape (1, 100).
Python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import numpy as np

model = Sequential([
    Dense(64, activation='relu', input_shape=(100,)),
    Dense(10, activation='softmax')
])
model.summary()

x = np.random.random((1, 100))

output = model.predict(x)
print("Output:\n", output)

Output:

Screenshot-2025-06-26-123733
Output for Sequential API in Keras

Functional API in Keras

  • The Functional API in Keras is a powerful and flexible way to build complex neural network architectures. Unlike the Sequential API which limits you to stacking layers linearly, the Functional API allows you to define models where layers can have multiple inputs and outputs, shared layers or even non linear connections such as branching and skip connections.
  • In the Functional API the model is built by explicitly connecting layers using function calls. You start by defining an Input layer which specifies the shape of the input data. Each layer is then treated as a function that takes a tensor as input and returns another tensor as output. By chaining these operations you create a directed acyclic graph (DAG) of layers which represents the flow of data through the model.
  • This approach is especially useful when designing architectures like multi input models, multi output models, models with shared layers and models with internal loops or residual connections.
  • Once the network structure is defined, you create the model by passing the input and output tensors to the Model class. This model can then be compiled, trained and evaluated just like a Sequential model using methods such as .compile(), .fit(), .evaluate() and .predict().

Implementation

  • This code builds a neural network using Keras's Functional API defining the flow from input to output explicitly.
  • It creates an input layer, a hidden dense layer with ReLU activation and an output softmax layer then prints the model summary and generates a prediction for random input data.
Python
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
import numpy as np

inputs = Input(shape=(100,))
x = Dense(64, activation='relu')(inputs)
outputs = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs=outputs)

model.summary()

x_input = np.random.random((1, 100))

output = model.predict(x_input)
print("Output:\n", output)

Output:

Screenshot-2025-06-26-123744
Output for Functional API in Keras

How to create Models in Keras?

What is Keras?


Next Article

Similar Reads