ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Deep Learning Fundamentals with Keras
Chapter 1: Introduction to Deep Learning Concepts
What is Deep Learning?
Artificial Neurons and Network Structure
Supervised Learning with Neural Networks
Introduction to Keras
Setting Up Your Keras Environment
Hands-on Practical: Environment Verification
Chapter 2: Building Neural Networks with Keras
The Sequential API
Common Layer Types: Dense
Activation Functions
Specifying Input Shape
The Functional API
Model Summary and Visualization
Practice: Building Your First Network with Keras
Quiz for Chapter 2
Chapter 3: Training Deep Neural Networks
The Compilation Step
Understanding Loss Functions
Optimization Algorithms
Backpropagation
The Training Loop: fit() Method
Batches and Epochs
Validation Data and Monitoring Performance
Model Evaluation: evaluate() Method
Hands-on Practical: Training a Simple Classifier
Quiz for Chapter 3
Chapter 4: Convolutional Neural Networks (CNNs)
Introduction to Convolutional Networks
Convolutional Layers (Conv2D)
Pooling Layers (MaxPooling2D)
Building a Simple CNN Architecture
Flattening and Dense Layers in CNNs
Working with Image Data in Keras
Understanding Feature Maps
Practice: Implementing a CNN for Image Classification
Quiz for Chapter 4
Chapter 5: Recurrent Neural Networks (RNNs)
Introduction to Sequence Data
Recurrent Neural Network Concepts
SimpleRNN Layer in Keras
The Vanishing Gradient Problem
Long Short-Term Memory (LSTM) Networks
LSTM Layer in Keras
Gated Recurrent Units (GRUs)
Preparing Sequence Data for RNNs
Practice: Building an RNN/LSTM for Text Classification
Quiz for Chapter 5
Chapter 6: Improving Model Performance and Workflow
Understanding Overfitting and Underfitting
Regularization Techniques: L1, L2, Dropout
Data Augmentation
Using Callbacks
Saving and Loading Models
Introduction to TensorBoard
Hyperparameter Tuning Concepts
Hands-on Practical: Applying Improvement Techniques
Quiz for Chapter 6

The Sequential API

Keras provides intuitive ways to define the architecture of your neural networks. For many standard deep learning tasks, networks consist of layers arranged in a straightforward sequence, where the output of one layer feeds directly into the input of the next. For these common cases, Keras offers the Sequential API, a simple and elegant way to build models layer-by-layer.

Think of the Sequential model as a linear stack of pancakes. You add layers one on top of the other, and data flows through them in the order they were added. It's the most common way to start building models in Keras due to its simplicity.

Creating a Sequential Model

There are two primary ways to create a Sequential model:

  1. Passing a list of layers to the constructor: This is often the most concise method when you know the entire architecture upfront.
  2. Using the .add() method: This allows you to incrementally add layers, which can be useful when building models programmatically or experimenting.

Let's see both in action. First, ensure you import the necessary components:

# Import Sequential model type
from keras.models import Sequential

# Import the Dense layer type (fully connected)
from keras.layers import Dense

Method 1: List of Layers

You can define your model by passing a list of layer instances directly to the Sequential constructor:

# Define a model with two Dense layers
model = Sequential([
    Dense(units=64, activation='relu', input_shape=(784,)), # First layer needs input_shape
    Dense(units=10, activation='softmax')                  # Output layer
])

# Print a summary of the model's layers and parameters
model.summary()

Method 2: Using .add()

Alternatively, you can instantiate an empty Sequential model and add layers one by one using the .add() method:

# Create an empty Sequential model
model = Sequential()

# Add the first layer (specifying input_shape)
model.add(Dense(units=64, activation='relu', input_shape=(784,)))

# Add the second (output) layer
model.add(Dense(units=10, activation='softmax'))

# Print the summary
model.summary()

Both methods result in the exact same model architecture. The choice between them often comes down to personal preference or coding style. The list-based approach is compact, while the .add() method can be more readable for complex sequences or when layers are added conditionally.

Specifying the Input Shape

Notice the input_shape=(784,) argument in the first Dense layer of both examples. This is important. Keras needs to know the shape of the input data the model should expect so it can create the necessary weights for the first layer.

  • Why only the first layer? Once the first layer is defined and knows its input shape, Keras can automatically infer the input shape for all subsequent layers. The output shape of layer NNN becomes the input shape of layer N+1N+1N+1.
  • What does (784,) mean? This specifies that the model expects input samples where each sample is a vector of 784 features. This is a common shape for flattened MNIST images (28x28 pixels = 784). If you were working with tabular data with 10 features, you might use input_shape=(10,). For image data that isn't flattened, you might use shapes like (height, width, channels), e.g., (28, 28, 1).

You only need to specify the input shape for the very first layer added to the Sequential model.

Visualizing the Linear Flow

The Sequential model enforces a simple, linear flow of data:

A diagram illustrating the linear data flow through a simple Sequential model with two Dense layers. Input data passes through Layer 1, then Layer 2, producing the final output.

When Not to Use Sequential

While powerful for many tasks, the Sequential API has limitations. It's designed strictly for models where each layer has exactly one input tensor and one output tensor, and these layers are arranged linearly. You cannot use the Sequential API to build models that:

  • Have multiple inputs or multiple outputs.
  • Share layers (use the same layer instance multiple times in the graph).
  • Have non-linear connections, such as residual connections or branches.

For these more complex architectures, Keras provides the Functional API, which we will cover in a later section.

For now, the Sequential API provides a clean and straightforward way to get started with building many common types of neural networks, such as simple feed-forward classifiers and regressors. In the following sections, we'll look closer at the Dense layer and activation functions used in these examples.

  • Sequential model, Keras team, 2024 - Official Keras documentation providing detailed API specifications and usage examples for the Sequential model.
  • Deep Learning with Python, François Chollet, 2021 (Manning Publications) - An authoritative book by the creator of Keras, covering core concepts and practical applications of Keras, including model building with the Sequential API.
  • Build a Keras model with the Sequential API, fchollet, 2024 (TensorFlow) - Official TensorFlow guide that discusses how to construct models using the Keras Sequential API, with practical code examples.

© 2026 ApX Machine Learning