Ubuntu16.04安装TensorFlow2.x CPU和GPU必备指南

原文内容:https://github.com/apachecn/AiLearning/blob/master/docs/TensorFlow2.x/安装指南.md

  • CPU安装: pip install tensorflow
  • GPU安装: pip install tensorflow-gpu别慌,GPU需要先安装以下内容
  • 注意: 不要同时安装

硬件要求

支持以下启用GPU的设备:

  • 具有CUDA®Compute Capability 3.5或更高版本的NVIDIA®GPU卡。请参阅支持CUDA的GPU卡列表 。

软件需求

您的系统上必须安装以下NVIDIA®软件:

  • NVIDIA®GPU 驱动程序 CUDA 10.0需要410.x或更高版本。
  • CUDA®工具包 - TensorFlow支持CUDA 10.0(TensorFlow> = 1.13.0)
  • CUPTI随附CUDA工具包。
  • cuDNN SDK(> = 7.4.1)
  • (可选) TensorRT 5.0 可以改善延迟和吞吐量,以在某些模型上进行推断。

1.安装 NVIDIA 驱动

$ wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
$ sudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
$ sudo apt-get update

$ ubuntu-drivers devices  # 查看推荐的版本安装
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd000017C8sv00001458sd000036B6bc03sc00i00
vendor   : NVIDIA Corporation
model    : GM200 [GeForce GTX 980 Ti]
driver   : nvidia-387 - third-party non-free
driver   : nvidia-410 - third-party non-free
driver   : nvidia-384 - third-party non-free
driver   : nvidia-430 - third-party free recommended   # 推荐安装
driver   : xserver-xorg-video-nouveau - distro free builtin
driver   : nvidia-396 - third-party non-free
driver   : nvidia-390 - third-party non-free
driver   : nvidia-418 - third-party non-free
driver   : nvidia-415 - third-party free

# –no-install-recommends参数来避免安装非必须的文件,从而减小镜像的体积
$ sudo apt-get install --no-install-recommends nvidia-430 -y
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会同时安装下列软件:
  lib32gcc1 libc-dev-bin libc6 libc6-dbg libc6-dev libc6-i386
建议安装:
  glibc-doc
推荐安装:
  libcuda1-430 nvidia-opencl-icd-430
下列【新】软件包将被安装:
  lib32gcc1 libc6-i386 nvidia-430
下列软件包将被升级:
  libc-dev-bin libc6 libc6-dbg libc6-dev
升级了 4 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 260 个软件包未被升级。
需要下载 99.7 MB/111 MB 的归档。
解压缩后会消耗 429 MB 的额外空间。
您希望继续执行吗? [Y/n] Y
获取:1 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu xenial/main amd64 nvidia-430 amd64 430.26-0ubuntu0~gpu16.04.1 [99.7 MB]
25% [1 nvidia-430 492 kB/99.7 MB 0%] 563 B/s 2天 0小时 58分 14秒58秒
...
92% [1 nvidia-430 93.0 MB/99.7 MB 93%] 4,111 B/s 27分 28秒
已下载 7,180 kB,耗时 4分 26秒 (26.9 kB/s)

Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/4.10.0-28-generic/updates/dkms/

depmod....

DKMS: install completed.
正在处理用于 libc-bin (2.23-0ubuntu10) 的触发器 ...
正在处理用于 initramfs-tools (0.122ubuntu8.14) 的触发器 ...
update-initramfs: Generating /boot/initrd.img-4.10.0-28-generic

# $ sudo reboot  # 安装完需要重启电脑
$ sudo nvidia-smi

这里面大家需要注意的是: 采用在终端输入 ubuntu-drivers devices 会提示推荐你用什么版本,我的设备显示不出来,所以安装的是418.43这个型号的驱动。(目前最新版本)

注意事项一:官网下载地址
推荐网址:(https://www.geforce.cn/drivers)只有这个GeForce型号的,别的型号推荐去其对应的网址查询。

注意事项二:不要在下面这个网址下载,不是不能,最直接的官网,对应的东西最新,也最详细
网址如下(https://www.nvidia.com/Download/index.aspx?lang=cn)

理由:

  • (1)上面的网址,选择驱动型号,点进去可以看到许多详细的信息,尤其是它支持什么样的显卡,都有,特别详细。
  • (2)这个网址在我写博客(2019.3.6)为止,还没有GTX1660Ti的Ubuntu驱动

注意事项三:具体操作见网上别人写好的。

2.安装 CUDA 1.0 + cuDNN 7

CUDA

下面这个网址是tensorflow各环境参数对应版本图(https://tensorflow.google.cn/install/source)可供参考。cuda和cudnn对应关系应该没问题,但是tensorflow版本不能过高,否则会出错。

注意事项一:下载地址
cuda下载网址为:(https://developer.nvidia.com/),右上角搜索“CUDA Toolkit Archive”,点击第一个(最新的)的进去,里面有许多版本可供选择,切记!切记!切记!目前网友的说法是:tensorflow只能支持cuda9.0及以下版本。

注意事项二:选择run下载,而不选择del
这个具体是什么原因,没搞明白,网友也强烈推荐run,我之前试过del的,失败了,所以大家尽量采用run这种方法。可能有人没明白说明意思,你在选择的时候多留个心眼就注意到了。

cuDNN

官网网址如下
网址:https://developer.nvidia.com/cudnn
需要注册,我是从别人那直接过来的,就没注册,大家需要的自己去,这个安装相对简单。

同样有验证的过程,这个相对来说是简单的,没什么需要太注意的,跟着网上的走就好了。

执行命令

$ sudo apt-get install gnupg-curl
$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_10.0.130-1_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu1604_10.0.130-1_amd64.deb
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
$ sudo apt-get update -y

# sudo apt-get install cuda
$ sudo apt-get install --no-install-recommends \
    cuda-10-0 \
    libcudnn7=7.6.2.24-1+cuda10.0  \
    libcudnn7-dev=7.6.2.24-1+cuda10.0

$ sudo apt-get install -y --no-install-recommends libnvinfer5=5.1.5-1+cuda10.0 libnvinfer-dev=5.1.5-1+cuda10.0

3.安装TensorFlow并验证

  • GPU安装: sudo pip3 install tensorflow-gpu
from __future__ import absolute_import, division, print_function, unicode_literals

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

结果:

>>> from __future__ import absolute_import, division, print_function, unicode_literals
>>>
>>> import tensorflow as tf

>>> print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
2019-10-10 16:12:15.524570: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2019-10-10 16:12:15.537451: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-10-10 16:12:15.538341: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce GTX 980 Ti major: 5 minor: 2 memoryClockRate(GHz): 1.2405
pciBusID: 0000:01:00.0
2019-10-10 16:12:15.538489: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2019-10-10 16:12:15.539261: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2019-10-10 16:12:15.539899: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2019-10-10 16:12:15.540081: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2019-10-10 16:12:15.540886: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2019-10-10 16:12:15.541540: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2019-10-10 16:12:15.543506: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2019-10-10 16:12:15.543601: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-10-10 16:12:15.544469: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1006] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-10-10 16:12:15.545326: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
Num GPUs Available:  1

信息来源:

  • https://tensorflow.google.cn/install/gpu
  • https://tensorflow.google.cn/guide/gpu
  • https://blog.csdn.net/qq_44774398/article/details/99832436
  • https://blog.csdn.net/weixin_43012220/article/details/88241766

你可能感兴趣的:(Ubuntu16.04安装TensorFlow2.x CPU和GPU必备指南)