How to Visualize a Deep Learning Neural Network Model in Keras

The Keras Python deep learning library provides tools to visualize and better understand your neural network models.

In this tutorial, you will discover exactly how to summarize and visualize your deep learning models in Keras.

After completing this tutorial, you will know:

  • How to create a textual summary of your deep learning model.
  • How to create a graph plot of your deep learning model.
  • Best practice tips when developing deep learning models in Keras.

Let’s get started.

How to Visualize a Deep Learning Neural Network Model in Keras

How to Visualize a Deep Learning Neural Network Model in Keras
Photo by Ed Dunens, some rights reserved.

Tutorial Overview

This tutorial is divided into 4 parts; they are:

  1. Example Model
  2. Summarize Model
  3. Visualize Model
  4. Best Practice Tips

Example Model

We can start off by defining a simple multilayer Perceptron model in Keras that we can use as the subject for summarization and visualization.

The model we will define has one input variable, a hidden layer with two neurons, and an output layer with one binary output.

For example:

The code listing for this network is provided below.

Summarize Model

Keras provides a way to summarize a model.

The summary is textual and includes information about:

  • The layers and their order in the model.
  • The output shape of each layer.
  • The number of parameters (weights) in each layer.
  • The total number of parameters (weights) in the model.

The summary can be created by calling the summary() function on the model that returns a string that in turn can be printed.

Below is the updated example that prints a summary of the created model.

Running this example prints the following table.

We can clearly see the output shape and number of weights in each layer.

Visualize Model

The summary is useful for simple models, but can be confusing for models that have multiple inputs or outputs.

Keras also provides a function to create a plot of the network neural network graph that can make more complex models easier to understand.

The plot_model() function in Keras will create a plot of your network. This function takes a few useful arguments:

  • model: (required) The model that you wish to plot.
  • to_file: (required) The name of the file to which to save the plot.
  • show_shapes: (optional, defaults to False) Whether or not to show the output shapes of each layer.
  • show_layer_names: (optional, defaults to True) Whether or not to show the name for each layer.

Below is the updated example that plots the created model.

Note, the example assumes that you have the graphviz graph library and the Python interface installed.

Running the example creates the file model_plot.png with a plot of the created model.

Plot of Neural Network Model Graph

Plot of Neural Network Model Graph

Best Practice Tips

I generally recommend to always create a summary and a plot of your neural network model in Keras.

I recommend this for a few reasons:

  • Confirm layer order. It is easy to add layers in the wrong order with the sequential API or to connect them together incorrectly with the functional API. The graph plot can help you confirm that the model is connected the way you intended.
  • Confirm the output shape of each layer. It is common to have problems when defining the shape of input data for complex networks like convolutional and recurrent neural networks. The summary and plot can help you confirm the input shape to the network is as you intended.
  • Confirm parameters. Some network configurations can use far fewer parameters, such as the use of a TimeDistributed wrapped Dense layer in an Encoder-Decoder recurrent neural network. Reviewing the summary can help spot cases of using far more parameters than expected.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Summary

In this tutorial, you discovered how to summarize and visualize your deep learning models in Keras.

Specifically, you learned:

  • How to create a textual summary of your deep learning model.
  • How to create a graph plot of your deep learning model.
  • Best practice tips when developing deep learning models in Keras.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Frustrated With Your Progress In Deep Learning?

Deep Learning with Python

 What If You Could Develop A Network in Minutes

…with just a few lines of Python

Discover how in my new Ebook: Deep Learning With Python

It covers self-study tutorials and end-to-end projects on topics like:
Multilayer PerceptronsConvolutional Nets and Recurrent Neural Nets, and more…

Finally Bring Deep Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

38 Responses to How to Visualize a Deep Learning Neural Network Model in Keras

  1. Mishti December 13, 2017 at 6:05 pm #

    Running plot_model leaves an error.
    It cant import pydot.
    I did install it and try.
    No improvement at all.

    • Jason Brownlee December 14, 2017 at 5:34 am #

      What is the error?

      • Mishti December 14, 2017 at 9:54 pm #

        I get this.
        attribute error: ‘module’ object has no attribute ‘find_graphviz’

        • Jason Brownlee December 15, 2017 at 5:33 am #

          You may need to install two packages: pydot and pygraphviz.

        • Shantanu Oak January 2, 2018 at 9:53 pm #

          if you are using conda environment then may be this will work.

          !conda install –yes graphviz
          !conda install –yes pydotplus

    • Mitchell January 4, 2018 at 5:01 pm #

      I have the same problem.
      It shows “ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.” even I had installed graphviz and pydot or pydotplus.

  2. Murtaza December 17, 2017 at 11:00 am #

    Hi Jason , you could also include a tutorial for tensorboard in which each time a model is run we can log it using callback function and display all runs on tensorboard

  3. vinci December 24, 2017 at 3:02 pm #

    thank you Jason Brownlee for your help……god bless you

  4. Stefano December 27, 2017 at 1:42 am #

    All the prints do not contain the activation function, I think important in defining a layer!…

  5. Daniel January 9, 2018 at 7:05 pm #

    check out this project for a better visual result:
    https://github.com/yu4u/convnet-drawer

  6. mistermoper February 5, 2018 at 3:32 am #

    What does a dense layer respect other kind of layers?
    Another question, un all the neurons of the same layer It Will be used the same activation función?

    • Jason Brownlee February 5, 2018 at 7:47 am #

      All of the neurons in a layer do use the same activation function.

  7. Junho Lee February 20, 2018 at 2:34 am #

    HI, I got this error : “raise ImportError(‘Failed to import pydot. You must install pydot’
    ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.”

    I have installed both pydot and graphviz via “sudo pip install **”, but still not solving the problem. Can I have a comment?

    Thank you

    • Jason Brownlee February 21, 2018 at 6:29 am #

      I recommend installing pygraphviz instead of graphviz.

  8. Jacob J February 23, 2018 at 3:57 pm #

    I’m currently trying to get comfortable with Keras. I think I need to gather examples of importing things like image data. Also, I only really understand basic neural networks so far, I’ll have to study further on how to make convolutional networks with Keras. Finally, I’m not sure I understand how a network can work with batches of images at the same time.

    I plan to use Keras with this repo: github.com/diginessforever/machinelearning -> it already has a nifty webscraper and some of my noobish neural network code.

  9. Pawan March 9, 2018 at 8:06 pm #

    Can we visualize the model in tensorboard

  10. Gautam March 25, 2018 at 5:20 am #

    Jason, really didn’t get it working. I have installed pydot, pygraphviz and gpraphviz using both conda and python – m pip command but still same message. what is the trick. If you can specify command in sequence may help. Thanks in advace.

    • Jason Brownlee March 25, 2018 at 6:34 am #

      There was no special trick, I just pip-installed those two libraries.

      Perhaps try posting/searching for your error on stackoverflow.com?

  11. Alexandru March 25, 2018 at 9:48 pm #

    Hi Jason, cool tutorial! Could you please expand it to a Model with multiple inputs? Nothing complicated, maybe a Dense layer on input a, a Dense layer on input b, merge them together, map to some classes, done.Thanks in advance.

  12. Amit March 26, 2018 at 3:43 am #

    For training purpose in Keras, Do we need to explicitly configure to use GPU ? Or it will automatically use the GPU?

    • Jason Brownlee March 26, 2018 at 10:03 am #

      You must configure the backend (e.g. tensorflow, etc.) to use the GPU. If done, Keras will then use the GPU.

  13. Akhtar Munir April 1, 2018 at 8:46 pm #

    ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.

    I have already installed pydot and graphviz using conda install pydot and conda install graphivz,

    but still showing me this error?

    • Jason Brownlee April 2, 2018 at 5:22 am #

      Sorry to hear that. Perhaps post the error to stackoverflow?

  14. Tomas Mendoza April 3, 2018 at 8:13 pm #

    Hello Jason.

    Digging in stackoverflow i found out that the following error is actually a bug in Keras and many people have it, it does not get fixed even if you install both libraries:

    ImportError: Failed to import pydot. You must install pydot and graphviz for pydotprint to work.

    Apparently there is no way to solve this to this day. Is there any other trick to visualize the model? Thanks.

    • Jason Brownlee April 4, 2018 at 6:11 am #

      No trick. I installed the libraries and wrote/ran the code.

  15. Yu-Chia Hsu June 24, 2018 at 8:55 pm #

    Hello Jason,

    I use your code to picture the same model. And I got a problem that the picture had no input

    layers. Instead, it pictures numbers such as 140223915681232. Do you have the same problem

    before?

    • Jason Brownlee June 25, 2018 at 6:21 am #

      I have not seen that, perhaps it is a bug? You could try posting to issues section of the Keras github project?

  16. Vivek June 27, 2018 at 5:07 pm #

    Hi, Jason I have used conv3d layers in network as my dataset is 3d. now i want to plot the features of intermidiate layers. what is the process to do that and how can i visualize feature maps in intermidiate layers in 3d dataset. w hich library should i use?

    • Jason Brownlee June 28, 2018 at 6:11 am #

      Sorry, I don’t have examples of visualizing the feature maps within a CNN.

Leave a Reply