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.
There are two primary ways to create a Sequential
model:
.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.
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.
(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.
The Sequential
model enforces a simple, linear flow of data:
A conceptual diagram illustrating the linear data flow through a simple
Sequential
model with twoDense
layers. Input data passes through Layer 1, then Layer 2, producing the final output.
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:
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.
© 2025 ApX Machine Learning