1、基本步骤
1)判断电脑是否支持cuda
2)安装NVIDIA显卡驱动
3)安装64位的Python 3.6
4)安装tensorflow-gpu
5)安装cuda
6)安装cudnn
7)安装Microsoft Visual C++ 2015
2、显卡介绍
显卡的处理器称为图形处理器(Graphics Processing Unit,GPU),它是显卡的“心脏”,与CPU类似,只不过GPU是专为执行复杂的数学和几何计算而设计的,这些计算是图形渲染所必需的。
供应商:英特尔、NVIDIA、AMD等。查看电脑GPU型号,打开我的电脑属性 → 点开设备管理器 → 点击显示适配器
3、注意事项
注意一:整个安装下来估计需要15G左右的空间。
注意二: VMware Workstation虚拟机中不支持GPU的虚拟化。 虚拟机中的显卡是虚拟的,称为VMware SVGA 3D ,所以,不能使用CUDA ,即无法使用GPU。
注意三: 若安装CDH版的 NVIDIA驱动 ,则NVIDIA 控制面板在Windows 10中需要通过应用商店进行安装。安装之后就可以通过英伟达控制面板(NVIDIA Control Panel)来查看当前系统支持的CUDA版本了。或者在安装 NVIDIA时不要选择CDH版本,而选择标准版后安装后就有英伟达控制面板了。
注意四:查看tensorflow-gpu对应的CUDA版本和cudnn版本的问题:(预先安装好Anaconda的前提下)
D:\Program Files\Python\Python36-64\Lib\site-packages\tensorflow_core\python\platform
(注:本人使用的是windows10,python3.6.5(64位),驱动nvidia442.19(标准版),tensorflow-gpu2.1, cuda10.1,cudnn的版本要与 cuda10.1对应)
方法一:
下载软件 GPU-Z,查看本机是否支持cuda。
方法二:
系统查看是否支持Nvidia显卡(本地查看或者下载鲁大师查看是否有独立显卡)
若支持,则继续后续步骤;如不支持,换电脑(硬件跟不上,怎么尝试都没用),因为CUDA和CUDNN都是只支持Nvidia显卡的。
NVIDIA介绍:NVIDIA,中文名英伟达,是一家人工智能计算公司,与ATI(后被AMD收购)齐名,专注于打造能够增强个人和专业计算平台的人机交互体验的产品(一家显卡厂商),是全球可编程图形处理技术领袖,发明了GPU,重新定义了现代计算机图形技术,并彻底改变了并行计算。其中 NVIDIA 组织的 NVIDIA GPU 技术大会 (GTC) 是AI和深度学习领域的大会,也是全球范围的GPU开发者行业聚会。
下载显卡NVIDIA 驱动:https://www.nvidia.cn/Download/index.aspx?lang=cn
在安装 NVIDIA时不要选择CDH版本,而选择标准版后安装后就有英伟达控制面板了。
驱动安装成功后,生成以下库文件
驱动程序类型需要选择标准,不要选择DCH,否则安装后没有 NVIDIA 控制面板。
目前的tensorflow不支持windows32位的python,只支持64位的python,如何判断python是32位还是64位?
方法一:
方法二:
import struct
struct.calcsize("P")
如果是4,说明是32位的;如果是其他的是,64位的。
struct.calcsize用于计算格式字符串所对应的结果长度。
1、官网查看配置信息
2、下载安装TF
# 直接安装推荐的最新版本即可#pip使用清华源(tsinghua)秒速安装TensorFlow 2.1.0 GPU版pip install tensorflow-gpu==2.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装过程中,请尽量保持电脑处在性能良好的状态下。TF安装过程如下:
# tf 安装过程#pip使用清华源(tsinghua)秒速安装TensorFlow 2.1.0 GPU版
C:\Users\xxt>pip install tensorflow-gpu==2.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: tensorflow-gpu==2.1.0 from file:///C:/Users/xxt/Desktop/tf-gpu/tensorflow_gpu-2.1.0-cp36-cp36m-win_amd64.whl in c:\programdata\anaconda3\lib\site-packages (2.1.0)
Requirement already satisfied: numpy<2.0,>=1.16.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.18.1)
Requirement already satisfied: tensorflow-gpu-estimator<2.2.0,>=2.1.0rc0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (2.1.0)
Requirement already satisfied: keras-applications>=1.0.8 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.0.8)
Requirement already satisfied: scipy==1.4.1; python_version >= "3" in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.4.1)
Requirement already satisfied: opt-einsum>=2.3.2 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (3.1.0)
Requirement already satisfied: grpcio>=1.8.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.21.1)
Requirement already satisfied: gast==0.2.2 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (0.2.2)
Requirement already satisfied: wrapt>=1.11.1 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.11.2)
Requirement already satisfied: astor>=0.6.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (0.8.0)
Requirement already satisfied: keras-preprocessing>=1.1.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.1.0)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (0.31.1)
Requirement already satisfied: protobuf>=3.8.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (3.8.0)
Requirement already satisfied: six>=1.12.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.14.0)
Requirement already satisfied: absl-py>=0.7.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (0.7.1)
Requirement already satisfied: google-pasta>=0.1.6 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (0.1.7)
Requirement already satisfied: termcolor>=1.1.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (1.1.0)
Requirement already satisfied: tensorboard<2.2.0,>=2.1.0 in c:\programdata\anaconda3\lib\site-packages (from tensorflow-gpu==2.1.0) (2.1.0)
Requirement already satisfied: h5py in c:\programdata\anaconda3\lib\site-packages (from keras-applications>=1.0.8->tensorflow-gpu==2.1.0) (2.7.1)
Requirement already satisfied: setuptools in c:\programdata\anaconda3\lib\site-packages (from protobuf>=3.8.0->tensorflow-gpu==2.1.0) (41.0.1)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (0.4.1)
Requirement already satisfied: werkzeug>=0.11.15 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (0.14.1)
Requirement already satisfied: google-auth<2,>=1.6.3 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (1.11.2)
Requirement already satisfied: requests<3,>=2.21.0 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (2.23.0)
Requirement already satisfied: markdown>=2.6.8 in c:\programdata\anaconda3\lib\site-packages (from tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (3.1.1)
Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\programdata\anaconda3\lib\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (1.3.0)
Requirement already satisfied: rsa<4.1,>=3.1.4 in c:\programdata\anaconda3\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (4.0)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in c:\programdata\anaconda3\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (4.0.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\programdata\anaconda3\lib\site-packages (from google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (0.2.8)
Requirement already satisfied: idna<3,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (2.6)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (2018.4.16)
Requirement already satisfied: chardet<4,>=3.0.2 in c:\programdata\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests<3,>=2.21.0->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (1.22)
Requirement already satisfied: oauthlib>=3.0.0 in c:\programdata\anaconda3\lib\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (3.1.0)
Requirement already satisfied: pyasn1>=0.1.3 in c:\programdata\anaconda3\lib\site-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.2.0,>=2.1.0->tensorflow-gpu==2.1.0) (0.4.8)
C:\Users\xxt>
3、 测试是否成功
如果是32位的python,安装tf会报错:
#如果是32位的python,安装tf会报错C:\Users\Administrator>pip install tensorflow-gpu==2.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simplesLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simplesERROR: Could not find a version that satisfies the requirement tensorflow-gpu==2.1.0 (from versions: none)ERROR: No matching distribution found for tensorflow-gpu==2.1.0
1、cuda介绍和下载
1)CUDA(Compute Unified Device Architecture)是显卡厂商NVIDIA推出的运算平台,是一种通用并行计算架构,该架构使GPU能够解决复杂的计算问题。它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。开发人员现在可以使用C语言来为CUDA架构编写程序。
2)随着显卡的发展,GPU越来越强大,而且GPU为显示图像做了优化。在计算上已经超越了通用的CPU。如此强大的芯片如果只是作为显卡就太浪费了,因此 NVIDIA 推出CUDA,让显卡可以用于图像计算以外的目的。目前只有G80、G92、G94、G96、GT200、GF100、GF110、GK100、GK104、GK107平台等NVidia显卡才能使用CUDA,工具集的核心是一个C语言编译器。G80中拥有128个单独的ALU,因此非常适合并行计算,而且数值计算的速度远远优于CPU。
3)CUDA目前支持linux和Windows操作系统。进行CUDA开发需要依次安装驱动、toolkit、SDK三个软件(安装cuda是为使用GPU加速)。
4)查看cuda版本(安装环境可能用到)
方法一:
在目录以下可以看到安装的CUDA版本
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA
方法二:
打开cmd,进入命令行模式,输入命令
nvcc --version
方法三:
打开控制面板,选择NVIDIA控制面板,选择帮助 → 下拉菜单 → 系统信息 → 组件 → cuda支持的最大版本号
直接百度cuda
可以找到(https://developer.nvidia.com/cuda-toolkit-archive)
注意:CUDA和CUDNN都是只支持Nvidia显卡的,官网默认下载是最新的版本,可能与TF查到的版本不一致,所以要选择历史版本(Legacy Release)
2、双击安装软件
3、点击同意并继续
4、如果不知道怎么选,就选择精简安装
5、勾选,next
注:如若出现安装失败,则一般为360无检测为木马所致,建议关闭360。
6、配置环境变量
在系统环境变量Path里,添加:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include;C:\Program Files\NVIDIA GPU Computing; Toolkit\CUDA\v10.1\lib\x64
即:
#将以下三个路径添加到系统环境变量path里C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin;
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include;
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64;
7、测试是否安装成功
命令行输入nvcc -V,如果输出是 CUDA 版本信息,则说明安装成功
1、下载对应版本的cudnn
进入官网(https://developer.nvidia.com/rdp/cudnn-download ),注册登录账号,下载适用CUDA10.1的CUDNN (cudnn-10.1-windows7-x64-v7.6.5.32)。
2、解压
大概两百多兆,下完之后得到一个压缩包,解压,看到三个文件夹:
3、将上面三个文件夹的内容复制到之前提到得三个路径里
(注意:其实没改过的话默认是将cuda里的三个文件夹里的文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1 路径下相同的文件夹里面,不是将文件夹直接复制过去覆盖。)
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1
#可以看到和CUDNN文件夹有三个相同名字的文件夹,
#将cudnn文件夹里三个文件里的内容复制到上面文件里相同名字的文件夹下,即可。
七、安装 Microsoft Visual C++ 2015 首先查看系统是否有Microsoft Visual C++ 2015。若有,则没必要安装了,若没有,继续按步骤下载安装:
若没有,则下载安装Microsoft Visual C++ 2015(x64:vc_redist.x64.exe),安装完后,重启电脑。https://support.microsoft.com/zh-cn/help/2977003/the-latest-supported-visual-c-downloads
八、测试
import tensorflow as tf
print(f"tensorflow version = {tf.__version__}", end='\n\n')
a = tf.random.normal((100, 100))b = tf.random.normal((100, 100))c = tf.linalg.matmul(a, b)c_numpy = c.numpy()
print(c, end='\n\n')print(c_numpy)
结果:
2020-02-24 12:51:22.674894: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll2020-02-24 12:51:23.101927: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:pciBusID: 0000:02:00.0 name: GeForce 940MX computeCapability: 5.0coreClock: 1.189GHz coreCount: 3 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 37.33GiB/s2020-02-24 12:51:23.112183: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll2020-02-24 12:51:23.641344: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll2020-02-24 12:51:23.676729: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll2020-02-24 12:51:23.721057: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll2020-02-24 12:51:23.752607: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll2020-02-24 12:51:23.773300: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll2020-02-24 12:51:23.821718: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll2020-02-24 12:51:24.148799: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 02020-02-24 12:51:24.156221: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX22020-02-24 12:51:24.165691: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:pciBusID: 0000:02:00.0 name: GeForce 940MX computeCapability: 5.0coreClock: 1.189GHz coreCount: 3 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 37.33GiB/s2020-02-24 12:51:24.175903: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll2020-02-24 12:51:24.183306: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll2020-02-24 12:51:24.189834: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll2020-02-24 12:51:24.199380: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll2020-02-24 12:51:24.232643: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll2020-02-24 12:51:24.274273: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll2020-02-24 12:51:24.309493: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll2020-02-24 12:51:24.360742: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
About Me:小婷儿
● 本文作者:小婷儿,专注于python、数据分析、数据挖掘、机器学习相关技术,也注重技术的运用
● 作者博客地址:https://blog.csdn.net/u010986753
● 本系列题目来源于作者的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
● 微信:tinghai87605025 联系我加微信群
● QQ:87605025
● QQ交流群py_data :483766429
● 公众号:python宝 或 DB宝
● 提供OCP、OCM和高可用最实用的技能培训
● 题目解答若有不当之处,还望各位朋友批评指正,共同进步
如果你觉得到文章对您有帮助,欢迎赞赏哦!有您的支持,小婷儿一定会越来越好!