EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial Keras Plot Model
Secondary Sidebar
Keras Tutorial
  • Basic
    • What is Keras?
    • Keras Install
    • Keras Applications
    • Keras Sequential
    • Keras Model Predict
    • Keras Save Model
    • Keras conv2D
    • Keras ImageDataGenerator
    • Keras input
    • Keras Datasets
    • Keras Early Stopping
    • Keras input
    • Keras Model Save
    • Keras LSTM Example
    • Keras Flatten
    • Keras Optimizers
    • Keras Layers
    • Keras Dense
    • Keras fit
    • Keras Model
    • Keras Metrics
    • Keras Batch Normalization
    • Keras CNN
    • Keras predict
    • Keras Dropout
    • Keras Embedding
    • Keras LSTM
    • Keras GPU
    • Keras Tuner
    • Keras VGG16
    • Keras Generator
    • Keras Pre-trained Models
    • Keras Custom Loss Function
    • keras.utils.to_categorical
    • Keras Neural Network
    • Keras Preprocessing
    • Keras Regularization
    • Keras Softmax
    • Keras Regression
    • Keras MaxPooling2D
    • Keras U-Net
    • Keras Initializers
    • Keras Transformer
    • Keras Data Augmentation
    • Keras ResNet50
    • Keras Verbose
    • Keras Plot Model
    • Keras OCR
    • Keras Utils Sequence
    • Keras Binary Classification
    • Keras Padding
    • UpSampling2d
    • Keras EfficientNet
    • Keras pad_sequences

Keras Plot Model

Keras Plot Model

Introduction to Keras Plot Model

Keras plot model is defined in tensorflow, a machine learning framework provided by Google. Basically, it is an open source that was used in the Tensorflow framework in conjunction with Python to implement the deep learning algorithm. It contains the optimization technique that was used to perform the complicated and mathematical operations. It is more scalable and will support multiple platforms.

Key Takeaways

  • The keras plot model is plotted as layers of the graph. We can achieve the keras graph by using the function name as plot_model in keras.
  • We can define it by using the keras model and keras model sequential function. We need to import the plot_model library.

What is Keras Plot Model?

Keras API is a deep learning API written in Python. It is a high-level API with a productive interface that helps us in solving machine learning problems. Keras plot is built on top of the Tensorflow framework. It is designed to experiment in a specific way. It will provide the building blocks and abstractions that will allow us to encapsulate the machine learning solution.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The keras function API will help us in creating models that are more flexible than models created using the sequential API. The functional API will work with a model that has a nonlinear topology, sharing layers and handling multiple outputs and inputs. The deep learning model has multiple layers and is directed. The keras functional API helps us in the creation of graph layers.

How to Use the Keras Plot Model?

The below steps show how we can use the keras plot model as follows. For using the plot model we need to import the required models as follows:

1. In this step we are importing the required model to use the keras plot model as follows. We are importing the same by using keywords as an import.

Code:

import pandas as pd
………
from keras.utils import plot_model
import numpy as np
from keras.datasets import mnist
……..
from keras.layers import Dense

Output:

Keras Plot Model Importing

2. After importing the module now in this step we are loading the dataset. We are loading the dataset name mnist as follows.

Code:

(X_train, y_train), (X_test, y_test) = mnist.load_data()

Output:

Keras Plot Model Loading

3. After loading the dataset now in this step we are defining the model. We are defining the model name as sequential.

Code:

mod = Sequential()
mod.add(layers.Dense(128, kernel_initializer = 'uniform', input_shape = (10,)))

Output:

Keras Plot Model - Sequential

4. While defining the model now in this step we are defining the activation function by using the created model as follows.

Code:

mod.add (layers.Activation ('relu'))
mod.add (Dense(256))
mod.add (Dropout(0.2))
mod.add (Dense(256, activation = 'relu'))
mod.add (Dropout(0.1))

Output:

defining the activation function

5. After defining the function of activation now in this step we are setting up the configuration of the image as follows.

Code:

img_width, img_height = 22, 22
batch_size = 64
no_epochs = 15
no_classes = 5
validation_split = 0.1
verbosity = 1

Output:

Keras Plot Model Configuration

6. After the setup of image configuration now in this step we are adding the optimizer.

Code:

mod.compile (optimizer = 'adamax')

Output:

adding the optimizer

7. After adding the optimizer now in this step we are plotting the model by using keras plot_model.

Code:

ifile = 'cat.jpg'
tf.keras.utils.plot_model (mod, to_file = ifile, show_shapes = True, show_layer_names = True)

Output:

Keras Plot Model Plotting

How to Plot Model in Architecture?

For understanding the model of keras we have an idea of model layers virtual representation. We can display the architechture of the keras model and save the same into a file. The tf.keras.utils provide the plot_model function for plotting and saving the architechture of the model into the file. For plotting the architechture of the plot model we are importing the below model and creating a model snippet.

Code:

import tensorflow as tf
ip = tf.keras.Input (shape = (100,), dtype = 'int32', name = 'ip')
x_ax = tf.keras.layers.Embedding (
output_dim=512, input_dim = 1000, input_length = 100)(ip)
x_ax = tf.keras.layers.LSTM(32)(x_ax)
x_ax = tf.keras.layers.Dense(64, activation = 'relu')(x_ax)
op = tf.keras.layers.Dense (1, activation = 'sigmoid', name = 'op')(x_ax)
mod = tf.keras.Model(inputs = [ip], outputs = [op])

Output:

Keras Plot Model - model snippet

After defining the model snippet now in this step, we are defining the plot model architechture.

Code:

ifile = 'dog.jpg'
tf.keras.utils.plot_model (mod, to_file = ifile, show_shapes = True, show_layer_names = True)

Output:

plot model architechture

Keras Plot Model Function

The function will convert the keras model in the dot format and save the file. Below is the keras plot_model function and its argument as follows. It will contain multiple arguments.

Code:

tf.keras.utils.plot_model
(
mod,
to_file = "cat.jpg",
show_shapes = False,
show_dtype = False,
show_layer_names=True,
rankdir = "GB",
expand_nested = False,
dpi = 64,
layer_range = None,
show_layer_activations = False,
)

Output:

in the dot format

Below are the arguments of the plot_model function as follows. Keras plot_model contains multiple arguments.

  • Model – This is an instance of the keras model.
  • To_file – This is defined as the file name of the plot image.
  • Show_shapes – It is defined as whether we display the shape information.
  • Show_dtype – It is defined as whether we display layer dtype.
  • Show_layer_name – It is defined as whether we display layer names.
  • Rankdir – We are passing this argument with pydot.
  • Expand_nested – This is defined as whether we are expanding the models which were nested.
  • Dpi – This is defined as per inch dot.
  • Layer_range – This is defined as a list which was containing the items of two strings.
  • Show_layer_activations – This is defined as display layer activations.

Keras Plot Model Graph

The summary is very useful when using simple models, but it becomes very confusing when we have multiple inputs and outputs. Keras provides a function for creating a plot for the graph of a network neural network, which makes the model more complex, but it is very simple to understand. The plot model function in Keras will generate a plot of our network. When we use this in our code, it will accept multiple arguments.

Code:

from keras.layers import Dense
from keras.models import Sequential
from keras.utils.vis_utils import plot_model
mod = Sequential()
mod.add (Dense (2, input_dim = 1, activation = 'relu'))
mod.add (Dense(1, activation = 'sigmoid'))
plot_model (mod, to_file = 'cat.jpg', show_shapes = True, show_layer_names = True)

Output:

Graph

FAQ

Given below are the FAQs mentioned:

Q1. What is the use of keras plot model?

Answer: It is used to convert the keras model into the dot format and after converting it will save in a specified image file.

Q2. Which libraries module do we need to import at the time of using keras plot model?

Answer: We need to import the keras, pandas, numpy, plot_model, and tensorflow module at the time of using the keras plot model.

Q3. How we can use the keras to plot the model by using python?

Answer: We can use the keras plot_model to plot the graph in keras. We need to use the function name as plot_model and need to import the library of the keras module.

Conclusion

The functional API will work with a model that has a nonlinear topology, sharing layers and working with multiple outputs and inputs. Basically, it is an open source that was used in conjunction with the Tensorflow framework in Python to implement the deep learning algorithm.

Recommended Articles

This is a guide to Keras Plot Model. Here we discuss the introduction, and how to use the keras plot model. how to plot a model in architecture? and FAQ. You may also have a look at the following articles to learn more –

  1. Keras Tuner
  2. What is Keras GPU?
  3. Keras Embedding
  4. Keras predict
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more