The Sequential model in Keras in Python
In this tutorial, we will see the sequential model in Keras and how to use this to build a deep learning model in Python.
An overview of this post:
- What is Keras?
- What is a Sequential model?
- How to use this to build a deep learning model?
Keras:
It is a tensor flow deep learning library to create a deep learning model for both regression and classification problems.
Sequential model:
It allows us to create a deep learning model by adding layers to it. Here, every unit in a layer is connected to every unit in the previous layer.
To build a deep learning model:
Things to get installed:
TensorFlow
pip install tensorflowKeras
pip install keras
Steps involved:
- Import the necessary modules
- Instantiate the model
- Add layers to it
- Compile the model
- Fit the model
1. Import modules:
import keras from keras.model import Sequential from keras.layers import Dense
2. Instantiate the model:
model = Sequential()
3. Add layers to the model:
- INPUT LAYER
model.add(Dense(number.of.nodes, activation function,input shape))- HIDDEN LAYER
model.add(Dense(number.of.nodes, activation function))Note:
We can add more hidden layers based on our requirements.
- OUTPUT LAYER
model.add(Dense(no.of.nodes))Note:
- For a classification problem, we will include an activation function called “softmax” that represents multiple outcomes.
4. Compile the model:
Here, we need to pass two main things as arguments. They are
- Optimizer (to control the learning rate, thus reducing the losses).
- Loss function
model.compile(optimizer,loss function)We pass an additional argument called metrics for classification problems to see the model’s progress, i.e., accuracy.
model.compile(optimizer,loss function,metrics)
5. Fit the model:
model.fit(features,target)Note:
For a classification problem, we need to get the target for each class. So, we will convert a single output to multiple outputs using “to_categorical.”
from keras.np_utils import to_categoricalFinally, we can make predictions on the model.
CODE in Python:
Now, we will take an example dataset of a classification problem.
import pandas as pd
import numpy as np
df = pd.read_csv("titanic_dataset.csv")
df.head()
#getting the features and target from the data frame
features = np.array(df.drop(['survived'],axis=1))
target = df["survived"]
#converting target column into categories
from keras.utils import to_categorical
target=to_categorical(target)
#To create a Sequential model
import keras
from keras.models import Sequential
from keras.layers import Dense
#instantiate the model
model = Sequential()
#input layer
#We take the number of columns in features as input shape.
model.add(Dense(100,activation='relu',input_shape=(10,)))
#hidden layer
model.add(Dense(100, activation='relu'))
#output layer
model.add(Dense(2,activation='softmax'))
Note:
Since this data set has two outcomes (survived or not survived), we have used two nodes in the output layer.
#compile and fit the model model.compile(optimizer = 'adam',loss = 'categorical_crossentropy',metrics = ['accuracy']) model.fit(features,target,validation_split = 0.3,epochs = 10,batch_size = 128)

Click here to know more about the optimizer that we used.
- Validation split – Splits some of the data for validation.
- Epoch – Number of times the training vectors used to update the weights.
- Batch Size – For the larger dataset, this helps in dividing the data into samples and train them.
#To get the summary of the model: model.summary()

I hope this post helps!
Leave a Reply