Keras画出网络(拓扑)结构

 在linux环境下:使用sudo apt-get install graphviz即可;

谈一谈windows的用法:

Keras中显示网络拓扑结构的包在utils中的plot_model,

from keras.utils import plot_model
# visualize model layout with pydot_ng
plot_model(model, to_file='./model2.png', show_shapes=True)

但是需要依赖包: 

pip install pydot-ng
pip install graphviz
pip install pydot

 然后安装graphviz的客户端:

Keras画出网络(拓扑)结构_第1张图片

安装时需要将路径添加到系统环境变量PATH中。

Keras画出网络(拓扑)结构_第2张图片

Keras画出网络(拓扑)结构_第3张图片

因为你添加到系统环境变量中去了,所以大概率你需要重启。这个重启不一定是系统,还有可能是Jupyter环境。你先重启软件,不行,再重启一下系统。如果是Pycharm你可以像我一样在代码中添加一句:

import os

os.environ["PATH"] += os.pathsep + 'C:/Program Files/Graphviz/bin/'
from keras.layers import Input, Dense, Lambda, Reshape
from keras.models import Model
from keras import backend as K


# defining the key parameters
batch_size = 100
original_dim = 784
latent_dim = 2
intermediate_dim = 256
epsilon_std = 1.0


def sampling(args: tuple):
    # we grab the variables from the tuple
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(K.shape(z_mean)[0], latent_dim), mean=0.,
                              stddev=epsilon_std)
    return z_mean + K.exp(z_log_var / 2) * epsilon


# input to our encoder
x = Input(shape=(original_dim,), name="input")
# intermediate layer
h = Dense(intermediate_dim, activation='relu', name="encoding")(x)
# defining the mean of the latent space
z_mean = Dense(latent_dim, name="mean")(h)
# defining the log variance of the latent space
z_log_var = Dense(latent_dim, name="log-variance")(h)
# note that "output_shape" isn't necessary with the TensorFlow backend
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
# defining the encoder as a keras model
encoder = Model(x, [z_mean, z_log_var, z], name="encoder")
# print out summary of what we just did
encoder.summary()

# visualize model layout with pydot_ng
from keras.utils import plot_model
import os

os.environ["PATH"] += os.pathsep + 'C:/Program Files/Graphviz/bin/'
plot_model(encoder, to_file='./model2.png', show_shapes=True)

Keras画出网络(拓扑)结构_第4张图片

 

你可能感兴趣的:(环境配置,keras,网络,神经网络,神经网络拓扑可视化)