How to draw neural network diagrams using Graphviz

In my previous post about neural networks, I have presented two figures to illustrate possible neural network’s structures that could be used in binary and multi-class classification problems, respectively. Both figures, which I reproduce below, were draw using Graphviz. Graphviz is an open source graph visualization software and is useful to represent structural information as diagrams of abstract graphs and networks. These neural network diagrams below were my very first try with Graphviz.  Next, I illustrate how to get started with Graphviz and make the code used to create Figures 1 and 2 available.

Neural network diagram with one output unit for binary classification problems

Figure 1: An example of a neural network diagram with one output unit for binary classification problems.

Installing Graphviz

If you use Linux, type

sudo apt-get install graphviz

to install Graphviz. If you don’t use Linux I highly suggest you to start using it as soon as possible 🙂

A neural network diagram for multi-class classification problems

Figure 2: An example of a neural network diagram with K=4 output units for a multi-class classification problem.

Description files

The Graphviz layout programs take descriptions of graphs in a simple text language, and make diagrams in useful formats, such as images, PDF, etc. The description files containing the code used to build Figures 1 and 2 are given below as links to gist:

Code to Figure 1
Code to Figure 2

There is a technical guide on how to produce code to draw directed graphs using Graphviz. However, I highly recommend you to visit their gallery, look for examples close to what you want to produce and then try to figure it out what to modify to get there. If doubt comes, then you go to their technical guide and/or try to search on the web for more explanation. At least that is how I did it.

From description files to images

Once you have the description code into file.txt, you then type

dot -Tpng -O file.txt

on the command-line to produce a .PNG image. You can use -Tpdf to produce a .pdf file or -Tformat if you want another available format. dot is the default tool to draw “hierarchical” or layered drawings of directed graphs.

Related posts:

– 4th and 5th week of Coursera’s Machine Learning (neural networks)

Leave a comment