1. 程式人生 > >TensorFlow入門——安裝(帶GPU)

TensorFlow入門——安裝(帶GPU)

incr per 下載地址 變量 環境 warning ice mman sign

這一系列基本上是屬於我自己進行到了那個步驟就做到那個步驟的

由於新裝了GPU (GTX750ti)和CUDA9.0、CUDNN7.1版本的軟件,所以希望TensorFlow能在GPU上運行,也算上補上之前的承諾

說了下初衷,由於現在新的CUDA版本對TensorFlow的支持不好,只能采取編譯源碼的方式進行

所以大概分為以下幾個步驟

1.安裝依賴庫(這部分我已經做過了,不進行介紹,可以看前邊的依賴庫,基本一致)

2.安裝Git(有的就跳過這一步)

3.安裝TensorFlow的build工具bazel

4.編譯TensorFlow源碼

5.安裝並配置環境變量

1.安裝依賴庫

2.安裝Git

使用

sudo
apt-get install git

3. 安裝TensorFlow的build工具bazel

這一步比較麻煩,是因為apt-get中沒有bazel這個工具

因此需要到GitHub上先下載,再進行安裝 下載地址是https://github.com/bazelbuild/bazel/releases

選擇正確版本下載,並使用sudo命令安裝.sh文件即可

4.編譯TensorFlow源碼

這一步特別麻煩,有很多選項需要選擇,我的選擇如下:

技術分享圖片
 1 jourluohua@jour:~/tools/tensorflow$ ./configure 
 2 WARNING: Running Bazel server needs to be killed, because the startup options are different.
3 You have bazel 0.14.1 installed. 4 Please specify the location of python. [Default is /usr/bin/python]: 5 6 7 Found possible Python library paths: 8 /usr/local/lib/python2.7/dist-packages 9 /usr/lib/python2.7/dist-packages 10 Please input the desired Python library path to use. Default is [/usr/local/lib/python2.7
/dist-packages] 11 12 Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: Y 13 jemalloc as malloc support will be enabled for TensorFlow. 14 15 Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n 16 No Google Cloud Platform support will be enabled for TensorFlow. 17 18 Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n 19 No Hadoop File System support will be enabled for TensorFlow. 20 21 Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n 22 No Amazon S3 File System support will be enabled for TensorFlow. 23 24 Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: n 25 No Apache Kafka Platform support will be enabled for TensorFlow. 26 27 Do you wish to build TensorFlow with XLA JIT support? [y/N]: y 28 XLA JIT support will be enabled for TensorFlow. 29 30 Do you wish to build TensorFlow with GDR support? [y/N]: y 31 GDR support will be enabled for TensorFlow. 32 33 Do you wish to build TensorFlow with VERBS support? [y/N]: y 34 VERBS support will be enabled for TensorFlow. 35 36 Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N 37 No OpenCL SYCL support will be enabled for TensorFlow. 38 39 Do you wish to build TensorFlow with CUDA support? [y/N]: y 40 CUDA support will be enabled for TensorFlow. 41 42 Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 8 43 44 45 Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 46 47 48 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 49 50 51 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 52 53 54 Do you wish to build TensorFlow with TensorRT support? [y/N]: N 55 No TensorRT support will be enabled for TensorFlow. 56 57 Please specify the NCCL version you want to use. [Leave empty to default to NCCL 1.3]: 58 59 60 Please specify a list of comma-separated Cuda compute capabilities you want to build with. 61 You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. 62 Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0] 63 64 65 Do you want to use clang as CUDA compiler? [y/N]: N 66 nvcc will be used as CUDA compiler. 67 68 Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 69 70 71 Do you wish to build TensorFlow with MPI support? [y/N]: N 72 No MPI support will be enabled for TensorFlow. 73 74 Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 75 76 77 Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N 78 Not configuring the WORKSPACE for Android builds. 79 80 Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details. 81 --config=mkl # Build with MKL support. 82 --config=monolithic # Config for mostly static monolithic build. 83 Configuration finished
View Code

然後使用bazel進行編譯(本步驟非常容易出問題,而且特別耗時)

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

5.安裝並配置環境變量

使用pip進行安裝

$ sudo pip install /tmp/tensorflow_pkg/tensorflow

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

最後就是測試

import tensorflow as tf
sess = tf.InteractiveSession()
sess.close()

如果每一步都不報錯的,TensorFlow就編譯並安裝成功了

TensorFlow入門——安裝(帶GPU)