In Keras, a neural network visualization function plot is provided, and the visualization results can be saved locally. Plot use is as follows:
From Keras.utils.visualize_util import plot
plot (model, to_file= ' model.png ')
Note: The author uses the Keras version is 1.0.6, if is python3.5
From
keras.utils
import
plot_model
plot_model (model,to_file= ' model.png ')
However, this feature relies on the Graphviz module and the Pydot module, so you need to install the two modules first and install the Graphviz software itself (the version I installed is 2.38).
Second, the installation step command line input pip install Graphviz install Graphviz software. Official website address for http://www.graphviz.org/decompression version: Configure environment variables. Add Graphviz-2.38\release\bin in the installation directory to the PATH environment variable installation version: Install the MSI command line to enter PIP install pydot==1.1.0 Note: The Pydot for the installation of version 1.1.0 is required here because the Find_graphviz function is deprecated in the latest version (up to 2016.8 the latest version number is 1.2.x) and is used with an error
Third, test methods use the following script
# encoding:utf-8 "" "" "" "
import numpy as NP from
keras.models import sequential
from Keras.layers.core import dense, activation from
keras.optimizers import SGD from
keras.utils import np_utils From
keras.utils.visualize_util import plot
def run ():
# Build Neural network
model = sequential ()
Model.add (Dense (4, input_dim=2, init= ' uniform '))
Model.add (Activation (' Relu '))
model.add (Dense (2, init= ' Uniform '))
Model.add (Activation (' sigmoid '))
SGD = SGD (lr=0.05, decay=1e-6, momentum=0.9, nesterov=true)
model.compile (loss= ' binary_crossentropy ', OPTIMIZER=SGD, metrics=[' accuracy ')
# Neural Network visualization
plot (model, to_file= ' model.png ')
if __name__ = = ' __ Main__ ':
run ()
Run the example, the error will be displayed: No module named ' Keras.utils.visualize_util '
Pip Install pydot==1.1.0 This method is for Python2,
but Python3 is not, because the Python3 installed 1.2. There is a change in the version, where the visualization needs to be visualize_ Util such an API, but in 1.2, this API was canceled, so python3 users should install the Pydot_ng command line
Pip install pydot_ng
Pip install Pydot
Import NumPy as NP
from keras.models import sequential
to Keras.layers.core import dense, activation from
K Eras.optimizers Import SGD
keras.utils import np_utils from
keras.utils.vis_utils import Plot_model
def run ():
# Build Neural network
model = sequential ()
model.add (Dense (4, input_dim=2, kernel_initializer= ' uniform '))
Model.add (Activation (' Relu '))
model.add (Dense (2, kernel_initializer= ' Uniform '))
Model.add ( Activation (' sigmoid ')
sgd = SGD (lr=0.05, decay=1e-6, momentum=0.9, nesterov=true)
model.compile (loss= ' Binary_crossentropy ', OPTIMIZER=SGD, metrics=[' accuracy ']
# Neural network visualization
Plot_model (model, to_file= ' Model.png ')
if __name__ = = ' __main__ ':
run ()
Note: Now the visual module is not called: keras.utils.visualize_util, changed to Keras.utils.vis_utils, so the introduction of the time to pay special attention.
The following code demonstrates creating a diagram:
Import Pydot g = Pydot. Dot (graph_type= ' graph ') G.add_node (Pydot. Node (str (0), fontcolor= ' Transparent ')) for I in range (5): G.add_node (Pydot. Node (STR (i + 1)) G.add_edge (Pydot). Edge (str (0), str (i + 1))) for J in Range (5): G.add_node (Pydot. Node (Str (j + 1) + ' 0 ' + str (i + 1)) G.add_edge (Pydot. Edge (Str (j + 1) + ' 0 ' + str (i + 1), str (j + 1)) g.write_png (' C:/ch02_fig2-9_graph.png ', prog= ' Neato ')