top of page
Writer's pictureThe Tech Platform

How to Visualize a Neural Network in Python

Neural networks are self-upgrading capacities that guide contributions to the right results. We can then put another contribution to the capacity, where it will foresee a result in light of its capacity with the preparation information. In this article, we will guide you on How to Visualize a Neural Network in Python.



There are many tools you can use for visualizing the architecture of a neural network. Some of the best tools for visualizing a neural network are:

  1. Net2Vis

  2. VisualKeras

  3. drawconvent

  4. NNSVG

  5. PlotNeuralNet

  6. Tensorboard

  7. Graphviz

  8. TensorSpace


Visualize Neural Network in Python using Graphviz

Graphviz is a python module that is open-source graph visualization software. It represents structural information as diagrams of abstract graphs and networks. You just need to provide the description of the graph and this will automatically read and create the graph.


A simple graph with Graphviz

1. Import module

2. Create an object of diagraph

3. Add node() and edge() into graph object

4. Save the source code with the render() object.


Code:

# import module
from graphviz import Digraph
  
# instantiating object
dot = Digraph(comment='A Round Graph')
  
# Adding nodes
dot.node('A', 'Rishi')
dot.node('B', 'Mohit')
dot.node('C', 'Shayam')
dot.node('D', 'Sophiya')
  
# Adding edges
dot.edges(['AB', 'AC', 'AD'])
dot.edge('B', 'C', constraint = 'false')
dot.edge('C', 'D', constraint = 'false')
  
# saving source code
dot.format = 'png'
dot.render('Graph', view = True)

Output:

Graph.png

We can check the generated source code with dot.source methods

print(dot.source)

Output:

// A Round Graph
diagraph {
    A [label = Rishi]
    B [label = Mohit]
    C [label = Shayam]
    D [Label = Sophiya]
    A -> B
    A -> C
    A -> D
    B -> C [constraint = false]
    C -> D [constraint = false]
}

How to visualize a neural network with Graphviz


1. Create a digraph object.

2. Define the direction of the graph using rankdir.

3. Create a subgraph with the following things:

Set color.

Set node properties.

Set the Level of the subgraph

4. Create the edge between the object with (->).


This source code must be saved in a .txt file(myfile.txt) and run `dot -Tpng -O myfile.txt` from the command line to get a .png figure with the diagram.


Example 1:

digraph G {

        rankdir=LR
    splines=line
        
        node [fixedsize=true, label=""];

        subgraph cluster_0 {
        color=white;
        node [style=solid,color=blue4, shape=circle];
        x1 x2 x3 x4;
        label = "layer 1 (Input layer)";
    }

    subgraph cluster_1 {
        color=white;
        node [style=solid,color=red2, shape=circle];
        a12 a22 a32;
        label = "layer 2 (hidden layer)";
    }

    subgraph cluster_2 {
        color=white;
        node [style=solid,color=seagreen2, shape=circle];
        O;
        label="layer 3 (output layer)";
    }

        x1 -> a12;
        x1 -> a22;
        x1 -> a32;
        x2 -> a12;
        x2 -> a22;
        x2 -> a32;
        x3 -> a12;
        x3 -> a22;
        x3 -> a32;
    x4 -> a12;
        x4 -> a22;
        x4 -> a32;
    

        a12 -> O
        a22 -> O
        a32 -> O

}

Run this into the terminal:

dot -Tpng -O myfile.txt

Output:















Resource: Geeksofgeeks, thecleverprogrammer


The Tech Platform

0 comments

コメント


bottom of page