conda update conda
pip install tf-nightly-gpu-2.0-preview
conda install https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudnn-7.3.1-cuda10.0_0.tar.bz2
conda install https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudatoolkit-10.0.130-0.tar.bz2
环境准备与依赖安装
环境配置说明:
- 首先更新 conda 基础环境
- 目标安装 TensorFlow 2.0 最新版
- 需配合 cudnn 7.3.1 和 cudatoolkit-10.0 版本,建议下载后本地安装以确保兼容性
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/cudnn-7.3.1-cuda10.0_0.tar.bz2
conda install cudnn-7.3.1-cuda10.0_0.tar.bz2
常见错误及解决方案汇总
依赖包冲突问题
ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
旧版本依赖复杂导致无法清晰删除,此时应忽略旧版本约束进行升级。解决办法:pip install tf-nightly-gpu-2.0-preview --ignore-installed wrapt
NumPy 版本兼容性问题
若提示 numpy 存在旧版本,可使用 pip 卸载 numpy,直到系统提示无已安装包为止,随后重新安装最新兼容版本
CUDA 驱动不匹配问题
tensorflow.python.framework.errors_impl.InternalError: cudaGetDevice() failed. Status: CUDA driver version is insufficient for CUDA runtime version
此错误由驱动版本与运行时版本不匹配引起,请前往 NVIDIA 官网下载并安装与上述一致的 CUDA 10.0 驱动程序

安装命令: https://juejin.im/post/5cce44e3f265da036902a89c,然后一路确定,最后使用 watch nvidia-smi
验证安装结果:

功能测试及其他优化
验证测试可用:
import tensorflow as tf
print(tf.__version__)
print(tf.keras.__version__)
if tf.test.is_gpu_available():
device = "/gpu:0"
else:
device = "/cpu:0"
print(device)
减少 TensorFlow 冗余输出信息
TensorFlow的log信息共有四个等级,按重要性递增为:INFO(通知)
tf.compat.v1.logging.set_verbosity('ERROR')
或者采用以下环境变量设置方式
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
TensorFlow 2.0 在 PyCharm 下的代码提示问题
TensorFlow 2.0 使用 Keras 通常通过 tensorflow.keras 调用,但 PyCharm 往往无代码提示。原因是实际 keras 路径位于 tensorflow/python/keras,而程序中 tensorflow 模块下可能缺失 python 目录结构。解决方法如下:
try:
import tensorflow.python.keras as keras
except:
import tensorflow.keras as keras
经此配置,PyCharm 即可正常显示代码提示,且无需在程序运行期间修改代码逻辑。
总结
以上所述是小编给大家介绍的解决Linux Tensorflow2.0安装问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持! 如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!