$ 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
編譯tensorflow前須要的依賴:html
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.list
git
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
。
須要使用到的python依賴有:
爲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
若是不使用GPU就能夠跳過這節。
最後必須安裝libcupti-dev: $ sudo apt-get install libcupti-dev
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
。
使用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
執行pip install
來安裝, 會生成一個.whl
的文件。例如在linux上的Tensorlow 1.2.0:$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.0-py2-none-any.whl
使用terminal進入python,輸入
# Python import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello))
若是輸出Hello, TensorFlow!
就表示成功了!
AttributeError: 'module' object has no attribute 'Default'
,那麼執行sudo pip install --upgrade protobuf