Visualizing Artificial Neural Networks (ANNs) with just One Line of Code

Adesh Shah
Towards Data Science
3 min readApr 14, 2018

--

ann-visualizer

ANN Visualizer is a python library that enables us to visualize an Artificial Neural Network using just a single line of code. It is used to work with Keras and makes use of python’s graphviz library to create a neat and presentable graph of the neural network you’re building.

With advanced in deep learning, you can now visualise the entire deep learning process or just the Convolutional Neural Network you’ve built.

We are going to build simple neural network using keras and then use ANNvisualizer to visualize our neural network.

Jason Brownlee has produced some great work on http://machinelearningmastery.com/. One of his example was building simple neural network using Keras.

Installation

We will need 3 libraries for this demo

  • keras
  • ANNvisualizer
  • graphviz

You can install the library using the below commands:

pip3 install keraspip3 install ann_visualizerpip install graphviz

Create a new file called index.py copy-and-paste the code into the file

To generate the visualization, you need to follow the below command structure:

ann_viz(model, view=True, filename=”network.gv”, title=”MyNeural Network”)

  • model – Your Keras sequential model
  • view – If set to true, it opens the graph preview after the command has been executed
  • filename – Where to save the graph. (it’s saved in a ‘.gv’ file format)
  • title – The title for the visualized ANN

You have just seen how you can easily create your first neural network model in Keras.

Let’s tie it ann_viz() together into this code.

Run index.py using:

python3 index.py

Below is an example of what the final visualization looks like:

Beautiful, isn’t it? You can go through this GitHub repository here to look at a demo of how a model is created and visualized.

This is useful in a lot of ways. It can be used for teaching purposes when you want to explain how your neural network of that model looks like with just one line of code.

Link to full code for this

References:

This is a library worth checking out.

Thanks for reading! 😊 If you enjoyed it, test how many times can you hit 👏 in 5 seconds. It’s great cardio for your fingers AND will help other people see the story.

--

--

Writing to express, not to impress. Principal Investigator | Machine Learning Fanatic | AI Dev | Optimistic Futurist