Teach you step by step how to draw neural network diagrams in Python

  • Next, I will teach you how to use the networkx library in Python to draw a beautiful and standard neural network.
  • Neural networks with different structures will be drawn according to the specified number of layers and nodes.

The networkx library can be used to create and manipulate graph-type data structures, including undirected graphs, directed graphs, weighted graphs, and more.
Neural networks can be viewed as a graph data structure, so they can be created using the networkx library and operated visually.

Simple example: Draw a 2-layer neural network

  1. First, you need to install the networkx library in advance, and then import networkx and matplotlib in the code. Then use DiGraph to create a directed graph G.

  2. The network we want to draw includes 5 nodes. The nodes in the first layer are numbered 1, 2, and the nodes in the second layer are 3, 4, 5. We use add_edge from 1 to 3, 4, 5, and from 2 to 3, 4, 5, connect an edge.

  3. In order to make the drawn image look like a neural network, we need to set the coordinates for these 5 nodes. Create a dictionary pos. The key of the dictionary is the name of the node, and the value of the dictionary is the location of the node.

  4. Finally, use the nx.draw function to draw.
    where G is the graph to be drawn,
    pos is the coordinate of the node in the graph,
    with_labels = True, represents the name of the drawing node
    node_color and edgecolor are the colors of nodes and edges
    linewidths and width are the thickness of nodes and edges
    node_size is the size of the node

# Import networkx and matplotlib in the code
import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph() # Use DiGraph to create a directed graph G

# The network includes 5 nodes
# The node numbers on the first layer are 1 and 2, and the node numbers on the second layer are 3, 4 and 5.
G.add_edge(1, 3) # from 1 to 3
G.add_edge(1, 4) # from 1 to 4
G.add_edge(1, 5) # from 1 to 5
G.add_edge(2, 3) # from 2 to 3
G.add_edge(2, 4) # from 2 to 4
G.add_edge(2, 5) # from 2 to 5

# Create dictionary pos, the key of the dictionary is the name of the node
#The value of the dictionary is the location of the node

# Nodes 1 and 2 are in one column
# 3, 4, 5 in one column
# So set the x coordinates of 1 and 2 to 0; 3, 4, and 5 to 1

#Nodes in the same group can be evenly distributed in the same column
# So we set the y coordinates of 1 and 2 to 0.25 and 0.75
# The y coordinates of 3, 4, and 5 are 0.2, 0.5, and 0.8

# {Node name: (node x coordinate, node y coordinate)}
pos = {<!-- -->
    1: (0, 0.25), # Coordinates of node 1 (0,0.25)
    2: (0, 0.75), # Coordinates of node 2 (0,0.75)
    3: (1, 0.2), # Coordinates of node 3 (1, 0.2)
    4: (1, 0.5), # Coordinates of node 4 (1, 0.5)
    5: (1, 0.8), # The coordinates of node 5 (1, 0.8)
}

# Use nx.draw function to draw
nx.draw(G, #The picture to be drawn
        pos, #Coordinates of nodes in the graph
        with_labels=True, # Draw the name of the node
        node_color='white', #The color of the node
        edgecolors='black', # edge colors
        linewidths=3, #The thickness of the node
        width=2, # The thickness of the edge
        node_size=1000 #The size of the node
        )

plt.show() # Use the show method to display graphics

Customized function, freely draw neural network according to parameters

# Import networkx and matplotlib in the code
import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph() # Use DiGraph to create a directed graph G

# The network includes 5 nodes
# The node numbers on the first layer are 1 and 2, and the node numbers on the second layer are 3, 4 and 5.
G.add_edge(1, 3) # from 1 to 3
G.add_edge(1, 4) # from 1 to 4
G.add_edge(1, 5) # from 1 to 5
G.add_edge(2, 3) # from 2 to 3
G.add_edge(2, 4) # from 2 to 4
G.add_edge(2, 5) # from 2 to 5

# Create dictionary pos, the key of the dictionary is the name of the node
#The value of the dictionary is the location of the node

# Nodes 1 and 2 are in one column
# 3, 4, 5 in one column
# So set the x coordinates of 1 and 2 to 0; 3, 4, and 5 to 1

#Nodes in the same group can be evenly distributed in the same column
# So we set the y coordinates of 1 and 2 to 0.25 and 0.75
# The y coordinates of 3, 4, and 5 are 0.2, 0.5, and 0.8

# {Node name: (node x coordinate, node y coordinate)}
pos = {<!-- -->
    1: (0, 0.25), # Coordinates of node 1 (0,0.25)
    2: (0, 0.75), # Coordinates of node 2 (0,0.75)
    3: (1, 0.2), # Coordinates of node 3 (1, 0.2)
    4: (1, 0.5), # Coordinates of node 4 (1, 0.5)
    5: (1, 0.8), # The coordinates of node 5 (1, 0.8)
}

# Use nx.draw function to draw
nx.draw(G, #The picture to be drawn
        pos, #Coordinates of nodes in the graph
        with_labels=True, # Draw the name of the node
        node_color='white', #The color of the node
        edgecolors='black', # edge colors
        linewidths=3, #The thickness of the node
        width=2, # The thickness of the edge
        node_size=1000 #The size of the node
        )

plt.show()


# Draw the corresponding neural network based on the number of neurons in the input layer, hidden layer, and output layer.
def draw_network_digraph(input_num, hidden_num, output_num):
    G = nx.DiGraph() # Create a graph G

    # Connect the edge between the input layer and the hidden layer
    for i in range(input_num):
        for j in range(hidden_num):
            G.add_edge(i, input_num + j)

    # Connect the edge between the hidden layer and the output layer
    for i in range(hidden_num):
        for j in range(output_num):
            G.add_edge(input_num + i, input_num + hidden_num + j)

    pos = dict() # Calculate the coordinate pos of each node
    # The coordinates of the node, (x, y) are set to:
    # (0,i-input_num/2)
    # (1,i-hidden_num)/2)
    # (2,i-output_num/2)
    # According to the number of nodes in each layer, distribute the nodes from the middle to both sides
    for i in range(0, input_num):
        pos[i] = (0, i - input_num / 2)
    for i in range(0, hidden_num):
        hidden = i + input_num
        pos[hidden] = (1, i - hidden_num / 2)
    for i in range(0, output_num):
        output = i + input_num + hidden_num
        pos[output] = (2, i - output_num / 2)

    # Call nx.draw to draw the neural network
    nx.draw(G, #The picture to be drawn
            pos, #Coordinates of nodes in the graph
            with_labels=False, # Draw the name of the node
            node_color='white', #The color of the node
            edgecolors='black', # edge colors
            linewidths=3, #The thickness of the node
            width=2, # The thickness of the edge
            node_size=1000 #The size of the node
            )


if __name__ == '__main__':
    # Try multiple sets of parameters to draw neural networks with different structures
    draw_network_digraph(3, 5, 2)
    plt.show()
    draw_network_digraph(5, 2, 6)
    plt.show()
    draw_network_digraph(1, 10, 1)
    plt.show()