Conda、Tensorflow 和 Keras 版本不匹配问题

Conda, Tensorflow, and Keras version mismatch issue

当我尝试在我的 Conda 环境中使用 Keras 时出现此错误:

pr_curve_pb = _pr_curve_summary.pb attributeerror: 'module' object has no attribute 'pb'

错误痕迹:

File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/init.py", line 3, in 
from . import utils
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/utils/init.py", line 6, in 
from . import conv_utils
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/utils/conv_utils.py", line 9, in 
from .. import backend as K
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/backend/init.py", line 1, in 
from .load_backend import epsilon
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/backend/load_backend.py", line 90, in 
from .tensorflow_backend import *
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 5, in 
import tensorflow as tf
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/init.py", line 98, in 
from tensorflow_core import *
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow_core/init.py", line 45, in 
from . _api.v2 import compat
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/init.py", line 24, in 
from . import v2
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v2/init.py", line 32, in 
from . import compat
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v2/compat/init.py", line 23, in 
from tensorflow._api.v2.compat import v1
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/init.py", line 24, in 
from . import v2
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow_core/_api/v2/compat/v2/init.py", line 314, in 
from tensorboard.summary._tf import summary
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/summary/init.py", line 25, in 
from tensorboard.summary import v1
File "/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorboard/summary/v1.py", line 46, in 
pr_curve_pb = _pr_curve_summary.pb
AttributeError: module 'tensorboard.plugins.pr_curve.summary' has no attribute 'pb'

我尝试卸载并重新安装,但没有成功。

我找到了解决办法:

尝试使用 Conda 安装这些包 运行

    conda install -c anaconda tensorflow-gpu 
    
    conda install -c anaconda keras

我在最近 conda update --all 之后尝试导入 kerastuner(它也导入 tensorflow)时出现上述错误(conda 不允许我安装 keras-tuner 直到我更新了所有包)。从 github 那里得到 this 很好的建议,它解决了我的问题:

conda remove tensorflow
conda install tensorflow-estimator=2.1
conda install tensorflow-gpu=2.1

如果你重新安装了kerastuner before doing the above, it will no longer be installed. So you will need to follow the [instructions][2] to install kerastuner`(目前推荐的方法之一如下):

我遇到了一个非常相似的错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/runner/.local/lib/python2.7/site-packages/tensorflow/__init__.py", line 99, in <module>
    from tensorflow_core import *
  File "/home/runner/.local/lib/python2.7/site-packages/tensorflow_core/__init__.py", line 36, in <module>
    from tensorflow._api.v1 import compat
  File "/home/runner/.local/lib/python2.7/site-packages/tensorflow_core/_api/v1/compat/__init__.py", line 24, in <module>
    from tensorflow._api.v1.compat import v2
  File "/home/runner/.local/lib/python2.7/site-packages/tensorflow_core/_api/v1/compat/v2/__init__.py", line 322, in <module>
    from tensorboard.summary._tf import summary
  File "/home/runner/.local/lib/python2.7/site-packages/tensorboard/summary/__init__.py", line 25, in <module>
    from tensorboard.summary import v1
  File "/home/runner/.local/lib/python2.7/site-packages/tensorboard/summary/v1.py", line 46, in <module>
    pr_curve_pb = _pr_curve_summary.pb
AttributeError: 'module' object has no attribute 'pb'

这是 Python 2.7 和 TF 1.15.0,在 GitHub CI workflow.

在搜索这个异常时,我在这里找到了这个post,而且 this: AttributeError: module 'tensorboard.plugins.pr_curve.summary' has no attribute 'pb'

这链接到 this comment,建议 pip uninstall tensorflow-tensorboard,因为有旧包 tensorflow-tensorboard 和新包 tensorboard,这可能会弄乱它向上。

在我的例子中,这没有帮助(我得到一个例外,即 tensorflow-tensorboard 没有安装,但 tensorboard 也没有安装),尽管包 tensorboard 存在于 ~/.local/lib/python2.7/site-packages.

对我有用的解决方案很简单:

rm -rf ~/.local/lib/python2.7/site-packages/tensorboard*

然后再安装新的 TF (pip install --user tensorflow==1.15.0)。