主要步骤
- 安装
tensorflow
,tensorflow 2.0
之后已经不再区分CPU和GPU版本; - 安装cuda;
- 添加cudnn库.
tensorflow安装
在正式安装前, 检查是否有msvcp140_1.dll
, 如果有安装Visual Studio 2019的话是默认有的, 如果没有或不确定可以从微软官网下载安装, 选择Visual Studio 2015, 2017 and 2019
版本即可.
tensorflow可以直接利用pip install tensorflow
进行安装, 但为了便于管理, 建议在conda
中安装.
conda可以选择anaconda, 从官网下载安装即可.
在最后可以选择是否添加到环境变量, 如果添加到环境变量中, 可以直接从终端(cmd或powershell)打开conda.
然后利用conda创建一个tensorflow的环境. 激活后安装tensorflow
#创建名为`tf2`的环境, python版本为3.8(不指定会启用base中的版本)
conda create -n tf2 python=3.8
conda activate tf2
pip install --upgrade pip
pip install tensorflow
检查是否安装成功
import tensorflow as tf
print(tf.__version__)
2020-09-17 09:50:39.712664: W tensorflow/stream_executor/platform/default/ http:// dso_loader.cc:59 ] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found 2020-09-17 09:50:39.718061: I tensorflow/stream_executor/cuda/ http:// cudart_stub.cc:29 ] Ignore above cudart dlerror if you do not have a GPU set up on your machine. 2.3.0
在最后一行, 显示了当前的tensorflow版本. 需要留意的是第一行输出的warning: 缺少cudart64_101.dll
. 这个信息在后面会使用到.
cuda安装
cuda可以直接从官网下载, 版本的选择可以参考上一个步骤中的warning信息, cudart64_101.dll
表示当前电脑为64位, 需要cuda10.1
. 下载对应版本即可.
安装过程没有什么复杂的, 直接下一步也可以. 但对于不希望安装在默认盘的同志, 有一些细节需要注意
- 第一次出现的路径选择是选择临时提取路径, 不要被骗了(你猜我被骗了吗?)
- 在安装程序中选择自定义安装, 否则连路径选择的机会都没有
- 在安装选项中, CUDA是必选, 其他的看需求(可以不用)
- 最后才是安装路径的选择
然后安装即可. 命令行中测试:
#测试
nvcc -V
有输出即可:
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2019 NVIDIA Corporation Built on Sun_Jul_28_19:12:52_Pacific_Daylight_Time_2019 Cuda compilation tools, release 10.1, V10.1.243
添加cudnn库
首先进行测试:
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices)
这里会输出许多信息:
2020-09-17 10:20:50.788436: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library cudart64_101.dll 2020-09-17 10:20:52.545573: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library nvcuda.dll 2020-09-17 10:20:52.569364: I tensorflow/core/common_runtime/gpu/
http://
gpu_device.cc:1716
] Found device 0 with properties: pciBusID: 0000:01:00.0 name: Quadro K600 computeCapability: 3.0 coreClock: 0.8755GHz coreCount: 1 deviceMemorySize: 1.00GiB deviceMemoryBandwidth: 26.55GiB/s 2020-09-17 10:20:52.593774: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library cudart64_101.dll 2020-09-17 10:20:52.614110: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library cublas64_10.dll 2020-09-17 10:20:52.631976: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library cufft64_10.dll 2020-09-17 10:20:52.639591: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library curand64_10.dll 2020-09-17 10:20:52.648180: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library cusolver64_10.dll 2020-09-17 10:20:52.667679: I tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:48
] Successfully opened dynamic library cusparse64_10.dll 2020-09-17 10:20:52.677125: W tensorflow/stream_executor/platform/default/
http://
dso_loader.cc:59
] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found 2020-09-17 10:20:52.692602: W tensorflow/core/common_runtime/gpu/
http://
gpu_device.cc:1753
] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at
https://www.
tensorflow.org/install/
gpu
for how to download and setup the required libraries for your platform. Skipping registering GPU devices...
其中info
级别的信息可以忽略, 重点关注warning
, 可以看到, 缺少cudnn64_7.dll
动态库, 这表示我们需要下载cudnn
库, 其版本为7.
然后从官网下载对应版本, 即对应cuda10.1
版本的cudnn
库, 其大版本应该为7.
下载后解压, 可以直接把里面的文件拷贝到cuda安装目录下. 但更推荐直接添加环境变量, 指向<你的cudnn库路径>cudnn-10.1-windows10-x64-v7.6.5.32cudabin
再次运行
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices)
查看可用的GPU设备