Ubuntu16.04 源码安装tensorflow(GPU版)

来源:互联网 发布:最全网络金融理财产品 编辑:程序博客网 时间:2024/05/01 03:35

一、cuda及cuDNN安装

这部分参见本人另外一篇博客Ubuntu16.04 Caffe安装笔记 中的一二三章节。

二、Bazel的安装

sudo apt-get install openjdk-8-jdksudo add-apt-repository ppa:webupd8team/javasudo apt-get update && sudo apt-get install oracle-java8-installersudo apt-get install curl

最好重启一下电脑

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listcurl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -sudo apt-get update && sudo apt-get install bazelsudo apt-get upgrade bazel

这部分的具体详情可以参考bazel官网指南

其他依赖安装

sudo apt-get install default-jdk python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel python3-wheel libcurl3-dev zip zlib1g-dev unzip 

注意上面的zlib1g-dev那是阿拉伯数字1不是l。
因为接下来想安装python3对应的tensorflow,使用基于python3的各种依赖也要安装。

编译tensorflow

下载Tensorflow源码

git clone https://github.com/tensorflow/tensorflow

下载完成后

cd ~/tensorflow  ./configure  

接下来的配置选项如下:

yangyuan@yangyuan-lab:~/tensorflow$ ./configure You have bazel 0.8.1 installed.Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5Found possible Python library paths:  /usr/local/lib/python3.5/dist-packages  /usr/lib/python3/dist-packagesPlease input the desired Python library path to use.  Default is [/usr/local/lib/python3.5/dist-packages]Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: nNo jemalloc as malloc support will be enabled for TensorFlow.Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: nNo Google Cloud Platform support will be enabled for TensorFlow.Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: nNo Hadoop File System support will be enabled for TensorFlow.Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: nNo Amazon S3 File System support will be enabled for TensorFlow.Do you wish to build TensorFlow with XLA JIT support? [y/N]: nNo XLA JIT support will be enabled for TensorFlow.Do you wish to build TensorFlow with GDR support? [y/N]: nNo GDR support will be enabled for TensorFlow.Do you wish to build TensorFlow with VERBS support? [y/N]: nNo VERBS support will be enabled for TensorFlow.Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: nNo OpenCL SYCL support will be enabled for TensorFlow.Do you wish to build TensorFlow with CUDA support? [y/N]: yCUDA support will be enabled for TensorFlow.Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 9.0]: 8.0Please 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 default to cuDNN 7.0]: 5Please 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: 6.1]6.1Do you want to use clang as CUDA compiler? [y/N]: Nnvcc will be used as CUDA compiler.Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: NNo MPI support will be enabled for TensorFlow.Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: Add "--config=mkl" to your bazel command to build with MKL support.Please note that MKL on MacOS or windows is still not supported.If you would like to use a local MKL instead of downloading, please set the environment variable "TF_MKL_ROOT" every time before build.Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: NNot configuring the WORKSPACE for Android builds.Configuration finished

其中因为我要配置成python3版本的tensorflow,所以python路径都用python3的。

而后用bazel来进行编译。

bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_packagebazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg 

这个要花挺长的时间……
完后用pip来安装tensorflow,注意因为前面编译的是python3版本的tensorflow,所以要将系统默认python版本切换为python3,否则安装时会报错说要安装的tensorflow不支持当前平台。

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

如果以后要切换回Python2,执行以下命令就好了:

sudo update-alternatives --config python

安装tensorflow:

sudo pip3 install /tmp/tensorflow_pkg/tensorflow*

因为后面whl文件是编译生成,个人版本应该有差异,故可以通过一个通配符来表示。
安装完后后重启电脑。


原创粉丝点击