2020-12-26

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:

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值