Mac下使用源碼編譯安裝TensorFlow CPU版本

這是安裝TensorFlow CPU版本的第二種方法,第一種方法大家可以參見我的
Mac上Anaconda+Tensorflow安裝這篇博文,其實探討這種方法是因爲在用第一種安裝方法安裝好了之後,在運行程序的時候可能會出現:

2017-07-06 22:56:18.520959: 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.
2017-07-06 22:56:18.520985: 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.
2017-07-06 22:56:18.520989: 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.
2017-07-06 22:56:18.520993: 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.

這樣的錯誤,所以參照:TensorFlow CPU環境SSE / AVX / FMA指令集編譯
這篇文章進行探索,但是問題沒有解決,卻突然發現這就是使用源碼編譯的形式來安裝TensorFlow的方法,所以就還是碼篇博文來分享,希望哪個大牛看到也可以順便幫我解決一下前面這幾個警告的問題。廢話不多說了,下面就是安裝的步驟了:

下面的步驟都是參考官方文檔(放在這鎮樓):
Installing TensorFlow from Sources

首先Mac下內置有gcc,可以通過終端命令:
$ gcc -v
查看,查看的結果如下:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

其中的4.2.1就是gcc的版本,或者大家也可以直接輸入終端命令:
$ python
可以看到:

Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org

裏面有一個GCC 4.2.1,感覺比上面那個命令更直觀一些。

然後就是要安裝Google自家的編譯工具bazel了,mac上最好的辦法就是直接使用brew安裝了:

$ brew install bazel

安裝好之後就要下載官方的TensorFlow源碼了:

$ git clone https://github.com/tensorflow/tensorflow

下載完成後會在你的用戶名的目錄下生成一個tensorflow的文件夾,使用

$ cd tensorflow

進入這個文件夾,然後輸入:

./configure

進行配置,在配置過程中會出現一系列的問題,通常情況下沒有特殊的要求,一般全部都選擇n:
這裏寫圖片描述

配置完成後就是編譯過程了,輸入終端命令(輸入此命令後你會發現你的電腦呼呼作響,說實話還是有點小心疼啊,但是爲了學習,我忍了,並且會花一段時間來編譯,大家可以打一盤農藥了):

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

經過漫長的編譯後,任務就基本上完成了,接下來就是輸入下面的命令來生成pip安裝包了:

$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.1-py2-none-any.whl

最後就是用pip命令來安裝TensorFlow了:

$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.1-py2-none-any.whl

至此就完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章