安装keras之后导入tensorflow报错 ImportError: cannot import name 'abs' 解决方法

2018.10.8 问题描述:

        在tensorflow(gpu版,1.7.0)可以正常import的情况下,安装了keras(conda install keras)之后,此时无论import keras还是import tensorflow均提示错误信息:

File "/home/huo/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/keras/backend/__init__.py", line 22, in
    from tensorflow.python.keras._impl.keras.backend import abs
ImportError: cannot import name 'abs'

       再卸载keras还是不能导入tensorflow,猜测是keras与tensorflow版本不兼容并且改变了tensorflow的路径导致的,网上的解决方案大多是重新移除和安装tensorflow以及keras。因为不想完全重新安装tensorflow,尝试升级tensorflow与keras到最新版本,即tensorflow1.11.0和keras2.2.4:

pip install --upgrade tensorflow-gpu

pip install --upgrade keras

      问题解决。

 

2018.10.9更新:

       今天在运行加载vgg预训练模型参数时报错:

2018-10-09 15:20:41.537464: E tensorflow/stream_executor/cuda/cuda_dnn.cc:343] Loaded runtime CuDNN library: 7.0.5 but source was compiled with: 7.2.1.  CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library.  If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.

       猜想可能与昨天将tensorflow升级到1.11.0有关,导致cudnn版本不匹配,遂将tensorflow版本回滚至1.10.0:

pip install tensorflow_gpu==1.10.0 

       问题解决。

     

       也看到有不用更改tensorflow版本而建立cudnn软连接的方法(https://blog.csdn.net/MacwinWin/article/details/80236111),由于本地没有下载cudnn7.2.1。所以没有尝试。

你可能感兴趣的:(tensorflow)