Ubuntu16 04 源码安装tensorflow GPU版

                       

一、cuda及cuDNN安装

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

二、Bazel的安装

sudo apt-get install openjdk-8-jdk#sudo add-apt-repository ppa:webupd8team/java#sudo apt-get update && sudo apt-get install oracle-java8-installersudo apt-get install curl
   
   
  • 1
  • 2
  • 3
  • 4

最好重启一下电脑

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
   
   
  • 1
  • 2
  • 3
  • 4

这部分的具体详情可以参考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 
   
   
  • 1

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

编译tensorflow

下载Tensorflow源码

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

下载完成后

cd ~/tensorflow  ./configure  
   
   
  • 1
  • 2

接下来的配置选项如下:

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
   
   
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

其中因为我要配置成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 ~/tensorflow_pkg 
   
   
  • 1
  • 2
  • 3

这个要花挺长的时间……
完后用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
   
   
  • 1
  • 2

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

sudo update-alternatives --config python
   
   
  • 1

安装tensorflow:

sudo pip3 install ~/tensorflow_pkg/tensorflow*
   
   
  • 1

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


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43678480/article/details/86358036