TensorFlow源碼編譯-基于Ubuntu 15.04
Ubuntu/Linux直接安裝:
# 僅使用 CPU 的版本 $ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl開啟 GPU 支持的版本 (安裝該版本的前提是已經安裝了 CUDA sdk)
$ pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl</pre>
源碼編譯:
克隆 TensorFlow 倉庫
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
--recurse-submodules
參數是必須得, 用于獲取 TesorFlow 依賴的 protobuf 庫.Linux 安裝
安裝 Bazel
首先依照 教程 安裝 Bazel 的依賴. 然后使用下列命令下載和編譯 Bazel 的源碼:
$ git clone https://github.com/bazelbuild/bazel.git $ cd bazel $ git checkout tags/0.1.0$ ./compile.sh上面命令中拉取的代碼標簽為
0.1.0
, 兼容 Tensorflow 目前版本. bazel 的HEAD
版本 (即最新版本) 在這里可能不穩定.將執行路徑
output/bazel
添加到$PATH
環境變量中.安裝其他依賴
$ sudo apt-get install python-numpy swig python-dev可選: 安裝 CUDA (在 Linux 上開啟 GPU 支持)
為了編譯并運行能夠使用 GPU 的 TensorFlow, 需要先安裝 NVIDIA 提供的 Cuda Toolkit 7.0 和 CUDNN 6.5 V2.
TensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5 的顯卡. 被支持的顯卡 包括但不限于:
-
NVidia Titan
-
NVidia Titan X
-
NVidia K20
-
NVidia K40
下載并安裝 Cuda Toolkit 7.0
將工具安裝到諸如 /usr/local/cuda
之類的路徑.
下載并安裝 CUDNN Toolkit 6.5
解壓并拷貝 CUDNN 文件到 Cuda Toolkit 7.0 安裝路徑下. 假設 Cuda Toolkit 7.0 安裝 在 /usr/local/cuda
, 執行以下命令:
tar xvzf cudnn-6.5-linux-x64-v2.tgz sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64
配置 TensorFlow 的 Cuba 選項
從源碼樹的根路徑執行:
$ ./configure Do you wish to bulid TensorFlow with GPU support? [y/n] y GPU support will be enabled for TensorFlow Please specify the location where CUDA 7.0 toolkit is installed. Refer to README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda Please specify the location where CUDNN 6.5 V2 library is installed. Refer to README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda Setting up Cuda include Setting up Cuda lib64 Setting up Cuda bin Setting up Cuda nvvm Configuration finished
這些配置將建立到系統 Cuda 庫的符號鏈接. 每當 Cuda 庫的路徑發生變更時, 必須重新執行上述 步驟, 否則無法調用 bazel 編譯命令.
編譯目標程序, 開啟 GPU 支持
從源碼樹的根路徑執行:
$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer $ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu# 大量的輸出信息. 這個例子用 GPU 迭代計算一個 2x2 矩陣的主特征值 (major eigenvalue).# 最后幾行輸出和下面的信息類似.000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
注意, GPU 支持需通過編譯選項 "--config=cuda" 開啟.
已知問題
-
盡管可以在同一個源碼樹下編譯開啟 Cuda 支持和禁用 Cuda 支持的版本, 我們還是推薦在 在切換這兩種不同的編譯配置時, 使用 "bazel clean" 清理環境.
-
在執行 bazel 編譯前必須先運行 configure, 否則編譯會失敗并提示錯誤信息. 未來, 我們可能考慮將 configure 步驟包含在編譯過程中, 以簡化整個過程, 前提是 bazel 能夠提供新的特性支持這樣.
來自: http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/basic_usage.html