![]() |
VOOZH | about |
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 provides a deep dive into the Sequential class, explaining its features, usage, and common practices.
Table of Content
The Keras Sequential class is a fundamental component of the Keras library, which is widely used for building and training deep learning models. This class provides a simple and intuitive way to create neural networks by stacking layers in a linear fashion. It is particularly well-suited for beginners and for constructing straightforward feedforward networks.
One of the key components of Keras is the Sequential class, which allows developers to build models layer-by-layer in a linear stack. This class is ideal for creating feedforward neural networks and convolutional networks, where the flow of data is straightforward.
Key Features of the Sequential Class:
The Sequential class is particularly suited for situations where your model follows a linear structure. For example, it's ideal for simple neural networks like fully connected layers, convolutional neural networks, and recurrent neural networks. Here are some benefits of using the Sequential model:
Sequential models work by stacking layers in a linear manner. Each layer takes the output from the previous layer as its input. A Sequential model can be thought of as a pipeline where the input data flows through several transformations (layers) to produce a final prediction.
To create a Sequential model in Keras, you can either pass a list of layer instances to the constructor or add layers incrementally using the add() method. Here is an example of creating a simple Sequential model:
The structure typically looks like this:
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))
In this example, the model consists of two layers:
Letβs create a basic neural network using the Sequential class. We'll use the MNIST dataset, which consists of handwritten digits.
Output:
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 ββββββββββββββββββββ 0s 0us/step
/usr/local/lib/python3.10/dist-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)
Adding layers in Sequential is as simple as calling the add() method. For example, to add a convolutional layer followed by a pooling layer, you would do:
In this example:
After defining the model's architecture, the next step is to compile the model. Compiling configures the model for training by specifying the optimizer, loss function, and metrics to monitor during training.
Once compiled, the model can be trained using the fit() method. This method accepts the training data and trains the model for a specified number of epochs.
Output:
Epoch 1/10
1500/1500 ββββββββββββββββββββ 242s 160ms/step - accuracy: 0.9103 - loss: 0.2891 - val_accuracy: 0.9712 - val_loss: 0.0934
Epoch 2/10
1500/1500 ββββββββββββββββββββ 262s 160ms/step - accuracy: 0.9831 - loss: 0.0533 - val_accuracy: 0.9808 - val_loss: 0.0671
Epoch 3/10
1500/1500 ββββββββββββββββββββ 244s 148ms/step - accuracy: 0.9911 - loss: 0.0289 - val_accuracy: 0.9818 - val_loss: 0.0719
Epoch 4/10
1500/1500 ββββββββββββββββββββ 267s 151ms/step - accuracy: 0.9944 - loss: 0.0164 - val_accuracy: 0.9804 - val_loss: 0.0775
Epoch 5/10
1500/1500 ββββββββββββββββββββ 254s 146ms/step - accuracy: 0.9972 - loss: 0.0087 - val_accuracy: 0.9803 - val_loss: 0.0853
Epoch 6/10
1500/1500 ββββββββββββββββββββ 277s 156ms/step - accuracy: 0.9970 - loss: 0.0093 - val_accuracy: 0.9810 - val_loss: 0.0914
Epoch 7/10
1500/1500 ββββββββββββββββββββ 229s 153ms/step - accuracy: 0.9979 - loss: 0.0068 - val_accuracy: 0.9809 - val_loss: 0.1056
Epoch 8/10
1500/1500 ββββββββββββββββββββ 253s 147ms/step - accuracy: 0.9978 - loss: 0.0063 - val_accuracy: 0.9803 - val_loss: 0.1141
Epoch 9/10
1500/1500 ββββββββββββββββββββ 260s 145ms/step - accuracy: 0.9980 - loss: 0.0061 - val_accuracy: 0.9819 - val_loss: 0.1107
Epoch 10/10
1500/1500 ββββββββββββββββββββ 259s 143ms/step - accuracy: 0.9989 - loss: 0.0037 - val_accuracy: 0.9830 - val_loss: 0.1137
313/313 ββββββββββββββββββββ 14s 44ms/step - accuracy: 0.9780 - loss: 0.1101
Test accuracy: 0.9828000068664551
313/313 ββββββββββββββββββββ 10s 32ms/step
While the Sequential class offers simplicity and ease of use, it comes with certain limitations:
The Sequential class is best used when:
The Sequential class in Keras offers an intuitive way to build simple neural networks. With its easy-to-use interface, developers can prototype models quickly. However, for more complex architectures, alternative approaches like the Functional API or Model subclassing may be more suitable. Nonetheless, Sequential remains a powerful tool for building standard neural networks efficiently.