Python keras和pydot中的plot_模型问题

Python keras和pydot中的plot_模型问题,python,keras,graphviz,pydot,Python,Keras,Graphviz,Pydot,我读过类似的问题——我的错误似乎不同,因为提出的解决方案不能解决我的问题 我在绘制keras模型的图形时遇到问题 我已经使用自制软件安装了graphviz二进制文件 我已经使用pip安装了graphviz python包装器和pydot(也尝试使用conda,因为这在过去似乎是个问题) 使用python 3.5 运行: 来自keras.utils导入plot\u模型 plot_model(cnn_model,to_file='cnn_model.png') 我得到一个错误: ImportErro

我读过类似的问题——我的错误似乎不同,因为提出的解决方案不能解决我的问题

我在绘制keras模型的图形时遇到问题

我已经使用自制软件安装了graphviz二进制文件

我已经使用pip安装了graphviz python包装器和pydot(也尝试使用conda,因为这在过去似乎是个问题)

使用python 3.5

运行:

来自keras.utils导入plot\u模型
plot_model(cnn_model,to_file='cnn_model.png')

我得到一个错误:

ImportError:导入pydot失败。您必须安装pydot和 用于
pydotprint
工作的图形

与跟踪:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in _check_pydot()
     26         # so no specific class can be caught.
---> 27         raise ImportError('Failed to import pydot. You must install pydot'
     28                           ' and graphviz for `pydotprint` to work.')

AttributeError: 'NoneType' object has no attribute 'Dot'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-450-82ff54d9260b> in <module>()
      1 from keras.utils import plot_model
----> 2 plot_model(cnn_model, to_file='cnn_model.png')

/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in plot_model(model, to_file, show_shapes, show_layer_names, rankdir)
    133     if not extension:
    134         extension = 'png'
--> 135     else:
    136         extension = extension[1:]
    137     dot.write(to_file, format=extension)

/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in model_to_dot(model, show_shapes, show_layer_names, rankdir)
     54     dot.set('rankdir', rankdir)
     55     dot.set('concentrate', True)
---> 56     dot.set_node_defaults(shape='record')
     57 
     58     if isinstance(model, Sequential):

/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in _check_pydot()
     29 
     30 
---> 31 def model_to_dot(model,
     32                  show_shapes=False,
     33                  show_layer_names=True,
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in_check_pydot()
26#因此无法捕获特定的类。
--->27 raise ImportError('导入pydot失败。必须安装pydot'
“28”和“pydotprint”的图形表示“工作”。)
AttributeError:“非类型”对象没有属性“点”
在处理上述异常期间,发生了另一个异常:
ImportError回溯(最近一次呼叫最后一次)
在()
1从keras.utils导入plot_模型
---->2 plot_model(cnn_model,to_file='cnn_model.png')
/plot_模型中的Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py(模型、to_文件、显示_形状、显示_层名称、rankdir)
133如果不是扩展:
134扩展名='png'
-->135.其他:
136扩展=扩展[1:]
137点写入(到_文件,格式=扩展名)
/模型点中的Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py(模型、显示形状、显示图层名称、rankdir)
54点集('rankdir',rankdir)
55点整定('浓缩',真)
--->56点。设置节点默认值(shape='record')
57
58如果存在(型号,顺序):
/Users/jusjosgra/anaconda/lib/python3.5/site-packages/keras/utils/vis_utils.py in_check_pydot()
29
30
--->31 def型号到dot(型号,
32显示形状=假,
33显示图层名称=真,
我可以独立地成功导入pydot和graphviz


keras和graphviz之间似乎有一段错误历史。有没有解决方案的想法?

错误消息不明确:当成功导入
pydot
(或模块
vis_utils
中提到的任何分叉)但调用
pydot.Dot.create
失败时,也可能引发异常。来源:

方法
pydot.Dot.create
尝试调用可执行文件
Dot
(由GraphViz安装):

如果
dot
不在环境的
PATH
变量中,则它对
pydot
不可见,尽管它存在于机器上

在Python解释器中导入包意味着它们可以在
站点包
下使用,也可以在开发模式下安装的任何地方使用(例如,使用
Python setup.py develope
,或使用
pip install-e.
)。GraphViz的可执行文件是否在路径上是另一个问题

此外,Python包
graphviz
pydot
无关,通过
pydot
使用graphviz时不需要它。有关此问题的更多信息,请参阅:

我用


sudo apt get install graphviz

我使用了“
conda install graphviz
”它解决了问题。

您的回答为我提供了解决问题的方向。我在mac上,首先我尝试了
brew安装graphviz
,但没有成功。然后我执行了
brew链接——覆盖graphviz
,一切都正常!
def _check_pydot():
    try:
        # Attempt to create an image of a blank graph
        # to check the pydot/graphviz installation.
        pydot.Dot.create(pydot.Dot())
    except Exception:
        # pydot raises a generic Exception here,
        # so no specific class can be caught.
        raise ImportError('Failed to import pydot. You must install pydot'
                          ' and graphviz for `pydotprint` to work.')