## How do you visualize neural network architectures?

126

97

When writing a paper / making a presentation about a topic which is about neural networks, one usually visualizes the networks architecture.

What are good / simple ways to visualize common architectures automatically?

2

– Martin Thoma – 2016-11-17T09:52:48.623

2

– Martin Thoma – 2017-03-09T10:10:30.637

3

I wrote Simple diagrams of convoluted neural networks with a survey of deep learning visualization approaches (both manual and automatic). I got a lot of inspiration, and links, from this thread - thx!

– Piotr Migdal – 2018-09-17T20:00:59.767

56

I recently created a tool for drawing NN architectures and exporting SVG, called NN-SVG

works for me 1/23/19. If you're still having an issue, please feel free to open an issue. – Alex Lenail – 2019-01-23T23:28:44.730

2this is the only right answer – ArtificiallyIntelligence – 2019-02-23T15:52:07.167

awesome tool. However, I noticed that in AlexNet style, the dimensions of the Tensors were mistakenly represented (width and height dimensions) – FlySoFast – 2019-04-29T16:01:06.010

Awesome, how can i visualize LSTM and attention? – keramat – 2020-07-14T18:53:11.763

How do you represent width, skip connections etc? This is very basic. Please delete this answer. – Soumya Kundu – 2020-10-05T10:57:00.567

@AlexLenail is this project open source? – Yatin – 2020-10-05T15:21:10.143

https://github.com/alexlenail/NN-SVG – Alex Lenail – 2020-10-07T02:07:56.220

Really awesome tool! – Javier TG – 2020-11-21T19:56:08.983

37

## Tensorflow, Keras, MXNet, PyTorch

If the neural network is given as a Tensorflow graph, then you can visualize this graph with TensorBoard.

Here is how the MNIST CNN looks like:

You can add names / scopes (like "dropout", "softmax", "fc1", "conv1", "conv2") yourself.

### Interpretation

The following is only about the left graph. I ignore the 4 small graphs on the right half.

Each box is a layer with parameters that can be learned. For inference, information flows from bottom to the top. Ellipses are layers which do not contain learned parameters.

The color of the boxes does not have a meaning.

I'm not sure of the value of the dashed small boxes ("gradients", "Adam", "save").

it is good, I am trying to avoid the name like conv1, conv2 etc, I want to make all the name of conv later as CONV, How I will do?? – Sudip Das – 2018-03-26T13:03:15.813

+1. It's not only for TF though: MXNet and Pytorch have some support too

– Jakub Bartczuk – 2018-07-03T16:08:36.257

@SudipDas You can add names in the code to the layers, which will show up as you plot it. – Ben – 2018-11-27T16:16:02.800

How I will show the name of each layer as "CONV", if I write it as "CONV" of each layer then I will get error, cause each layer should have a unique name as tf rules, BUT I want to know, is there any other way to overcome this problem?? @Ben – Sudip Das – 2018-11-27T16:22:30.797

@SudipDas Oh well, that does not work. I think you can only overcome this problem by storing it and editing it yourself. Maybe you can cheat by adding invisible characters to the name, but I would recommend giving them unique names anyway. – Ben – 2018-11-27T16:32:58.237

Yes, absolutely, Thank you so much for your reply @Ben – Sudip Das – 2018-11-28T13:04:57.433

1@onof I fixed the link – Martin Thoma – 2019-09-23T08:55:59.827

19

In Caffe you can use caffe/draw.py to draw the NetParameter protobuffer:

In Matlab, you can use view(net)

19

There is an open source project called Netron

Netron is a viewer for neural network, deep learning and machine learning models.

Netron supports ONNX (.onnx, .pb), Keras (.h5, .keras), CoreML (.mlmodel) and TensorFlow Lite (.tflite). Netron has experimental support for Caffe (.caffemodel), Caffe2 (predict_net.pb), MXNet (-symbol.json), TensorFlow.js (model.json, .pb) and TensorFlow (.pb, .meta).

17

I would add ASCII visualizations using keras-sequential-ascii (disclaimer: I am the author).

A small network for CIFAR-10 (from this tutorial) would be:

       OPERATION           DATA DIMENSIONS   WEIGHTS(N)   WEIGHTS(%)

Input   #####     32   32    3
Conv2D    \|/  -------------------       896     2.1%
relu   #####     30   30   32
MaxPooling2D   Y max -------------------         0     0.0%
#####     15   15   32
Conv2D    \|/  -------------------     18496    43.6%
relu   #####     13   13   64
MaxPooling2D   Y max -------------------         0     0.0%
#####      6    6   64
Flatten   ||||| -------------------         0     0.0%
#####        2304
Dense   XXXXX -------------------     23050    54.3%
softmax   #####          10


For VGG16 it would be:

       OPERATION           DATA DIMENSIONS   WEIGHTS(N)   WEIGHTS(%)

Input   #####      3  224  224
InputLayer     |   -------------------         0     0.0%
#####      3  224  224
Convolution2D    \|/  -------------------      1792     0.0%
relu   #####     64  224  224
Convolution2D    \|/  -------------------     36928     0.0%
relu   #####     64  224  224
MaxPooling2D   Y max -------------------         0     0.0%
#####     64  112  112
Convolution2D    \|/  -------------------     73856     0.1%
relu   #####    128  112  112
Convolution2D    \|/  -------------------    147584     0.1%
relu   #####    128  112  112
MaxPooling2D   Y max -------------------         0     0.0%
#####    128   56   56
Convolution2D    \|/  -------------------    295168     0.2%
relu   #####    256   56   56
Convolution2D    \|/  -------------------    590080     0.4%
relu   #####    256   56   56
Convolution2D    \|/  -------------------    590080     0.4%
relu   #####    256   56   56
MaxPooling2D   Y max -------------------         0     0.0%
#####    256   28   28
Convolution2D    \|/  -------------------   1180160     0.9%
relu   #####    512   28   28
Convolution2D    \|/  -------------------   2359808     1.7%
relu   #####    512   28   28
Convolution2D    \|/  -------------------   2359808     1.7%
relu   #####    512   28   28
MaxPooling2D   Y max -------------------         0     0.0%
#####    512   14   14
Convolution2D    \|/  -------------------   2359808     1.7%
relu   #####    512   14   14
Convolution2D    \|/  -------------------   2359808     1.7%
relu   #####    512   14   14
Convolution2D    \|/  -------------------   2359808     1.7%
relu   #####    512   14   14
MaxPooling2D   Y max -------------------         0     0.0%
#####    512    7    7
Flatten   ||||| -------------------         0     0.0%
#####       25088
Dense   XXXXX ------------------- 102764544    74.3%
relu   #####        4096
Dense   XXXXX -------------------  16781312    12.1%
relu   #####        4096
Dense   XXXXX -------------------   4097000     3.0%
softmax   #####        1000


13

Keras

The keras.utils.vis_utils module provides utility functions to plot a Keras model (using graphviz)

The following shows a network model that the first hidden layer has 50 neurons and expects 104 input variables.

plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)


Can I use it in LaTex compatible format? – hola – 2019-07-11T00:01:41.967

they use it so obviously you can.. probably just embed the image like any other figure – sivi – 2020-02-13T16:38:58.090

12

Here is yet another way - dotnets, using Graphviz, heavily inspired by this post by Thiago G. Martins.

12

The Python package conx can visualize networks with activations with the function net.picture() to produce SVG, PNG, or PIL Images like this:

Conx is built on Keras, and can read in Keras' models. The colormap at each bank can be changed, and it can show all bank types.

11

I've been working on a drag-and-drop neural network visualizer (and more). Here's an example of a visualization for a LeNet-like architecture. Models with fan-out and fan-in are also quite easily modeled. You can visit the website at https://math.mit.edu/ennui/

The open-source implementation is available at https://github.com/martinjm97/ENNUI.

my browser keeps crashing when press Train – datdinhquoc – 2019-09-25T04:25:07.483

1Thanks for checking it out. Yes, this bug just popped up recently and seems to be a result of some recent changes to WebGL on Chrome. Everything should work on Firefox. I'll update you when I know more. – Jesse – 2019-09-25T20:19:25.187

tks, your visualiser is amazing, looks greater than tf playground :) – datdinhquoc – 2019-09-26T01:50:45.550

1Thank you! Let me know if you have issues or ideas. We have fun things like code generation too! – Jesse – 2019-09-26T16:01:49.870

1Bug fixes are in and the implementation has been open-sourced! – Jesse – 2020-01-29T15:19:20.043

9

In R, nnet does not come with a plot function, but code for that is provided here.

Alternatively, you can use the more recent and IMHO better package called neuralnet which features a plot.neuralnet function, so you can just do:

data(infert, package="datasets")
plot(neuralnet(case~parity+induced+spontaneous, infert))


neuralnet is not used as much as nnet because nnet is much older and is shipped with r-cran. But neuralnet has more training algorithms, including resilient backpropagation which is lacking even in packages like Tensorflow, and is much more robust to hyperparameter choices, and has more features overall.

1

You should add the updated link for the code of NNet in R https://beckmw.wordpress.com/2013/11/14/visualizing-neural-networks-in-r-update/

– wacax – 2018-05-10T16:47:22.213

5

There are some novel alternative efforts on neural network visualization.

Stunning 'AI brain scans' reveal what machines see as they learn new skills

Inside an AI 'brain' - What does machine learning look like?

These approaches are more oriented towards visualizing neural network operation, however, NN architecture is also somewhat visible on the resulting diagrams.

Examples:

30Please explain what we see here. It looks beautiful, but I don't understand how the fancy images support understanding the operation of the network. – Martin Thoma – 2018-03-27T17:15:52.000

I don't like your derogatory usage of "fancy images" term. @Martin – VividD – 2018-03-27T17:25:21.993

One could call your diagram "lego boxes" as well, and ask the same question. This is not an appropriate conversation style for this site. – VividD – 2018-03-28T07:06:40.660

14I didn't mean to attack you, but your overly defensive answer without actually answering my question speaks for itself. - I added an "interpretation" part to the "lego boxes" diagram. – Martin Thoma – 2018-03-28T07:29:29.833

1By the way: The second link is dead. – Martin Thoma – 2018-03-28T07:37:12.477

6

@MartinThoma It's clearly data art, not data viz (vide https://lisacharlotterost.github.io/2015/12/19/Meaning-and-Beauty-in-Data-Vis/).

– Piotr Migdal – 2018-04-02T14:04:59.977

1Not sure how is this useful, in fact those labels could be anything. – phoxis – 2019-01-10T15:04:14.457

5

Tensorspace-JS is a fantastic tool for 3d visualization of network architecture:

https://tensorspace.org/

and here is a nice post about how to write a program:

https://medium.freecodecamp.org/tensorspace-js-a-way-to-3d-visualize-neural-networks-in-browsers-2c0afd7648a8

Could you provide a link to this tool? – Piotr Migdal – 2019-01-25T14:34:28.477

1@PiotrMigdal I updated the answer. – Ali Mirzaei – 2019-01-26T14:43:40.587

4

Not per se nifty for papers, but very useful for showing people who don't know a lot of about neural networks what their topology may look like. This Javascript library (Neataptic) lets you visualise your network:

4

You can read the popular paper Understanding Neural Networks Through Deep Visualization which discusses visualization of convolutional nets. Its implementation not only displays each layer but also depicts the activations, weights, deconvolutions and many other things that are deeply discussed in the paper. It's code is in caffe'. The interesting part is that you can replace the pre-trained model with your own.

4

# Tensorflow / Keras / Python

I wrote a small python package called visualkeras that allows you to directly generate the architecture from your keras model.

Install via pip install visualkeras

And then it's as simple as:

import visualkeras
visualkeras.layered_view(<model>)


There are lots of options to tweak it and I am working on more visualizations. Also, always open for PRs or feature requests.

Here's what VGG16 looks like:

4

# PlotNeuralNet LaTex tool

This solution is not automatically generated (you need to construct the graph by yourself) but the PlotNeuralNet github repo allows you to build images directly from LaTex, and the result is great ! See for example the image below from the README :

or my example :

3

Netscope is my everyday tool for Caffe models.

1

You can use eiffel2, which you can install using pip:

python -m pip install eiffel2


Just import builder from eiffel and provide a list of neurons per layer in your network as an input.

Example:

from eiffel2 import builder

builder([1, 10, 10, 5, 5, 2, 1])
# or the following if you want to have a dark theme
builder([1, 10, 10, 5, 5, 2, 1], bmode="night")


Output:

To see more about eiffel2 visit the Github repository:

I just figured out Eiffel does not have support anymore, use eiffel2 instead – Eduardo Lozano – 2020-06-10T02:28:04.990

Session crashed. – keramat – 2020-07-14T18:57:26.357