我在 Windows 10 上使用 Anaconda Python 2.7
我正计划进行 Keras 可视化,所以(当 spyder 打开时)我打开了 Anaconda 命令提示符并 pip 安装了 graphviz 和 pydot。现在,当我尝试运行以下命令时:
from keras.models import Sequential
或任何类型的“来自 keras”。 ,我得到错误:
ImportError: cannot import name gof
我已经卸载并重新安装了 Keras、Graphviz 和 pydot。我正在使用 theano 的开发版本。我找不到修复方法。
P.S
如果我卸载 graphviz 和 pydot,keras 又可以工作了
编辑
卸载 anaconda 并重新安装包括 theano、keras、graphviz 和 pydot 后,我现在收到以下错误:
from keras.utils.visualize_util import plot
Using Theano backend.
Using gpu device 0: GeForce GTX 970M (CNMeM is disabled, cuDNN not available)
Traceback (most recent call last):
File "<ipython-input-1-65016ddab3cd>", line 1, in <module>
from keras.utils.visualize_util import plot
File "C:\Anaconda2\lib\site-packages\keras\utils\visualize_util.py", line 8, in <module>
raise RuntimeError('Failed to import pydot. You must install pydot'
RuntimeError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.
我用了
pip install graphviz
和pip install git+https://github.com/nlhepler/pydot.git
错误消息有点误导,如您所见here。问题是没有安装 graphviz
但是你提到 graphviz 是使用 pip 安装的。这也是一种误导,因为 graphviz 包只是一个 python 包装器,而 graphviz 二进制文件 必须单独安装才能使 python 包装器工作。
如果你使用的是Anaconda环境,你最好通过
pydotplus
安装graphviz
和conda install
。
conda install graphviz
conda install pydotplus
注意: 你最好将你的 Keras 更新到最新版本(2.0.9+),它会自动检查并选择使用
pydotplus
,pydot-ng
,pydot
中的哪一个。 pydot-ng
很久没人维护了,只支持py3.4-和py2.7.
我所做的被跟踪。
import keras
import pydotplus
from keras.utils.vis_utils import model_to_dot
keras.utils.vis_utils.pydot = pydot
plot_model(your_model_name, to_file='model.png')
这对我有用。 在 mac 上 Anaconda python=3.6.8
Keras 2.0.6 寻找 pydot-ng(维护得更好),如果找不到,则回退到 pydot。我通过从 source 安装 pydot-ng 解决了这个问题。
我有同样的问题。我在 Ubuntu 上使用 Anaconda python。但似乎 Keras 使用系统的 python 而不是 Anaconda python。最初,我使用 conda 安装了 pydot 和 graphviz。当我在系统的 python 中安装 pydot 和 graphviz(使用 /usr/bin/pip install pydot)时,它工作正常。
在 osx 中通过 brew 安装 graphviz
brew install graphviz
,对于 ubuntu 使用 apt-get install graphviz
,不需要通过 pip 安装 graphviz。
我的 Keras(没有 anaconda)也有类似的问题。我已经用这种方式解决了我的问题
sudo pip install pydot
sudo pip install graphviz
sudo add-apt-repository ppa:gviz-adm/graphviz-dev
sudo apt-get update
sudo apt-get install graphviz-dev
1)Conda 安装 graphviz
2)pip 安装 graphviz
3)点安装pydot
然后:
import os
os.environ["PATH"] += os.pathsep + AppData\\Local\\Continuum\\anaconda3\\envs\\tensorflow\\Library\\bin\\graphviz'
对于 Mac 上的 Anaconda:
使用 conda run 安装这个包:
conda install -c anaconda graphviz
安装
graphviz
到系统。从 here 下载软件包,或在 Mac 上:
brew install graphviz
安装 python pydot-ng 和 graphviz 包装器。
pip install pydot-ng graphviz
conda install -c anaconda pydot-ng #Anaconda user
在您的代码中使用
pydot-ng
import pydot_ng as pydot
如果 Keras visualization utils 仍然使用 pydot,请尝试将
import pydot
更改为 import pydot_ng as pydot
在 visualize_util.py
以下在
jupyter/tensorflow-notebook
docker 容器中运行的 jupyter notebook 中运行。
!conda install -y graphviz pydotplus
import pydotplus
import keras.utils
keras.utils.vis_utils.pydot = pydotplus
keras.utils.plot_model(your_model_name, to_file='model.png', show_shapes=True)
需要告诉keras使用pydotplus
如果您是通过 pip 安装的,则与 pydot 或 graphviz 无关。
你应该去下载 graphviz brinary 并安装。
不要忘记将 bin 文件夹添加到您的路径中:C:/Program Files (x86)/Graphviz2.38/bin/
然后关闭并重新打开您的控制台。
安装好pydot和graphviz,并将graphviz加入路径后,即可重启IDE或终端。 看这里。
导入keras 导入pydotplus 从 keras.utils.vis_utils 导入 model_to_dot keras.utils.vis_utils.pydot = pydot
tf.keras.utils.plot_model( 模型, show_shapes = 真, show_dtype = 真, show_layer_activations = 真 )