Tensorflow安装指南

Tensorflow安装指南

简介

当然你可以查看

因为cs231n要使用TensorFlow完成作业,就只能硬着头皮安装了GPU版本的TensorFlow了。此次安装版本为CUDA9.0和cuDNN7.0

我的电脑显卡为:GeForce GTX 850M

硬件支持:

Distribution Kernel GCC GLIBC ICC PGI XLC CLANG
x86_64
RHEL 7.x 3.10.0 4.8.5 2.17 17.0 18.x NO 5.0.0
RHEL 6.x 2.6.32 4.4.7 2.12
CentOS 7.x 3.10.0 4.8.5 2.17
CentOS 6.x 2.6.32 4.4.7 2.12
Fedora 27 4.15.x 7.3.1 2.26
OpenSUSE Leap 42.3 4.4.120 4.8.5 2.22
SLES 12 SP3 4.4.120 4.8.5 2.22
Ubuntu 17.10 4.13 7.2.0 2.26
Ubuntu 16.04.4(**) 4.4.0 5.4.0 2.23
POWER8(***)
RHEL 7.x 3.10.0 4.8.5 2.17 NO 18.x 13.1.6 5.0.0
Ubuntu 16.04.4 4.4.0 5.4.0 2.23 NO 18.x 13.1.6 5.0.0
POWER9(****)
RHEL 7.5 IBM Power LE 4.14.0 4.8.5 2.17 NO 18.x 13.1.6 5.0.0

安装CUDA

我这里选择的是9.0

img

下载完之后,一次输入以下指令:

1
2
3
4
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64.deb
sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda

在下载安装过程中非常慢,因此我换了阿里云的源(不一定需要换源,如果速度够快的话):

  1. 备份原来的源
1
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup.list
  1. 更换源
1
sudo gedit /etc/apt/sources.list

复制以下的源地址:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
  1. 更新
1
sudo apt-get update

安装CUDA之后,需要添加环境变量:

1
2
3
4
5
export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
# 64bit
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# 32bit
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

安装cuDNN

这里注册一下,要做一个下的问卷调查。

进入download界面

勾选I Agree To the Terms of the cuDNN Software License Agreement

我选择的是v7.0.5版本的,记得Runtime Library和Developer Library都要下载,即:

img

一开始我只下载了Developer Library ,在安装的时候出现了这样的错误:

Package libcudnn7 is not installed.

两个都下载完之后,进入deb包目录,打开终端,输入:

1
sudo dpkg -i libcudnn7*.deb

输入nvidia-smi,看到:

img

如果显示报错,建议重启一下

安装TensorFlow

可以查看官网,选择一种安装方式。建议使用 Virtualenv 进行安装

验证安装

进入交互界面,输入以下代码:

1
2
3
4
5
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

如果显示了显卡信息,就基本没有问题了,我的显示如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
/home/lucienxian/Study/cs231n/assignment2/tensorflow/lib/python3.5/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
>>> hello = tf.constant('Hello,world!')
>>> sess = tf.Session()
2018-07-19 14:08:56.590394: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-07-19 14:08:56.675095: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:897] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-07-19 14:08:56.675799: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 0 with properties:
name: GeForce GTX 850M major: 5 minor: 0 memoryClockRate(GHz): 0.8625
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 1.73GiB
2018-07-19 14:08:56.675820: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1471] Adding visible gpu devices: 0
2018-07-19 14:09:14.060705: I tensorflow/core/common_runtime/gpu/gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-07-19 14:09:14.060759: I tensorflow/core/common_runtime/gpu/gpu_device.cc:958] 0
2018-07-19 14:09:14.060766: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0: N
2018-07-19 14:09:14.060961: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1489 MB memory) -> physical GPU (device: 0, name: GeForce GTX 850M, pci bus id: 0000:01:00.0, compute capability: 5.0)
>>> result = sess.run(hello)
>>> sess.close()
>>> print(result)
b'Hello,world!'
>>> exit()