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:

  1. What is Keras?
  2. What is a Sequential model?
  3. How to use this to build a deep learning model?


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:


pip install tensorflow


pip install keras

Steps involved:

  1. Import the necessary modules
  2. Instantiate the model
  3. Add layers to it
  4. Compile the model
  5. 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:

 model.add(Dense(number.of.nodes, activation function,input shape))
model.add(Dense(number.of.nodes, activation function))


We can add more hidden layers based on our requirements.



  • 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

  1. Optimizer (to control the learning rate, thus reducing the losses).
  2. 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:,target)


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_categorical

Finally, 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")

#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

#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. 

#hidden layer
model.add(Dense(100, activation='relu'))

#output layer

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']),target,validation_split = 0.3,epochs = 10,batch_size = 128)

The Sequential model in Keras in Python

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:

The Sequential model in Keras in Python

I hope this post helps!

Leave a Reply

Your email address will not be published. Required fields are marked *