使用GPU运行TensorFlow
System Information
- OS: Windows 10
- GPU: NVIDIA GeForce 930M (Compute Capability = 5.0)
- CUDA/cuDNN version: 10
- Python version: 3.7 (Use Anaconda3 env)
- TensorFlow version: tensorflow-gpu 1.13.1
Step1: 检查硬件
硬件要求:NVIDIA® GPU card with CUDA® Compute Capability 3.5 or higher.
1. 确认电脑配备GPU
- 打开 设备管理器 (Device Manager)
- 展开 显示适配器 (Display adapters)
-
确认及查看 GPU型号 (我的是NVIDIA GeForce 930M)
2. 查看GPU的计算能力(需>=3.5)
See the list of CUDA-enabled GPU cards.
e.g. NVIDIA GeForce 930M是
Step2: 安装软件
软件要求:
一些建议:
1. NVIDIA® GPU drivers:更新driver
设备管理器 -> GPU 右键 属性 -> Driver 栏 -> Update Driver
2. CUDA Toolkit:10.0 (9.0或以上)
安装:https://developer.nvidia.com/cuda-toolkit-archive
3. cuDNN
Download cuDNN v7.6.1 (June 24, 2019), for CUDA 10.0
All versions here
Step3: 添加环境变量
将CUDA, CUPTI, cuDNN 路径 加到 系统环境变量PATH 中。
Terminal command:
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras\CUPTI\libx64;%PATH%
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include;%PATH%
SET PATH=C:\tools\cuda\bin;%PATH%
Step4: 安装 tensorflow-gpu (in Anaconda Prompt)
1. 创建一个python3.7的环境
教程:https://www.jianshu.com/p/64b94a6a7e98
2. 激活环境
conda activate [env_name]
3. 安装tensorflow-gpu1.13.1 (2.0.0-beta1也可以)
先卸载 tensorflow (cpu): pip uninstall tensorflow
再安装 tensorflow-gpu 1.13.1: pip install tensorflow-gpu==1.13.1
或安装 tensorflow-gpu 2.0.0-beta1: pip install tensorflow-gpu==2.0.0-beta1
Step5: 测试
'''test.py'''
import tensorflow as tf
print("GPU Available: ", tf.test.is_gpu_available())
# 若成功,会返回:
# GPU Available: True
在Terminal中运行出现的问题
返回 GPU Available:False
明明有GPU, 为什么会返回False呢?
往上看错误信息:
CondaHTTPError: HTTP 000 CONNECTION FAILED for url [https://repo.anaconda.com/pkgs/main/win-64/repodata.json.bz2](https://repo.anaconda.com/pkgs/main/win-64/repodata.json.bz2)
解决方法:Update Driver (见Step2.1)
之后在Terminal中运行成功:
用Pycharm运行出现的问题
1. Configuration & Interpreter Setting 环境配置
PyCharm: Configure a Conda virtual environment
PyCharm: Setting an existing project interpreter
2. Import Error
from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.
解决方法: (Refer to jianshu passage)
-
将 Anaconda3的路径 添加到 环境变量Path 中:
- 重启PyCharm
3. CUDA Error
E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit:
CUDA_ERROR_UNKNOWN: unknown error
解决方法 (Refer to Guilherme Melo's advice):
- 打开 Anaconda Prompt:
source activate [env_name]
conda install -c conda-forge conda-wrappers
- 改变Pycharm中 Interpreter path:
将原本的<env>/python.exe
改为<env>/Scripts/wrappers/conda/python.bat
最后终于在PyCharm中运行成功啦:
Reference:
https://www.tensorflow.org/install/gpu
https://www.tensorflow.org/guide/using_gpu
有问题欢迎留言讨论