forked from amueller/introduction_to_ml_with_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_nn_graphs.py
116 lines (81 loc) · 3.43 KB
/
plot_nn_graphs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def plot_logistic_regression_graph():
import graphviz
lr_graph = graphviz.Digraph(node_attr={'shape': 'circle', 'fixedsize': 'True'},
graph_attr={'rankdir': 'LR', 'splines': 'line'})
inputs = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_0")
output = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_2")
for i in range(4):
inputs.node("x[%d]" % i, labelloc="c")
inputs.body.append('label = "inputs"')
inputs.body.append('color = "white"')
lr_graph.subgraph(inputs)
output.body.append('label = "output"')
output.body.append('color = "white"')
output.node("y")
lr_graph.subgraph(output)
for i in range(4):
lr_graph.edge("x[%d]" % i, "y", label="w[%d]" % i)
return lr_graph
def plot_single_hidden_layer_graph():
import graphviz
nn_graph = graphviz.Digraph(node_attr={'shape': 'circle', 'fixedsize': 'True'},
graph_attr={'rankdir': 'LR', 'splines': 'line'})
inputs = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_0")
hidden = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_1")
output = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_2")
for i in range(4):
inputs.node("x[%d]" % i)
inputs.body.append('label = "inputs"')
inputs.body.append('color = "white"')
hidden.body.append('label = "hidden layer"')
hidden.body.append('color = "white"')
for i in range(3):
hidden.node("h%d" % i, label="h[%d]" % i)
output.node("y")
output.body.append('label = "output"')
output.body.append('color = "white"')
nn_graph.subgraph(inputs)
nn_graph.subgraph(hidden)
nn_graph.subgraph(output)
for i in range(4):
for j in range(3):
nn_graph.edge("x[%d]" % i, "h%d" % j)
for i in range(3):
nn_graph.edge("h%d" % i, "y")
return nn_graph
def plot_two_hidden_layer_graph():
import graphviz
nn_graph = graphviz.Digraph(node_attr={'shape': 'circle', 'fixedsize': 'True'},
graph_attr={'rankdir': 'LR', 'splines': 'line'})
inputs = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_0")
hidden = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_1")
hidden2 = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_2")
output = graphviz.Digraph(node_attr={'shape': 'circle'}, name="cluster_3")
for i in range(4):
inputs.node("x[%d]" % i)
inputs.body.append('label = "inputs"')
inputs.body.append('color = "white"')
for i in range(3):
hidden.node("h1[%d]" % i)
for i in range(3):
hidden2.node("h2[%d]" % i)
hidden.body.append('label = "hidden layer 1"')
hidden.body.append('color = "white"')
hidden2.body.append('label = "hidden layer 2"')
hidden2.body.append('color = "white"')
output.node("y")
output.body.append('label = "output"')
output.body.append('color = "white"')
nn_graph.subgraph(inputs)
nn_graph.subgraph(hidden)
nn_graph.subgraph(hidden2)
nn_graph.subgraph(output)
for i in range(4):
for j in range(3):
nn_graph.edge("x[%d]" % i, "h1[%d]" % j, label="")
for i in range(3):
for j in range(3):
nn_graph.edge("h1[%d]" % i, "h2[%d]" % j, label="")
for i in range(3):
nn_graph.edge("h2[%d]" % i, "y", label="")
return nn_graph