Install GPU TensorFlow From Sources w/ Ubuntu 16.04 and Cuda 8.0

简介:

In this tutorial I will be going through the process of building the latest TensorFlow from sources for Ubuntu 16.04.  TensorFlow now supports using Cuda 8.0 & CuDNN 5.1 so you can use the pip’s from their website for a much easier install.  If you would like to install into a Anaconda environment the easiest method is to ‘conda install pip’ and just use the pip packages. If you prefer to build from sources using Ubuntu 14.04 please see my other tutorial.

In order to use TensorFlow with GPU support you must have a Nvidia graphic card with a minimum compute capability of 3.0.

Getting started I am going to assume you know some of the basics of using a terminal in Linux.


Install Required Packages

Open a terminal by pressing Ctrl + Alt + T
Paste each line one at a time (without the $) using Shift + Ctrl + V

$ sudo apt-get install openjdk-8-jdk git python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev


Update & Install Nvidia Drivers

You must also have the 367 (or later) NVidia drivers installed, this can easily be done from Ubuntu’s built in additional drivers after you update your driver packages.

$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt update

Once installed using additional drivers restart your computer.  If you experience any troubles booting linux or logging in: try disabling fast & safe boot in your bios and modifying your grub boot options to enable nomodeset.


Install Nvidia Toolkit 8.0 & CudNN

Skip if not installing with GPU support

To install the Nvidia Toolkit  download base installation .run file from Nvidiawebsite.  MAKE SURE YOU SAY NO TO INSTALLING NVIDIA DRIVERS! Also make sure you select yes to creating a symbolic link to your cuda directory.

$ cd ~/Downloads # or directory to where you downloaded file
$ sudo sh cuda_8.0.44_linux.run --override # hold s to skip

This will install cuda into: /usr/local/cuda

To install CudNN download cudNN v5.1 for Cuda 8.0 from Nvidia website and extract into /usr/local/cuda via:

$ sudo tar -xzvf cudnn-8.0-linux-x64-v5.1.tgz
$ 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*

Then update your bash file:

$ gedit ~/.bashrc

This will open your bash file in a text editor which you will scroll to the bottom and add these lines:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda

Once you save and close the text file you can return to your original terminal and type this command to reload your .bashrc file:

$ source ~/.bashrc

Install Bazel

Instructions also on Bazel website

$ 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
$ sudo apt-get upgrade bazel

Clone TensorFlow

$ cd ~
$ git clone https://github.com/tensorflow/tensorflow


Configure TensorFlow Installation

$ cd ~/tensorflow
$ ./configure

Use defaults by pressing enter for all except:

Please specify the location of python. [Default is /usr/bin/python]:

For Python 2 use default or If you wish to build for Python 3 enter:

$ /usr/bin/python3.5

Please input the desired Python library path to use. Default is [/usr/local/lib/python2.7/dist-packages]:

For Python 2 use default or If you wish to build for Python 3 enter:

$ /usr/local/lib/python3.5/dist-packages

Unless you have a Radeon graphic card you can say no to OpenCL support. (has anyone tested this? ping me if so!)

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 Cudnn version you want to use. [Leave empty to use system default]:

$ 5

You can find the compute capability of your device at:https://developer.nvidia.com/cuda-gpus

If all was done correctly you should see:

INFO: All external dependencies fetched successfully.
Configuration finished


Build TensorFlow

Warning Resource Intensive I recommend having at least 8GB of computer memory.

If you want to build TensorFlow with GPU support enter:

$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

For CPU only enter:

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package


Build & Install Pip Package

This will build the pip package required for installing TensorFlow in your /tmp/ folder

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

To Install Using Python 3 (remove sudo if using a virtualenv)

$ sudo pip3 install /tmp/tensorflow_pkg/tensorflow

# with no spaces after tensorflow hit tab before hitting enter to fill in blanks

For Python 2 (remove sudo if using a virtualenv)

$ sudo pip install /tmp/tensorflow_pkg/tensorflow

# with no spaces after tensorflow hit tab before hitting enter to fill in blanks


Test Your Installation

Close all your terminals and open a new terminal to test.

$ python # or python3
$ import tensorflow as tf
$ sess = tf.InteractiveSession()
$ sess.close()

TensorFlow also has instructions on how to do a basic test and a list of common installation problems.

There you have it, you should now have TensorFlow installed on your computer. This tutorial was tested on a fresh install of Ubuntu 16.04 with a GeForce GTX 780 and a GTX 970m.

If you want to give your GPU a workout maybe try building a massive image classifier following this tutorial.

https://alliseesolutions.wordpress.com/2016/09/08/install-gpu-tensorflow-from-sources-w-ubuntu-16-04-and-cuda-8-0-rc/



本文转自 stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1890536

相关实践学习
基于阿里云DeepGPU实例,用AI画唯美国风少女
本实验基于阿里云DeepGPU实例,使用aiacctorch加速stable-diffusion-webui,用AI画唯美国风少女,可提升性能至高至原性能的2.6倍。
相关文章
|
3月前
|
机器学习/深度学习 并行计算 API
【GPU】CUDA是什么?以及学习路线图!
【GPU】CUDA是什么?以及学习路线图!
102 0
|
4月前
|
并行计算 TensorFlow 算法框架/工具
win10上使用gpu版的tensorflow
win10上使用gpu版的tensorflow
|
5月前
|
并行计算 TensorFlow 算法框架/工具
TensorFlow识别GPU难道就这么难吗?还是我的GPU有问题?
TensorFlow识别GPU难道就这么难吗?还是我的GPU有问题?
|
5月前
|
TensorFlow 算法框架/工具 异构计算
Windows部署TensorFlow后识别GPU失败,原因是啥?
Windows部署TensorFlow后识别GPU失败,原因是啥?
|
5月前
|
并行计算 Linux Docker
Docker【部署 07】镜像内安装tensorflow-gpu及调用GPU多个问题处理Could not find cuda drivers+unable to find libcuda.so...
Docker【部署 07】镜像内安装tensorflow-gpu及调用GPU多个问题处理Could not find cuda drivers+unable to find libcuda.so...
325 0
|
26天前
|
机器学习/深度学习 并行计算 TensorFlow
TensorFlow与GPU加速:提升深度学习性能
【4月更文挑战第17天】本文介绍了TensorFlow如何利用GPU加速深度学习, GPU的并行处理能力适合处理深度学习中的矩阵运算,显著提升性能。TensorFlow通过CUDA和cuDNN库支持GPU,启用GPU只需简单代码。GPU加速能减少训练时间,使训练更大、更复杂的模型成为可能,但也需注意成本、内存限制和编程复杂性。随着技术发展,GPU将继续在深度学习中发挥关键作用,而更高效的硬件解决方案也将备受期待。
|
28天前
|
机器学习/深度学习 并行计算 算法框架/工具
Anaconda+Cuda+Cudnn+Pytorch(GPU版)+Pycharm+Win11深度学习环境配置
Anaconda+Cuda+Cudnn+Pytorch(GPU版)+Pycharm+Win11深度学习环境配置
115 3
|
2月前
|
并行计算 API C++
GPU 硬件与 CUDA 程序开发工具
GPU 硬件与 CUDA 程序开发工具
53 0
|
3月前
|
并行计算 API 开发工具
【GPU】GPU 硬件与 CUDA 程序开发工具
【GPU】GPU 硬件与 CUDA 程序开发工具
44 0
|
3月前
|
机器学习/深度学习 并行计算 流计算
【GPU】GPU CUDA 编程的基本原理是什么?
【GPU】GPU CUDA 编程的基本原理是什么?
53 0