tensorflow源碼安裝教程

1. 獲取原文件

$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ git checkout Branch # where Branch is the desired branch
$ git checkout r1.0 #r1.0 can be replaced by other version

2. 依賴

編譯tensorflow前須要的依賴:html

    1. bazel
    1. tensorflow的python依賴
    1. 可選: nvidia對tensorflow的GPU支持

2.1 bazel

bazel官網地址 有三種安裝方式:java

  • 1.使用APT安裝 (建議)python

    • 添加Bazel的分佈URI做爲源:linux

      echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listgit

      curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -github

    • 依賴:安裝以前可能須要jdk8依賴,能夠選擇:web

      • google-jdkapi

      • java8-jdkbash

      • java8-sdkoracle

      • oracle-java8-installer

      隨便安裝一個就能夠了

    • 安裝bazel:sudo apt-get update && sudo apt-get install bazel

    • 更新到最新的bazel:sudo apt-get upgrade bazel

  • 2.使用二進制文件安裝

    • 安裝依賴包:sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

    • 下載bazel包,地址。下載bazel-0.5.0-installer-linux-x86_64.sh。

    • 運行安裝:

      chmod +x bazel-0.5.0-installer-linux-x86_64.sh

      ./bazel-0.5.0-installer-linux-x86_64.sh --user

    • 設置環境,在~/.bashrc(使用zsh的在~/.zshrc)文件中加入:export PATH="$PATH:$HOME/bin"

    • 最後再更新下bazel:sudo apt-get upgrade bazel

  • 3.編譯bazel源文件

    • 確保系統中安裝了OpenJDK8,使用命令:sudo apt-get install openjdk-8-jdk

    • 從bazel的release頁面下載源文件

    • 解壓,而後執行:bash ./compile.sh

2.2 python

須要使用到的python依賴有:

  • numpy
  • dev
  • pip
  • wheel

爲python2.7安裝這些依賴: $ sudo apt-get install python-numpy python-dev python-pip python-wheel

爲python3安裝這些依賴: $ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

2.3 nvidia依賴

若是不使用GPU就能夠跳過這節。

  • nvidia硬件:
    • GPU的cuda計算能力必須大於等於3。能夠經過NVIDIA官網查看NVIDIA官方文檔
  • nvidia軟件:
    • NVIDIA's Cuda Toolkit (>= 7.0)。建議使用版本8.0的。
    • NVIDIA的驅動必須有NVIDIA's Cuda Toolkit
    • cuDNN (>= v3)。建議使用版本5.1的。

最後必須安裝libcupti-dev: $ sudo apt-get install libcupti-dev

Mac OS依賴安裝頁面看這裏

3. 開始安裝

tensorflow的文件夾中有一個configure的腳本。這個腳本會向你確認tensorflow依賴的路徑和一些特別要編譯的東西。你必須先運行prior這個腳原本建立pip package和安裝tensorflow。

若是你要使用GPU,那麼在運行configure的時候寫上cuda和cuDNN的版本。若是有多個版本的cuda,那麼就選擇你想要的版本,而不是系統默認的版本。

運行configure腳本會出現一下相似輸出:

$ 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

若是你使用GPU,那麼configure腳本會在你的系統上建立符號連接到cuda。因此每次改變cuda,在執行bazel build以前就從新運行configure

4. 編譯pip package

使用CPU-only,使用:$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

使用GPU,使用:$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

注意: gcc => 5: the binary pip packages available on the TensorFlow website are built with gcc 4, which uses the older ABI. To make your build compatible with the older ABI, you need to add --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" to your bazel build command. ABI compatibility allows custom ops built against the TensorFlow pip package to continue to work against your built package.

Tip: 這種須要不少的RAM,若是你的RAM沒有嗎麼多,能夠限制RAM的使用:--local_resources 2048,.5,1.0

執行bazel build會生成一個build_pip_package的腳本。運行這個腳本會生成一個.whl的文件在/tmp/tensorflow_pkg文件夾:

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

5. 安裝pip package

執行pip install來安裝, 會生成一個.whl的文件。例如在linux上的Tensorlow 1.2.0:$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.0-py2-none-any.whl

6. 驗證你的安裝是否成功

使用terminal進入python,輸入

# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

若是輸出Hello, TensorFlow!就表示成功了!

開始使用tensorflow

一些問題

問題

  • 若是碰到AttributeError: 'module' object has no attribute 'Default',那麼執行sudo pip install --upgrade protobuf
相關文章
相關標籤/搜索