tensorflow from source

環(huán)境:Mac OS EI Captain.
顯卡:Intel Iris Graphics 6100 1536 MB (是的,就是不能GPU加速了:('' )

我開始為了方便直接使用了sudo pip install tensorflow來安裝tensorflow,使用起來沒有問題,但是每次import tensorflow的時候都會出現(xiàn)下面的提醒,一來不勝其煩,二來既然說可以提高性能,那我就看看怎么解決吧。社區(qū)果然也有很多人問這個問題,來看解答,似乎是從source安裝tensorflow會解決這個問題。所以開始重新從source配置tensorflow,按照官網(wǎng)一步一步來。

W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[[0 2]

安裝bazel

我使用了homebrew來安裝,安裝步驟見bazel文檔。

  • 安裝JDK 8
  • 安裝Homebrew (如果你沒有裝過)
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  • 安裝Bazel
$ brew install bazel

裝好后務(wù)必檢查bazel的版本$ bazel version。我安裝好后沒有檢查直接下一步,然后報(bào)錯,提示bazel需要0.4.4以上然而我的bazel是0.2.2。老老實(shí)實(shí)$ brew upgrade bazel,然后再次報(bào)錯無法更新。google一通,原來homebrew很久沒有更新了。$ brew update; brew upgrade bazel。終于bazel版本問題解決。

安裝python dependencies

$ sudo pip install six numpy wheel 

通常應(yīng)該都已經(jīng)安裝過了。

由于我系統(tǒng)無法GPU加速,就跳過GPU配置了,有興趣的可以自己搞,按照官網(wǎng)文檔來,應(yīng)該不難。

配置

在進(jìn)行下一步之前務(wù)必先運(yùn)行下面的命令

$ cd tensorflow  # cd to the top-level directory created
$ ./configure

運(yùn)行命令后出現(xiàn)下面的輸出,按需選擇即可。我選擇了全部默認(rèn)配置,每步回車即可。需要配置GPU的在這里選好Cuda或者cuDNN的版本。

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

Build pip package

Build支持CPU的tensorflow的pip package:

$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

Build支持GPU的tensorflow的pip package:

$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package 

這一步巨慢,對RAM的要求比較高。如果你的電腦RAM是個問題,可以控制--local_resources 2048,.5,1.0,但目測應(yīng)該會更慢(我的RAM沒問題都花了差不多一個小時)。Building過程會有一堆(超大一堆)的warning, 反正我沒有理會。
上面的bazel build命令build了一個名字叫build_pip_package的腳本,運(yùn)行腳本,build一個.whl文件,放在了/tmp/tensorflow_pkg路徑。

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

安裝 pip package

安裝上面build好的pip package。.whl文件的名字根據(jù)不同平臺會不同,自己到/tmp文件夾下查詢。我的文件名如下:

$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.0.1-cp27-cp27m-macosx_10_10_x86_64.whl

安裝至此大功告成。

驗(yàn)證安裝

安裝完畢,需要檢查一下安裝是否OK。

  • 啟動一個terminal
  • cd到除了 configure命令所在的tensorflow的路徑
  • 啟動python
$python

在interactive shell里輸入

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

輸出應(yīng)該為

Hello, TensorFlow!

如果這里發(fā)現(xiàn)文章開頭的那些warning還是出現(xiàn)了,不要?dú)怵H。原因是你依然在調(diào)用原來的tensorflow (直接pip來的那款哦)。只需要先將它卸載,然后運(yùn)行安裝pip package步驟就可以了。

使用source安裝的tensorflow相比較于pip安裝的tensorflow肯定是做了一些優(yōu)化,在CPU上跑,有人說速度可以提升3x, 有人說4-6x,也有人說沒有區(qū)別,我還沒有做過比較。集合大家總的說法來看,在小matrix運(yùn)算上并不會有明顯的優(yōu)勢,但是當(dāng)matrix變大的時候,速度提升還是明顯的。有興趣的同學(xué)可以自我驗(yàn)證一下。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容