Win 10 解决运行Keras中的plot_model()函数时的报错,ImportError: 'Failed to import pydot. You must `pip install pydot` and install graphviz (https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.'

      Keras.__version__ == 2.4.3

       plot_model()是将Keras中的神经网络的模型进行可视化处理的函数。当win 10 中运行时,会出现以下报错:ImportError: 'Failed to import pydot. You must `pip install pydot` and install graphviz https://graphviz.gitlab.io/download/), ', 'for `pydotprint` to work.'。主要原因是:pydot_ng找不到graphviz中的一些可执行程序,即一些.exe文件。

        看了其他许多博客后,都无法解决。在这些博客的基础上,和查看源代码后,我总结了以下的解决方法

START:

     STEP 1. 下载并安装graphviz 2.38注意:设置安装路径为"C:\Program Files\att\Graphviz\bin",并将该路径添加到系统的Path环境变量中。软件下载链接为 https://softpedia-secure-download.com/dl/b07f24905c8ad73c39308d61e31f96dc/5fe6c2c9/100124651/software/other_tools/graphviz-2.38.msi

      STEP 2. cmd命令行中运行pip install pydot_ng,来安装pydot_ng包。

END.

测试代码借鉴 https://blog.csdn.net/qq_34564612/article/details/78851001?utm_medium=distribute.pc_relevant.none-task-blog-searchFromBaidu-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-searchFromBaidu-2.control 中的代码。

from keras.models import Model
from keras.layers import LSTM, Activation, Input
import numpy as np
from keras.utils.vis_utils import plot_model

data_dim = 1
timesteps = 12
num_classes = 4

inputs = Input(shape=(12,1))
lstm1 = LSTM(32, return_sequences=True)(inputs)
lstm2 = LSTM(4 , return_sequences=True)(lstm1)
outputs = Activation('softmax')(lstm2)
model = Model(inputs=inputs,outputs=outputs)
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, timesteps, num_classes))

x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, timesteps, num_classes))

model.fit(x_train, y_train,
          batch_size=64, epochs=5,
          validation_data=(x_val, y_val))
#模型可视化
plot_model(model, to_file='model.png')
x = np.arange(12).reshape(1,12,1)
a = model.predict(x,batch_size=64)
print a

来自 <https://blog.csdn.net/qq_34564612/article/details/78851001?utm_medium=distribute.pc_relevant.none-task-blog-searchFromBaidu-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-searchFromBaidu-2.control>

 

Result:

 

 

 

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐