1. 程式人生 > >Ubuntu 16.04編譯安裝TensorFlow 1.7.0 開發環境

Ubuntu 16.04編譯安裝TensorFlow 1.7.0 開發環境

1. Ref

2. Clone TensorFlow repository

  • Clone TensorFlow repo with protobuf sub-modules
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
  • checkout需要的tag
$ cd tensorflow
$ git tag             # 檢視有哪些tag
$ git checkout Branch # where Branch is the desired branch
$ git checkout v1.7.0
# checkout v1.7.0 $ git status # 檢視當前程式碼的狀態

3. 環境安裝 (CPU)

3.1 安裝 bazel

  • 安裝JDK8
$ sudo apt-get install openjdk-8-jdk
  • 增加bazel安裝包源
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://bazel.build/bazel-release.pub
.gpg | sudo apt-key add -
  • 安裝並升級bazel
$ sudo apt-get update && sudo apt-get install bazel
$ sudo apt-get upgrade bazel

3.2 安裝TensorFlow Python依賴包

  • numpy, which is a numerical processing package that TensorFlow requires.
  • dev, which enables adding extensions to Python.
  • pip, which enables you to install and manage certain Python packages.
  • wheel, which enables you to manage Python compressed packages in the wheel (.whl) format.

  • 對於Python2.7,需要執行

$ sudo apt-get install python-numpy python-dev python-pip python-wheel
  • 對於Python3.n, 需要執行
sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
  • 對於Pythoh2.7或Python3.n,在執行TensorFlow的configure時,需要輸入對應的路徑

4. 配置並編譯TensorFlow

4.1 配置TensorFlow

$ ./configure
  • 在重新執行configure之前,需要執行bazel clean
  • gcc版本要求:gcc 5 or later

4.2 編譯pip包

  • 僅支援CPU
$ 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 
  • The bazel build command builds a script named build_pip_package
  • Running this script as follows will build a .whl file within the /tmp/tensorflow_pkg directory
  • 建立.whl檔案
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

安裝pip包

  • 包名依賴於平臺和TensorFlow版本叼,對於TensorFlow 1.7.0 on Linux,安裝命令如下
$ sudo pip3 install --upgrade pip  # 升級pip3
$ sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.7.0-py2-none-any.whl

5. 驗證

ai@aivm:~/tools$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> a = tf.constant(3)
>>> b = tf.constant(5)
>>> c = a + b
>>> sess = tf.Session()
>>> print(sess.run(c))
8
>>> 

6. 安裝jupyter notebook

  • IPython是一個 Python 的一個互動式 shell,它提供了很多內建的函式。Jupyter Notebook是IPython的一個Web介面,其實它也支援其它語言;允許您建立和共享包含實時程式碼,方程,視覺化和說明文字的文件。 用途包括:資料清理和轉換,數值模擬,統計建模,機器學習等等。

  • 安裝Jupyter Notebook

$ sudo pip3 install jupyter 

7. 安裝Virtualenv

  • 安裝Virtualenv
$ sudo apt-get install python3-pip python3-dev python-virtualenv # for Python 3.n
  • 建立Virtualenv環境
$ virtualenv --system-site-packages -p python3 targetDirectory # for Python 3.n
$ virtualenv --system-site-packages -p python3 ~/tensorflow # for Python 3.n例子
  • 啟用Virtualenv環境
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
  • 確認pip ≥8.1
(tensorflow)$ easy_install -U pip
  • 在啟用的Virtualenv環境中安裝TensorFlow
(tensorflow)$ pip install --upgrade tensorflow      # for Python 2.7
(tensorflow)$ pip3 install --upgrade tensorflow     # for Python 3.n
(tensorflow)$ pip install --upgrade tensorflow-gpu  # for Python 2.7 and GPU
(tensorflow)$ pip3 install --upgrade tensorflow-gpu # for Python 3.n and GPU
  • 退出Virtualenv
(tensorflow)$ deactivate 
  • 解除安裝TensorFlow
$ rm -r targetDirectory
$ rm -r ~/tensorflow