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 KerasSequential API in KerasThe 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.ImplementationThis 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:Output for Sequential API in KerasFunctional API in KerasThe 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().ImplementationThis 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:Output for Functional API in KerasRelated Articles:How to create Models in Keras?What is Keras? Comment More infoAdvertise with us Next Article Sequential vs Functional API in Keras S shrurfu5 Follow Improve Article Tags : Deep Learning AI-ML-DS With Python Deep Learning Similar Reads Keras Sequential Class Keras is one of the most popular libraries for building deep learning models due to its simplicity and flexibility. The Sequential class in Keras is particularly user-friendly for beginners and allows for quick prototyping of machine learning models by stacking layers sequentially. This article prov 6 min read Custom Loss Function in R Keras In deep learning, loss functions guides the training process by quantifying how far the predicted values are from the actual target values. While Keras provides several standard loss functions like mean_squared_error or categorical_crossentropy, sometimes the problem you're working on requires a cus 3 min read How to Create a Custom Loss Function in Keras Creating a custom loss function in Keras is crucial for optimizing deep learning models. The article aims to learn how to create a custom loss function. Need to create Custom Loss Functions Loss function is considered as a fundamental component of deep learning as it is helpful in error minimization 3 min read Basic Image Classification with keras in R Image classification is a computer vision task where the goal is to assign a label to an image based on its content. This process involves categorizing an image into one of several predefined classes. For example, an image classification model might be used to identify whether a given image contains 10 min read How to Flatten Input in nn.Sequential in PyTorch One of the essential operations in neural networks, especially when transitioning from convolutional layers to fully connected layers, is flattening. Flattening transforms a multi-dimensional tensor into a one-dimensional tensor, making it compatible with linear layers. This article explores how to 3 min read Like