
阅读正文之前,请先阅读注意事项。
Tensorflow安装
更新pip
首先需要更新pip,即:
python -m pip install --upgrade pip
如果更新太慢可使用镜像,例如:
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
其中的镜像网址可更换。
安装tensorflow
- 直接安装匹配,使用以下命令。
pip install tensorflow
tensorflow库比较大,这样做往往不能顺利安装完全,因此建议用以下方法。
利用镜像安装tensorflow,命令如下:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow
其中的镜像网址可以更换。
如果还不能解决,建议直接进入清华镜像网址(CPU版本|GPU版本)下载合适的tensorflow安装包,例如下载tensorflow2.4.0-python3.8-win。(这里建议还是下载tensorflow2.3版本,具体原因见Tensorflow GPU软件配置)(百度网盘:TF23)
在windows power shell或cmd命令提示符中,利用cd命令转移到下载的tensorflow安装文件下,例如:下载路径是”C:\Users\XXX”,则输入:
cd C:\Users\XXX
再输入下载命令,安装tensorflow:
pip install tensorflow-2.4.0rc3-cp38-cp38-win_amd64.whl
然后就安装完惹。
另外,由于安装过程中可能需要更新其他包,所以这里依然建议带上镜像,例如:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-2.4.0rc3-cp38-cp38-win_amd64.whl
如果提示”ERROR: Could not install packages due to an EnvironmentError: [WinError 5]”,请参考此处。
Tensorflow GPU配置
硬件要求
支持以下带有 GPU 的设备:
- CUDA® 架构为 3.5、3.7、5.2、6.0、6.1、7.0 或更高的 NVIDIA® GPU 卡。请参阅支持 CUDA® 的 GPU 卡列表。(或点击快速查看支持CUDA® 的桌面显卡或笔记本显卡(仅Geforce和TITAN产品))
- 在配备 NVIDIA® Ampere GPU(CUDA 架构 8.0)或更高版本的系统上,内核已从 PTX 经过了 JIT 编译,因此 TensorFlow 的启动时间可能需要 30 多分钟。通过使用“
export CUDA_CACHE_MAXSIZE=2147483648”增加默认 JIT 缓存大小,即可将此系统开销限制为仅在首次启动时发生(有关详细信息,请参阅 JIT 缓存)。 - 对于 CUDA® 架构不受支持的 GPU,或为了避免从 PTX 进行 JIT 编译,亦或是为了使用不同版本的 NVIDIA® 库,请参阅在 Linux 下从源代码编译指南。
- 软件包不包含 PTX 代码,但最新支持的 CUDA® 架构除外;因此,如果设置了
CUDA_FORCE_PTX_JIT=1,TensorFlow 将无法在旧版 GPU 上加载。(有关详细信息,请参阅应用兼容性。)
软件要求
必须在系统中安装以下 NVIDIA® 软件:
- NVIDIA® GPU 驱动程序:CUDA® 10.1 需要 418.x 或更高版本。
- CUDA® 工具包:TensorFlow 支持 CUDA® 10.1(TensorFlow 2.1.0 及更高版本)(注意是10.1,不能是10.2,我最开始就翻车了……点击这里可以查看tensorflow-gpu与CUDA的对应关系(windows))。
- CUDA® 工具包附带的 CUPTI。
- cuDNN SDK 7.6
- (可选)TensorRT 6.0,可缩短用某些模型进行推断的延迟时间并提高吞吐量。
Windows 设置
请参阅上面列出的硬件要求和软件要求,并阅读适用于 Windows 的 CUDA® 安装指南。
确保安装的 NVIDIA 软件包与上面列出的版本一致。特别是,如果没有 cuDNN64_7.dll 文件,TensorFlow 将无法加载。如需使用其他版本,请参阅在 Windows 下从源代码构建指南。
将 CUDA®、CUPTI 和 cuDNN 安装目录添加到 %PATH% 环境变量中。例如,如果 CUDA® 工具包安装到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1,并且 cuDNN 安装到 C:\tools\cuda,请更新 %PATH% 以匹配路径:
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;%PATH%SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\lib64;%PATH%SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include;%PATH%SET PATH=C:\tools\cuda\bin;%PATH%
下载并安装NVIDIA CUDA Toolkit
下载
首先打开Nvidia控制面板,依次点击帮助→系统信息→组件,显示以下界面。
可以看到当前显卡支持到CUDA 10.2版本。
安装
安装前阅读以下注意事项.
注意: 必须安装驱动程序和工具包(Toolkit),数据自动化系统才能运行。如果尚未安装独立驱动程序,请从NVIDIA CUDA工具包安装驱动程序。
注意: 如果Windows Update在安装开始后启动,则安装可能会失败。等待Windows Update完成,然后再次尝试安装。
双击下好的.exe文件以安装。
选择合适的路径,点击OK。
安装进度到达百分百后,弹出以下界面。

点击同意并继续。
选择自定义,点击下一步。
取消✔勾选NSight Systems以及Nsight Compute,点击下一步。
点击下一步。
点击下一步。
安装结束,点击关闭。
安装cuDNN
下载
点击进入英伟达官网下载,下载需要先登陆账号。

填一个问卷,选择已下好CUDA版本对应的cuDNN即可。(百度网盘:cudn)
解压下好的cuDNN压缩包,如下图所示:
打开CUDA toolkit的安装目录:”C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2“
将cuDNN的bin,include和lib文件夹复制到该目录下。
添加环境变量
右击此电脑。

点击属性。
点击高级系统设置。
点击环境变量。
点击系统变量下的Path,再点击编辑。
点击新建。
添加以下两个环境变量:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
(以上路径基于个人安装路径)
验证安装
右键开始菜单或使用快捷键+X,点击打开Windows Power Shell,输入以下命令:
cd 'C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\demo_suite'
(以上路径基于个人安装路径)
再输入.\bandwidthTest.exe,若有以下显示则说明安装成功。
注意事项
- 以上安装基于windows平台。
- 以上python库安装基于pip命令,使用conda的用户可参阅conda的安装和更新命令,或是在conda安装路径下打开Anaconda Powershell Prompt,在打开的界面中使用pip命令。

- 支持 CUDA® 的 GPU列表中部分型号显卡并未列出,但仍可能使用CUDA加速,例如GTX 1050 Ti,GTX 1650,GTX 1650 SUPER等,在英伟达控制面板中,若能看到自己的显卡有CUDA核心,则一般可以使用CUDA加速。


- 安装的顺序:
可能遇到的问题
1. 提示"Could not load dynamic library 'xxx.dll'; dlerror: xxx.dll not found"

解决方法:
最可能的原因是tensorflow和CUDA的版本不对应,请查阅Tensorflow与CUDA对应关系表检查。
另外可能可行的办法:在
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin"目录下找到对应的.dll文件,并重命名为xxx.dll。例如提示:"Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found",则把文件
重命名为”cublas64_11.dll”即可。
2. 利用pip命令安装Tensorflow的.whl库文件时提示"ERROR: Could not install packages due to an EnvironmentError: [WinError 5] "
解决方法:在命令的末尾加上—user即可,例如:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-2.4.0rc3-cp38-cp38-win_amd64.whl --user
附录
支持 CUDA® 的 GPU列表(Geforce和TITAN)
GeForce 和 TITAN 产品
| GPU | 计算能力 |
|---|---|
| GeForce RTX 3090 | 8.6 |
| GeForce RTX 3080 | 8.6 |
| GeForce RTX 3070 | 8.6 |
| NVIDIA TITAN RTX | 7.5 |
| GeForce RTX 2080 Ti | 7.5 |
| GeForce RTX 2080 | 7.5 |
| GeForce RTX 2070 | 7.5 |
| GeForce RTX 2060 | 7.5 |
| NVIDIA TITAN V | 7.0 |
| NVIDIA TITAN Xp | 6.1 |
| NVIDIA TITAN X | 6.1 |
| GeForce GTX 1080 Ti | 6.1 |
| GeForce GTX 1080 | 6.1 |
| GeForce GTX 1070 | 6.1 |
| GeForce GTX 1060 | 6.1 |
| GeForce GTX 1050 | 6.1 |
| GeForce GTX TITAN X | 5.2 |
| GeForce GTX TITAN Z | 3.5 |
| GeForce GTX TITAN Black | 3.5 |
| GeForce GTX TITAN | 3.5 |
| GeForce GTX 980 Ti | 5.2 |
| GeForce GTX 980 | 5.2 |
| GeForce GTX 970 | 5.2 |
| GeForce GTX 960 | 5.2 |
| GeForce GTX 950 | 5.2 |
| GeForce GTX 780 Ti | 3.5 |
| GeForce GTX 780 | 3.5 |
| GeForce GTX 770 | 3.0 |
| GeForce GTX 760 | 3.0 |
| GeForce GTX 750 Ti | 5.0 |
| GeForce GTX 750 | 5.0 |
| GeForce GTX 690 | 3.0 |
| GeForce GTX 680 | 3.0 |
| GeForce GTX 670 | 3.0 |
| GeForce GTX 660 Ti | 3.0 |
| GeForce GTX 660 | 3.0 |
| GeForce GTX 650 Ti BOOST | 3.0 |
| GeForce GTX 650 Ti | 3.0 |
| GeForce GTX 650 | 3.0 |
| GeForce GTX 560 Ti | 2.1 |
| GeForce GTX 550 Ti | 2.1 |
| GeForce GTX 460 | 2.1 |
| GeForce GTS 450 | 2.1 |
| GeForce GTS 450* | 2.1 |
| GeForce GTX 590 | 2.0 |
| GeForce GTX 580 | 2.0 |
| GeForce GTX 570 | 2.0 |
| GeForce GTX 480 | 2.0 |
| GeForce GTX 470 | 2.0 |
| GeForce GTX 465 | 2.0 |
| GeForce GT 740 | 3.0 |
| GeForce GT 730 | 3.5 |
| GeForce GT 730 DDR3,128 位 | 2.1 |
| GeForce GT 720 | 3.5 |
| GeForce GT 705* | 3.5 |
| GeForce GT 640 (GDDR5) | 3.5 |
| GeForce GT 640 (GDDR3) | 2.1 |
| GeForce GT 630 | 2.1 |
| GeForce GT 620 | 2.1 |
| GeForce GT 610 | 2.1 |
| GeForce GT 520 | 2.1 |
| GeForce GT 440 | 2.1 |
| GeForce GT 440* | 2.1 |
| GeForce GT 430 | 2.1 |
| GeForce GT 430* | 2.1 |
GeForce 笔记本电脑产品
| GPU | 计算能力 |
|---|---|
| GeForce RTX 2080 | 7.5 |
| GeForce RTX 2070 | 7.5 |
| GeForce RTX 2060 | 7.5 |
| GeForce GTX 1080 | 6.1 |
| GeForce GTX 1070 | 6.1 |
| GeForce GTX 1060 | 6.1 |
| GeForce GTX 980 | 5.2 |
| GeForce GTX 980M | 5.2 |
| GeForce GTX 970M | 5.2 |
| GeForce GTX 965M | 5.2 |
| GeForce GTX 960M | 5.0 |
| GeForce GTX 950M | 5.0 |
| GeForce 940M | 5.0 |
| GeForce 930M | 5.0 |
| GeForce 920M | 3.5 |
| GeForce 910M | 5.2 |
| GeForce GTX 880M | 3.0 |
| GeForce GTX 870M | 3.0 |
| GeForce GTX 860M | 3.0/5.0(**) |
| GeForce GTX 850M | 5.0 |
| GeForce 840M | 5.0 |
| GeForce 830M | 5.0 |
| GeForce 820M | 2.1 |
| GeForce 800M | 2.1 |
| GeForce GTX 780M | 3.0 |
| GeForce GTX 770M | 3.0 |
| GeForce GTX 765M | 3.0 |
| GeForce GTX 760M | 3.0 |
| GeForce GTX 680MX | 3.0 |
| GeForce GTX 680M | 3.0 |
| GeForce GTX 675MX | 3.0 |
| GeForce GTX 675M | 2.1 |
| GeForce GTX 670MX | 3.0 |
| GeForce GTX 670M | 2.1 |
| GeForce GTX 660M | 3.0 |
| GeForce GT 755M | 3.0 |
| GeForce GT 750M | 3.0 |
| GeForce GT 650M | 3.0 |
| GeForce GT 745M | 3.0 |
| GeForce GT 645M | 3.0 |
| GeForce GT 740M | 3.0 |
| GeForce GT 730M | 3.0 |
| GeForce GT 640M | 3.0 |
| GeForce GT 640M LE | 3.0 |
| GeForce GT 735M | 3.0 |
| GeForce GT 635M | 2.1 |
| GeForce GT 730M | 3.0 |
| GeForce GT 630M | 2.1 |
| GeForce GT 625M | 2.1 |
| GeForce GT 720M | 2.1 |
| GeForce GT 620M | 2.1 |
| GeForce 710M | 2.1 |
| GeForce 705M | 2.1 |
| GeForce 610M | 2.1 |
| GeForce GTX 580M | 2.1 |
| GeForce GTX 570M | 2.1 |
| GeForce GTX 560M | 2.1 |
| GeForce GT 555M | 2.1 |
| GeForce GT 550M | 2.1 |
| GeForce GT 540M | 2.1 |
| GeForce GT 525M | 2.1 |
| GeForce GT 520MX | 2.1 |
| GeForce GT 520M | 2.1 |
| GeForce GTX 485M | 2.1 |
| GeForce GTX 470M | 2.1 |
| GeForce GTX 460M | 2.1 |
| GeForce GT 445M | 2.1 |
| GeForce GT 435M | 2.1 |
| GeForce GT 420M | 2.1 |
| GeForce GT 415M | 2.1 |
| GeForce GTX 480M | 2.0 |
| GeForce 710M | 2.1 |
| GeForce 410M | 2.1 |
不同版本Tensorflow-gpu对应的CUDA版本
| Version | Python version | Compiler | Build tools | cuDNN | CUDA |
|---|---|---|---|---|---|
| tensorflow_gpu-2.4.0 | 3.6-3.8 | MSVC 2019 | Bazel 3.1.0 | 8.0 | 11.0 |
| tensorflow_gpu-2.3.0 | 3.5-3.8 | MSVC 2019 | Bazel 3.1.0 | 7.6 | 10.1 |
| tensorflow_gpu-2.2.0 | 3.5-3.8 | MSVC 2019 | Bazel 2.0.0 | 7.6 | 10.1 |
| tensorflow_gpu-2.1.0 | 3.5-3.7 | MSVC 2019 | Bazel 0.27.1-0.29.1 | 7.6 | 10.1 |
| tensorflow_gpu-2.0.0 | 3.5-3.7 | MSVC 2017 | Bazel 0.26.1 | 7.4 | 10 |
| tensorflow_gpu-1.15.0 | 3.5-3.7 | MSVC 2017 | Bazel 0.26.1 | 7.4 | 10 |
| tensorflow_gpu-1.14.0 | 3.5-3.7 | MSVC 2017 | Bazel 0.24.1-0.25.2 | 7.4 | 10 |
| tensorflow_gpu-1.13.0 | 3.5-3.7 | MSVC 2015 update 3 | Bazel 0.19.0-0.21.0 | 7.4 | 10 |
| tensorflow_gpu-1.12.0 | 3.5-3.6 | MSVC 2015 update 3 | Bazel 0.15.0 | 7 | 9 |
| tensorflow_gpu-1.11.0 | 3.5-3.6 | MSVC 2015 update 3 | Bazel 0.15.0 | 7 | 9 |
| tensorflow_gpu-1.10.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
| tensorflow_gpu-1.9.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
| tensorflow_gpu-1.8.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
| tensorflow_gpu-1.7.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
| tensorflow_gpu-1.6.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
| tensorflow_gpu-1.5.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 7 | 9 |
| tensorflow_gpu-1.4.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 6 | 8 |
| tensorflow_gpu-1.3.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 6 | 8 |
| tensorflow_gpu-1.2.0 | 3.5-3.6 | MSVC 2015 update 3 | Cmake v3.6.3 | 5.1 | 8 |
| tensorflow_gpu-1.1.0 | 3.5 | MSVC 2015 update 3 | Cmake v3.6.3 | 5.1 | 8 |
| tensorflow_gpu-1.0.0 | 3.5 | MSVC 2015 update 3 | Cmake v3.6.3 | 5.1 | 8 |
windows的CUDA®安装指南
CUDA_Installation_Guide_Windows_2.pdf
镜像网站
阿里云 https://mirrors.aliyun.com/pypi/simple/
豆瓣(douban) https://pypi.douban.com/simple/

