What are the different types of Keras models?

A widely used high-level API called Keras makes the training and creation of deep learning models a lot more accessible. Keras is Python-based and works on environments that support Python3 and latest versions. It is known to provide a user-friendly interface and support for various computing engines such as Theano, TensorFlow, etc.

Types of Keras models

Keras models are the core data structures used to represent a neural network's design and configuration. They are essentially containers with layers that serve as the construction units for efficient models.

There are two types of Keras models:

Sequential Model in Keras

The Sequential model allows the creation of deep learning models using sequential layers where each layer is added one after another. However, the model is only configured to work on a linear layer stack with a single input and output tensor. It does not support the creation of models with multiple inputs or outputs.

You can find the code for the implementation of Sequential model below:

from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
#Creating a Sequential model
model = Sequential()
#Adding layers to the model
model.add(Dense(64, activation='relu', input_shape=(10,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
#Compiling the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
#Defining sample input data
input_data = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
#Predicting the output using the model
output = model.predict(input_data)
#Printing the output
print("Output:", output)

Code explanation

Line 1–3: These lines import the necessary modules i.e., keras, tensorflow for the creation of Sequential model using Dense layers.

Line 6: This line creates an instance for the Sequential model.

Line 9–11: These lines add Dense layers to the model. In line 9–10, The layer has 64 units/neurons and uses the relu activation function. In Line 11, the layer has 1 unit/neuron and uses the sigmoid activation function, which is suited for binary classification problems. The input_shape=(10,) specifies the shape of the input data, which is a one-dimensional array with 10 elements

Line 14: This line compiles the model by setting the optimizer to 'adam', a well-known optimization algorithm. The loss function is set to 'binary_crossentropy' since this is a binary classification problem. The accuracy metric is used to analyze the model's performance during the training and testing phases.

Line 17: This line defines the sample input data as a nested list containing a single sample.

Line 20: This line uses the predict() to predict the output based on the input data.
Line 23: This line prints the predicted output. It is a floating-point number between 0 and 1 expressing the expected probability of the input falling into the positive class.

Functional API Model in Keras

The Keras Functional API is mainly used to create complex models, such as directed acyclic graphs and models with shared layers. As opposed to the sequential model, it is more flexible concerning supporting models with multiple inputs and outputs and neural networks with various branches.

Being a data structure, Functional API can be saved as a single file, making it simple to recreate the exact model without the source code.

You can find the implementation of this model in Python in the following code:

from tensorflow import keras
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
#Defining input layer
inputs = Input(shape=(10,))
#Defining intermediate layers
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
#Defining output layer
outputs = Dense(1, activation='sigmoid')(x)
#Creating the model
model = Model(inputs=inputs, outputs=outputs)
#Compiling the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
#Defining sample input data
input_data = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
#Predicting the output using the model
output = model.predict(input_data)
#Printing the output
print("Output:", output)

Code explanation

Line 1–3: These lines import the necessary modules i.e., keras, tensorflow for the creation of functional API model using Dense layers.

Line 6: This line defines the input layer for the model. It specifies that the input data will have a shape(10,), indicating that it is a one-dimensional array with 10 elements.

Line 9–10: These lines add Dense layers to the model. They have 64 units/neurons and use the relu activation function. Line 9 takes the output of the input layer as its input. Line 10 takes the output of the previous Dense layer as its input.

Line 13: This line adds the output layer to the model. The layer has 1 unit/neuron and uses the sigmoid activation function suitable for binary classification problem. It takes the output of the previous Dense layer as its input.

Line 16: This line creates the model by defining the input and output layers.

Line 19: This line compiles the model by setting the optimizer to 'adam', a well-known optimization algorithm. The loss function is set to 'binary_crossentropy' since this is a binary classification problem. The accuracy metric is used to analyze the model's performance during training and testing phases.

Line 22: This line defines the sample input data as a nested list containing a single sample.

Line 25: This line uses the predict() method to predict the output based on the input data.

Line 28: This line prints the predicted output. It is a floating-point number between 0 and 1 expressing the expected probability of the input falling into the positive class.

Conclusion

Keras models are the fundamental building blocks of the Keras library, defining the structure, operation, and behavior of neural networks. They act as a framework for training, and deploying deep learning models for different tasks, such as classification, regression, etc.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved