不再支持源选项 5。请使用 7 或更高版本。_Linux系统下安装TensorFlow的GPU版本

a094598c5178f5b09e56376b98e48506.png

本文首发于“AI柠檬博客”:

Linux系统下安装TensorFlow的GPU版本 | AI柠檬

原文会不定期更新,以保证教程的有效性,点击链接以查看原文。


本文已在2020年10月更新到最新方法,确保本文教程的有效性。

前言:
曾经(2017年)安装TensorFlow的GPU版本真的不是一件容易的事(因为教程很少,本文是为数不多的可用教程),好难,网上的各种安装教程倒是不少,但是基本没有一个能完全照着那种方法就能安装成功的,甚至有些连最基本的一些必需的步骤都没写到,那样子的话能安装到位才是奇迹。
在经历各种踩坑后,我通过大量的互联网搜索,综合各个网站上写到的安装tensorflow-gpu的方法,终于总结出来一个走的通的方法,并且亲自动手实践,在Linux(Ubuntu) + Python3安装且运行成功。

Python2.7下安装过程跟本文内容大同小异,可参考NVIDIA官方教程:http://www.nvidia.com/object/gpu-accelerated-applications-tensorflow-installation.html

现在(2020年)不要使用Python2,因为其已经被官方停止维护。

准备:

  • 一块计算性能大于等于3.0的NVIDIA的显卡
    (不知道自己的NVIDIA GPU的计算性能的可以在这里查到: https://developer.nvidia.com/cuda-gpus )
  • 装有Linux系统的电脑
    (本文以Ubuntu 16.04.2 LTS Gnome 64位系统为例展开,其他的也是大同小异)
  • Python 3.5 – 3.7
    (请使用Python3版本,而不是2.7版)

开始:

安装NVIDIA Driver (可以跳过,并直接使用CUDA安装包自动安装)

首先检查你的NVIDIA VGA card model,检查你的显卡型号

$ sudo lshw -numeric -C display

我的电脑上可以看到其中的nvidia显卡有这样一条信息

product: GM107M [GeForce GTX 850M] [10DE:1391]

根据刚才显示的信息内容,到官网上选择合适的选项,检查你的显卡所适合的驱动版本:

http://www.nvidia.com/Download/index.aspx

我的电脑上的显卡所适合的版本是375.66。

同样,你还可以在终端上输入命令来看所需的版本:

$ ubuntu-drivers devices

我的电脑上出现有nividia-375字样,跟在官网上查的一样。所以我们可以在终端上使用apt来安装它,安装前建议先update一下源,或者更换一个速度快的源。

在Ubuntu系列系统中,我们只需要使用下面一行命令,即可一键自动地正确安装Nvidia显卡驱动,不需要使用任何其他的手动安装方式,简单高效便捷。

$ sudo apt install nvidia-375

其中,nvidia-375这个需要根据自己显卡的型号所支持的驱动版本来修改后面的数字。

如果您比较喜欢折腾或者想自己动手练习一步一步安装的话,可以考虑从官网上下载驱动来安装,但是AI柠檬不建议您这样做,因为您的时间是宝贵的,不要将有限的时间和精力投入到繁琐的装显卡驱动的操作中。

装完之后,你可以在dashboard上搜索nvidia,看到有 NVIDIA X Server Settings的东西,那么驱动就安装成功了,如果没有,那就重启一次电脑看看。

安装Cuda:

TensorFlow从最初的版本到现在,也已经有很多种版本了,很多以前的一些资料都不适用了。不同的TensorFlow的版本,所需要的CUDA和cuDNN的最低版本要求也不一样,2020年08月18日最新整理相关信息如下:

TensorFlow版本Python版本CUDA版本cuDNN版本
tensorflow_gpu-2.1.02.7、3.5-3.710.17.6
tensorflow_gpu 1.15.x2.7、3.5-3.710.07.6
tensorflow_gpu-1.13.0 ~ 2.0.0(不包含1.15.x)2.7、3.3-3.710.07.4
tensorflow_gpu-1.5.0 ~ 1.12.02.7、3.3-3.697
tensorflow_gpu-1.3.0 ~ 1.4.02.7、3.3-3.686
tensorflow_gpu-1.0.0 ~ 1.2.02.7、3.3-3.685.1
TensorFlow版本Python版本CUDA版本cuDNN版本
tensorflow_gpu-2.1.02.7、3.5-3.710.17.6
tensorflow_gpu 1.15.x2.7、3.5-3.710.07.6
tensorflow_gpu-1.13.0 ~ 2.0.0(不包含1.15.x)2.7、3.3-3.710.07.4
tensorflow_gpu-1.5.0 ~ 1.12.02.7、3.3-3.697
tensorflow_gpu-1.3.0 ~ 1.4.02.7、3.3-3.686
tensorflow_gpu-1.0.0 ~ 1.2.02.7、3.3-3.685.1

当前,所有版本均需要GCC的版本为4.8(从源码编译安装的话)。

信息来源:

https://www.tensorflow.org/install/source

而CUDA需要根据自己计算机中显卡支持的驱动的版本来下载,我的电脑的显卡最高只能支持到Cuda 8.0,那么tf也就最高只能使用1.4版本了,在服务器上Tesla计算卡支持所有CUDA版本,可以支持所有TF版本。我在服务器上安装了10.0版本的Cuda,其他的Cuda版本(主要是Cuda 9.0)可以借鉴以下内容,就是注意一下版本号的不同即可。

首先是去官网下载 cuda toolkit download ,选择你电脑对应的选项,下载文件一定要选择runfile。

https://developer.nvidia.com/cuda-downloads

下载完成之后,执行

$ sudo bash cuda_10.0.130_410.48_linux.run

就进入安装了,开始的一大堆文字都是End User License Agreement,可以CTRL +C 跳过,然后输入accept表示接受协议

接下来就是安装的交互界面,根据提示一步一步进行就好。

在开始的

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?

如果刚才没安装驱动,就选择y,否则就选择n,因为之前已经安装驱动了。

Using more to view the EULA.

End User License Agreement

--------------------------

Preface

-------

The following contains specific license terms and conditions

for four separate NVIDIA products. By accepting this

agreement, you agree to comply with all the terms and

conditions applicable to the specific product(s) included

herein.


NVIDIA CUDA Toolkit


Description

 

The NVIDIA CUDA Toolkit provides command-line and graphical

tools for building, debugging and optimizing the performance

of applications accelerated by NVIDIA GPUs, runtime and math

libraries, and documentation including programming guides,

user manuals, and API references. The NVIDIA CUDA Toolkit

License Agreement is available in Chapter 1.

 

Default Install Location of CUDA Toolkit

 

Windows platform:

 

Do you accept the previously read EULA?

accept/decline/quit: accept

 

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?

(y)es/(n)o/(q)uit: n

 

Install the CUDA 10.0 Toolkit?

(y)es/(n)o/(q)uit: y

 

Enter Toolkit Location

 [ default is /usr/local/cuda-10.0 ]: 

 

Do you want to install a symbolic link at /usr/local/cuda?

(y)es/(n)o/(q)uit: y

 

Install the CUDA 10.0 Samples?

(y)es/(n)o/(q)uit: y



Enter CUDA Samples Location

 [ default is /home/kinny ]:

Installing the CUDA Toolkit in /usr/local/cuda-10.0 ...

Missing recommended library: libXmu.so

Installing the CUDA Samples in /home/kinny ...

Copying samples to /home/kinny/NVIDIA_CUDA-10.0_Samples now...

Finished copying samples.

===========

= Summary =

===========

Driver:   Not Selected

Toolkit:  Installed in /usr/local/cuda-10.0

Samples:  Installed in /home/kinny, but missing recommended libraries

Please make sure that

 -   PATH includes /usr/local/cuda-10.0/bin

 -   LD_LIBRARY_PATH includes /usr/local/cuda-10.0/lib64, or, add /usr/local/cuda-10.0/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-10.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 10.0 functionality to work.

To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:

    sudo <CudaInstaller>.run -silent -driver

Logfile is /tmp/cuda_install_17494.log

配置Cuda环境变量:

在 ~/.bashrc 的最后添加

export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda

其中,

前 2 个(PATH, LD_LIBRARY_PATH) 是 CUDA 官网安装文档中建议的变量。

第 3 个(CUDA_HOME)是 tensorflow-GPU 版本要求的变量。

配置完环境变量之后,一定要重载.bashrc文件更新一下环境,否则不能立即生效。也可以通过重启电脑使得环境变量生效。

$ source ~/.bashrc

网上很多其他的相关教程都遗漏了这一步,对于新手来说,这是致命的灾难,往往会出现,明明按照教程配置的,却根本无法使用GPU的情况。

检查显卡驱动安装结果

$ nvidia-smi
Fri Oct 16 19:52:27 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.48                 Driver Version: 410.48 |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  Off  | 00000000:06:00.0 Off |                    0 |
| N/A   51C    P0   134W / 250W |  11855MiB / 12198MiB |    84%       Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0     27687      C   python                                     11845MiB |
+-----------------------------------------------------------------------------+

安装深度学习库cuDNN

上文提到过,不同版本的TensorFlow需要的cuDNN版本也不一样,博主最初安装的是1.0版的tf,需要cuDNN版本为5.1,如果我安装tf1.15,那么就是需要CUDA 10.0 + cuDNN 7.6。而当前各个情况下不同版本的配置要求详见上文刚才所述内容。

首先到官网下载cuDNN ( https://developer.nvidia.com/cudnn ),需要注册为开发者才能下载,而且直接下载可能速度非常慢,如果已有别人下载好的,应该是可以直接拿过来用,不过前提是版本是你需要的。

文件名为:cudnn-10.0-linux-x64-v7.6.4.38.solitairetheme8

然后改名并解压

$ mv cudnn-10.0-linux-x64-v7.6.4.38.solitairetheme8 cudnn-10.0-linux-x64-v7.6.4.38.tgz
$ tar zxf cudnn-10.0-linux-x64-v7.6.4.38.tgz

然后将库和头文件copy到cuda目录(一定是你自己安装的目录如/usr/local/cuda-10.0),不过正确安装的话,ubuntu一般就会有软链接/usr/local/cuda -> /usr/local/cuda-10.0/

$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include

$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64

接下来就是修改文件访问权限:

$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

最后一步:安装TensorFlow的GPU版本

在我博客的前一篇文章中,我写了如何安装支持GPU版本的TensorFlow,详见:

在多平台上安装TensorFlow的方法 | AI柠檬​blog.ailemon.me

这里我再重复一下

终端命令行输入

$ sudo pip3 install tensorflow-gpu==1.15.0

可根据自己的需要将1.15.0改为其他版本。等待一会儿,就安装好了。然后可以使用我刚才提到的那篇文章中的测试代码验证是否安装成功,以及GPU是否可用。

$ python3
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>>

对于GPU版本,可以使用下面的代码来测试TF是否可以使用GPU来加速计算,如果输出False,那就是不能使用,否则是可以使用,会输出GPU信息。

>>> tf.test.is_gpu_available()
2020-10-16 20:15:19.510395: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-10-16 20:15:19.581707: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcuda.so.1
2020-10-16 20:15:19.758984: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4d90a60 executing computations on platform CUDA. Devices:
2020-10-16 20:15:19.759031: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): Tesla P100-PCIE-12GB, Compute Capability 6.0
2020-10-16 20:15:19.763340: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2095340000 Hz
2020-10-16 20:15:19.763994: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x4e02bd0 executing computations on platform Host. Devices:
2020-10-16 20:15:19.764030: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2020-10-16 20:15:19.764859: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: Tesla P100-PCIE-12GB major: 6 minor: 0 memoryClockRate(GHz): 1.3285
pciBusID: 0000:06:00.0
2020-10-16 20:15:19.769317: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.0
2020-10-16 20:15:19.786903: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcublas.so.10.0
2020-10-16 20:15:19.792640: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcufft.so.10.0
2020-10-16 20:15:19.805077: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcurand.so.10.0
2020-10-16 20:15:19.819612: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcusolver.so.10.0
2020-10-16 20:15:19.831185: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcusparse.so.10.0
2020-10-16 20:15:19.854204: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudnn.so.7
2020-10-16 20:15:19.855461: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2020-10-16 20:15:19.855520: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.0
2020-10-16 20:15:19.856924: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-10-16 20:15:19.856947: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187] 0
2020-10-16 20:15:19.856959: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0:   N
2020-10-16 20:15:19.858309: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/device:GPU:0 with 62 MB memory) -> physical GPU (device: 0, name: Tesla P100-PCIE-12GB, pci bus id: 0000:06:00.0, compute capability: 6.0)
True

至此,如果你看到了跟我一样的输出的话,那么恭喜你,tensorflow-gpu可以正常使用GPU来计算了。

后记:

博主曾在给工作站配置的时候,import tensorflow之后遇到过一个这样的报错:

ImportError: libcudnn.6: cannot open shared object file:
  No such file or directory

详见 StackOverflow 42013316

这是因为我安装的1.4版的TF,却装了7.0版本的cuDNN,而不是tensorflow默认寻找的6.0版本,这时只需要使用 ” ln “命令创建一个名为“libcudnn.6”的软连接指向“libcudnn.7.0*”即可,然后,tensorflow即可正常使用了。但是博主建议还是直接安装6.0,否则基于tensorflow的一些其他框架比如keras可能无法正常使用tf来训练。

本文首发于“AI柠檬博客”:

Linux系统下安装TensorFlow的GPU版本 | AI柠檬

原文会不定期更新,以保证教程的有效性,点击链接以查看原文。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值