How to plot models using keras?

This recipe helps you plot models using keras

Recipe Objective

Plot models using keras.

Plotting of models tells us the how our code proceeds, one can easily demonstrates our keras model.

There is a built-in plot_model facility within Keras. It allows us to create a visualization of your model.

Step 1- Importing Libraries

#importing Libraries import pandas as pd import numpy as np from keras.datasets import mnist from sklearn.model_selection import train_test_split from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout from tensorflow.keras import layers from keras.utils import plot_model

Step 2- Loading dataset

#Loading Dataset (X_train, y_train), (X_test, y_test) = mnist.load_data()

Step 3- Defining the model.

We will define the model and then define the layers, kernel initializer, and its input nodes shape.

#Model model = Sequential() model.add(layers.Dense(64, kernel_initializer='uniform', input_shape=(10,)))

Step 4- Defining the activation function.

Define the activation function and add layers to the model

#Adding Layers model.add(layers.Activation('relu')) model.add(Dense(512)) model.add(Dropout(0.2)) model.add(Dense(256, activation='relu')) model.add(Dropout(0.1))

Step 5- Setting up the image.

Adding the parameters for the image to setup.

# Model configuration img_width, img_height = 28, 28 batch_size = 250 no_epochs = 25 no_classes = 10 validation_split = 0.2 verbosity = 1

Step 6- Adding optimizer.

Add the optimizer according to the need.

model.compile( optimizer='adamax')

Step 7- Plot the model

#plot model plot_model(model, to_file='model.png')

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.