使用 Anaconda 安装 python并搭建 tensorflow_gpu 环境

1,150 阅读4分钟

环境信息汇总

  • gpu:nVidia GeForce GTX 1050
  • Anaconda :Anaconda 3
  • CUDA: 11.0
  • cuDNN:cudnn-11.0-windows-x64-v8.0.5.39
  • tensorflow_gpu:2.4.0

检查环境信息

【注】 CUDA(ComputeUnified Device Architecture),是显卡厂商NVIDIA推出的运算平台。 CUDA是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。

是否支持cuda: 根据 forums.developer.nvidia.com/t/cuda-for-… 中回答查看 en.wikipedia.org/wiki/CUDA#G… 有下面描述:

  • CUDA SDK 8.0 support for compute capability 2.0 – 6.x (Fermi, Kepler, Maxwell, Pascal). Last version with support for compute capability 2.x (Fermi) (Pascal GTX 1070Ti Not Supported)
  • CUDA SDK 9.0 – 9.2 support for compute capability 3.0 – 7.2 (Kepler, Maxwell, Pascal, Volta) (Pascal GTX 1070Ti Not Supported. CUDA SDK 9.0 and support CUDA SDK 9.2).
  • CUDA SDK 10.0 – 10.2 support for compute capability 3.0 – 7.5 (Kepler, Maxwell, Pascal, Volta, Turing). Last version with support for compute capability 3.x (Kepler). 10.2 is the last official release for macOS, as support will not be available for macOS in newer releases.
  • CUDA SDK 11.0 – 11.2 support for compute capability 3.5 – 8.6 (Kepler (in part), Maxwell, Pascal, Volta, Turing, Ampere)[33] New data types: Bfloat16 and TF32 on third-generations Tensor Cores.[34]**
Compute capability(version)GPUsGeForce...
6.1GP102, GP104, GP106, GP107, GP108Nvidia TITAN Xp,Titan X,GeForce GTX 1080 Ti, GTX 1080, GTX 1070 Ti, GTX 1070, GTX 1060,GTX 1050 Ti, GTX 1050, GT 1030, GT 1010,MX350, MX330, MX250, MX230, MX150, MX130, MX110...

安装 Anaconda

zh.wikipedia.org/wiki/Anacon…

Anaconda致力于简化软件包管理系统和部署。Anaconda的包使用软件包管理系统Conda[6]进行管理。超过1200万人使用Anaconda发行版本,并且Anaconda拥有超过1400个适用于Windows、Linux和MacOS的数据科学软件包[7]。

在官网下载安装 www.anaconda.com/ ,并添加环境变量

F:\*\Anaconda3
F:\*\Anaconda3\Scripts
F:\*\Anaconda3\Library\bin

在命令行中执行命令查看是否添加成功:

conda list

CUDA安装

查看 www.tensorflow.org/install/sou…

版本Python 版本编译器构建工具cuDNNCUDA
tensorflow_gpu-2.4.03.6-3.8MSVC 2019Bazel 3.1.08.011.0

从此页面进行下载软件包: developer.nvidia.com/cuda-toolki…

图片.png

测试是否安装成功

C:\Users\xxx>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Thu_Jun_11_22:26:48_Pacific_Daylight_Time_2020
Cuda compilation tools, release 11.0, V11.0.194
Build cuda_11.0_bu.relgpu_drvr445TC445_37.28540450_0

cuDNN 神经网络加速库安装

developer.nvidia.com/zh-cn/cudnn

NVIDIA CUDA® 深度神经网络库 (cuDNN) 是经 GPU 加速的深度神经网络基元库。cuDNN 可大幅优化标准例程(例如用于前向传播和反向传播的卷积层、池化层、归一化层和激活层)的实施。

注册账号登陆后,来到此界面下载与CUDA版本对应的cuDNN: developer.nvidia.com/rdp/cudnn-a…

根据此指导进行复制粘贴

docs.nvidia.com/deeplearnin…

配置环境

可以使用 conda env list 来查看 在 Anaconda 中创建的环境

打开命令行后在windows下可以使用以下命令操作环境:

conda create -n xxx pip python=3.6 创建 python=3.6 的名为xxx环境

activate xxx 来进入 xxx 环境

conda deactivate xxx 退出 xxx 环境

安装 tensorflow

activate xxx 来进入 xxx 环境

使用pip命令来安装对应 tensorflow 版本:

pip install --ignore-installed --upgrade tensorflow_gpu==2.4.0

在xxx环境中运行以下测试代码:

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))

输出为可以使用的gpu个数:

(xxx) C:\Users\30819\Desktop>python 1.py
2021-03-14 17:12:59.896100: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-03-14 17:13:02.240782: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-03-14 17:13:02.245444: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
2021-03-14 17:13:03.241515: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1720] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce GTX 1050 computeCapability: 6.1
coreClock: 1.493GHz coreCount: 5 deviceMemorySize: 4.00GiB deviceMemoryBandwidth: 104.43GiB/s
2021-03-14 17:13:03.251678: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-03-14 17:13:03.262406: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-03-14 17:13:03.266693: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-03-14 17:13:03.283413: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-03-14 17:13:03.289394: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-03-14 17:13:03.303645: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-03-14 17:13:03.310455: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-03-14 17:13:03.316981: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-03-14 17:13:03.326594: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
Num GPUs Available:  1

可以进行更多的尝试代码:

www.tensorflow.org/guide/gpu

reference

docs.nvidia.com/deeplearnin…

blog.csdn.net/weixin_4417…

www.jianshu.com/p/f9fb9742d…

zhuanlan.zhihu.com/p/37086409