# 僅使用 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
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
--recurse-submodules
參數是必須得, 用於獲取 TesorFlow 依賴的 protobuf 庫.html
首先依照 教程 安裝 Bazel 的依賴. 而後使用下列命令下載和編譯 Bazel 的源碼:python
$ git clone https://github.com/bazelbuild/bazel.git $ cd bazel $ git checkout tags/0.1.0$ ./compile.sh
上面命令中拉取的代碼標籤爲 0.1.0
, 兼容 Tensorflow 目前版本. bazel 的HEAD
版本 (即最新版本) 在這裏可能不穩定.linux
將執行路徑 output/bazel
添加到 $PATH
環境變量中.git
$ sudo apt-get install python-numpy swig python-dev
爲了編譯並運行可以使用 GPU 的 TensorFlow, 須要先安裝 NVIDIA 提供的 Cuda Toolkit 7.0 和 CUDNN 6.5 V2.github
TensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5 的顯卡. 被支持的顯卡 包括但不限於:api
NVidia Titan工具
NVidia Titan Xui
NVidia K20google
NVidia K40spa
將工具安裝到諸如 /usr/local/cuda
之類的路徑.
解壓並拷貝 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
從源碼樹的根路徑執行:
$ ./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 編譯命令.
從源碼樹的根路徑執行:
$ 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 可以提供新的特性支持這樣.