Submit a blog to KDnuggets -- Top Blogs Win A Reward

Topics: AI | Data Science | Data Visualization | Deep Learning | Machine Learning | NLP | Python | R | Statistics

KDnuggets Home » News » 2020 » Dec » Tutorials, Overviews » Generating Beautiful Neural Network Visualizations ( 21:n01 )

Gold BlogGenerating Beautiful Neural Network Visualizations


If you are looking to easily generate visualizations of neural network architectures, PlotNeuralNet is a project you should check out.



Have you built a neural network for a paper, or need to share its architecture with others via a technical report or some other medium?

The Python library PlotNeuralNet by Haris Iqbal helps solve this problem by producing LaTeX code for drawing neural networks.

PlotNeuralNet does not work directly from existing architecture code. Instead, you have to separately and explicitly define network code for the program to consume and use to output its diagram. This makes PlotNeuralNet library agnostic, but you trade some additional manual work for this.

Here is an example of the resulting image rendered by the LaTeX generated by the Python script (got that?):

Figure

Sample convolutional neural network architecture generated by PlotNeuralNet (from the library's Github page)

 

The architectural definition is accomplished via a Python list of function calls, such as in this example from the project's Github:

# Define your neural network architecture here
arch = [
    to_head( '..' ),
    to_cor(),
    to_begin(),
    to_Conv("conv1", 512, 64, offset="(0,0,0)", to="(0,0,0)", height=64, depth=64, width=2),
    to_Pool("pool1", offset="(0,0,0)", to="(conv1-east)"),
    to_Conv("conv2", 128, 64, offset="(1,0,0)", to="(pool1-east)", height=32, depth=32, width=2),
    to_connection( "pool1", "conv2"),
    to_Pool("pool2", offset="(0,0,0)", to="(conv2-east)", height=28, depth=28, width=1),
    to_SoftMax("soft1", 10 ,"(3,0,0)", "(pool1-east)", caption="SOFT"),
    to_connection("pool2", "soft1"),
    to_end()
    ]


A full Python script required to generate a visualization is little more than the architecture definition above:

import sys
sys.path.append('../')
from pycore.tikzeng import *

# Define your neural network architecture here
...

def main():
    namefile = str(sys.argv[0]).split('.')[0]
    to_generate(arch, namefile + '.tex' )

if __name__ == '__main__':
    main()


Once you have the prerequisites installed, and have saved your architecture script above, all that is left is to execute a controlling bash script:

bash ../tikzmake.sh your_script_name


This is a sample excerpt of the LaTeX generated from a PlotNeuralNet architecture definition such as the above:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Draw Layer Blocks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\node[canvas is zy plane at x=0] (temp) at (-3,0,0) {\includegraphics[width=8cm,height=8cm]{cats.jpg}};
% conv1_1,conv1_2,%pool1
\pic[shift={(0,0,0)}] at (0,0,0) {RightBandedBox={name=cr1,caption=conv1,%
        xlabel={{"64","64"}},zlabel=I,fill=\ConvColor,bandfill=\ConvReluColor,%
        height=40,width={2,2},depth=40}};
\pic[shift={(0,0,0)}] at (cr1-east) {Box={name=p1,%
        fill=\PoolColor,opacity=0.5,height=35,width=1,depth=35}};
% conv2_1,conv2_2,pool2
\pic[shift={(2,0,0)}] at (p1-east) {RightBandedBox={name=cr2,caption=conv2,%
        xlabel={{"64","64"}},zlabel=I/2,fill=\ConvColor,bandfill=\ConvReluColor,%
        height=35,width={3,3},depth=35}};
\pic[shift={(0,0,0)}] at (cr2-east) {Box={name=p2,%
        fill=\PoolColor,opacity=0.5,height=30,width=1,depth=30}};


Do you not need these images for publication in a LaTeX document, but still want to use them? That's easy; just use a cloud-based Latex editor like Overleaf to generate the image and save it to your computer from there. Now use it in your blog post or wherever else you want it.

 
Related:


Sign Up

By subscribing you accept KDnuggets Privacy Policy