Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

TensorFlow 原始碼編譯安裝全流程(含 GPU 支援)

安裝環境

本文以 Ubuntu 14.04 為示範環境。

安裝 Java 8

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

安裝其他必要工具

apt-get install pkg-config zip g++ zlib1g-dev unzip

安裝 Bazel

下載安裝包後賦予執行權限並安裝:

chmod +x bazel-version-installer-os.sh
./bazel-version-installer-os.sh --user

安裝 Python 相關相依

sudo apt-get install python-numpy swig python-dev python-wheel

設定編譯參數

執行 ./configure,依提示指定 Python 路徑、是否啟用 GPU、GCC 主機編譯器、CUDA SDK 版本與路徑、cuDNN 版本與路徑,以及要編譯的 CUDA compute capability(例如 3.5)。範例輸出如下:

Please specify the location of python. [Default is /usr/bin/python]:
Do you wish to build TensorFlow with GPU support? [y/N] y
Please specify the Cuda SDK version you want to use. [Leave empty to use system default]: 7.5
Please specify the location where CUDA 7.5 toolkit is installed. [default is: /usr/local/cuda]: /usr/local/cuda
Please specify the Cudnn version you want to use. [Leave empty to use system default]: 4.0.4
Please specify the location where the cuDNN 4.0.4 library is installed. [default is: /usr/local/cuda]: /usr/local/cudnn-r4-rc/
Please specify a list of comma-separated Cuda compute capabilities you want to build with. [Default is: "3.5,5.2"]: 3.5
Configuration finished

編譯 GPU 版本

加上 --config=cuda 參數即可啟用 GPU 支援:

bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer
bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu

此範例會在 GPU 上反覆計算 2x2 矩陣的主要特徵值,輸出結尾的 lambda 與向量會逐步收斂。

注意事項

雖然可在同一原始碼樹中同時建置 CUDA 與非 CUDA 版本,但切換這兩種設定時,建議先執行 bazel clean。此外,必須先執行 configure 再執行 bazel build,否則編譯會失敗並顯示明確錯誤訊息。

Copyright © Linux教程網 All Rights Reserved