1. 程式人生 > >Installing CUDA 8.0 + cuDNN 5.1 + TensorFlow with Ubuntu 14.04 (下)

Installing CUDA 8.0 + cuDNN 5.1 + TensorFlow with Ubuntu 14.04 (下)

這篇文章因為篇幅原因分為上下兩篇,此為下篇,上篇連結為:

Step 2. Install NVIDIA cuDNN

Once the CUDA Toolkit is installed, download cuDNN v5.1 Library(或者從百度雲下載) for Linux (note that you will need to register for the Accelerated Computing Developer Program).

Once downloaded, uncompress the files and copy them into the CUDA Toolkit directory (assumed here to be in /usr/local/cuda/)

sudo tar -xvf cudnn-8.0-linux-x64-v5.1.tgz -C /usr/local

Configure the Environment

Finally we need to configure the environment to work with CUDA and cuDNN. Add to the following lines to your ~/.profile file.

export CUDA_HOME=/usr/local/cuda
export CUDA_ROOT=/usr/local/cuda
export PATH=$PATH:$CUDA_ROOT/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_ROOT/lib64
Step 3. Install and upgrade PIP
TensorFlow itself can be installed using the pip package manager. First, make sure that your system has pip installed and updated: - See more at: http://www.nvidia.com/object/gpu-accelerated-applications-tensorflow-installation.html#sthash.5lFpw96U.dpuf TensorFlow itself can be installed using the pip package manager. First, make sure that your system has pip installed and updated: - See more at: http://www.nvidia.com/object/gpu-accelerated-applications-tensorflow-installation.html#sthash.5lFpw96U.dpuf TensorFlow itself can be installed using the pip package manager. First, make sure that your system has pip installed and updated
sudo apt-get install python-pip python-dev
sudo pip install --upgrade pip

Step 4. Install Bazel

To build TensorFlow from source, the Bazel build system must first be installed as follows. Full details are available here.

sudo apt-get install software-properties-common swig
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get install bazel - See more at: http://www.nvidia.com/object/gpu-accelerated-applications-tensorflow-installation.html#sthash.5lFpw96U.dpuf

Step 5. Install TensorFlow
Start the process of building TensorFlow by cloning a TensorFlow repository.
To clone the latest TensorFlow repository, issue the following command:

git clone https://github.com/tensorflow/tensorflow 
The preceding git clone command creates a subdirectory named tensorflow. After cloning, you may optionally build a specific branch (such as a release branch) by invoking the following commands:
cd tensorflow
git checkout Branch # where Branch is the desired branch
For example, to work with the r1.0 release instead of the master release, issue the following command:
git checkout r1.0
Next, you must prepare your environment for Linux or Mac OS

Install TensorFlow Python dependencies
To install TensorFlow, you must install the following packages:
  * numpy, which is a numerical processing package that TensorFlow requires.
  * dev, which enables adding extensions to Python.
  * pip, which enables you to install and manage certain Python packages.
  * wheel, which enables you to manage Python compressed packages in the wheel (.whl) format.
To install these packages for Python 2.7, issue the following command:

sudo apt-get install python-numpy python-dev python-pip python-wheel
To install these packages for Python 3.n, issue the following command:
sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
Finally, you must also install libcupti-dev by invoking the following command:
sudo apt-get install libcupti-dev 
After preparing the environment, you must now configure the installation.
Configure the installation

The root of the source tree contains a bash script named configure. This script asks you to identify the pathname of all relevant TensorFlow dependencies and specify other build configuration options such as compiler flags. You must run this script prior to creating the pip package and installing TensorFlow.
If you wish to build TensorFlow with GPU, configure will ask you to specify the version numbers of Cuda and cuDNN. If several versions of Cuda or cuDNN are installed on your system, explicitly select the desired version instead of relying on the system default.
Here is an example execution of the configure script. Note that your own input will likely differ from our sample input:

$ cd tensorflow  # cd to the top-level directory created
$ ./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA JIT support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]
Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with OpenCL support? [y/N] N
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] Y
CUDA support will be enabled for TensorFlow
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 3.0
Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished

Build the pip package

To build a pip package for TensorFlow with CPU-only support, invoke the following command:

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
To build a pip package for TensorFlow with GPU support, invoke the following command:
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package 
Tip: By default, building TensorFlow from sources consumes a lot of RAM. If RAM is an issue on your system, you may limit RAM usage by specifying --local_resources 2048,.5,1.0 while invoking bazel.
The bazel build command builds a script named build_pip_package. Running this script as follows will build a .whl file within the /tmp/tensorflow_pkg directory:
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
Install the pip package
Invoke pip install to install that pip package. The filename of the .whlfile depends on your platform. For example, the following command will install the pip package for TensorFlow 1.0.1 on Linux:(這一步需要到/tmp/tensorflow_pkg/資料夾下看你的whl檔名)
sudo pip install /tmp/tensorflow_pkg/(your file name).whl
NOTE on gcc version 5: the binary pip packages available on the TensorFlow website are built with gcc4 that uses the older ABI. To make the library compatible with the older abi you have to add -cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"
Step 6. Validate your installation

Validate your TensorFlow installation by doing the following:
1. Start a terminal.
2. Change directory (cd) to any directory on your system other than the tensorflow subdirectory from which you invoked the configure command.
3. Invoke python:

python
4. Enter the following short program inside the python interactive shell:
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
If the Python program outputs the following, then the installation is successful and you can begin writing TensorFlow programs. (If you are new to TensorFlow, see Getting Started with TensorFlow):
Hello, TensorFlow!

Congratulations! All works are done, Enjoy it!