一、源代碼編譯
這里要為僅支持 CPU 的 TensorFlow 構建一個 pip 軟件包,需要調用以下命令:
$ bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=opt --verbose_failures //tensorflow/tools/pip_package:build_pip_package
提示:默認情況下,從源代碼構建 TensorFlow 會消耗大量的 RAM。如果您系統中的 RAM 資源有限,則可以通過在調用 bazel 時指定 --local_resources 2048,.5,1.0 來限制 RAM 使用量。
編譯期間因為缺少模塊中斷了幾次,前后大概編譯了幾個小時,終於編譯成功了!
Target //tensorflow/tools/pip_package:build_pip_package up-to-date:
bazel-bin/tensorflow/tools/pip_package/build_pip_package
INFO: Elapsed time: 1226.516s, Critical Path: 54.42s
INFO: 904 processes: 904 local.
INFO: Build completed successfully, 1126 total actions
二、生成whl文件(.whl 文件的名稱取決於您的平台)
bazel build 命令會構建一個名為 build_pip_package 的腳本。按如下所示運行此腳本后,一個 .whl 文件將在 /tmp/tensorflow_pkg 目錄內構建:
➜ tensorflow git:(master) bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
2018年 8月13日 星期一 02時25分45秒 CST : === Preparing sources in dir: /var/folders/6v/q8bvbf3n209f0_59508px3pr0000gn/T/tmp.XXXXXXXXXX.nuoSUsnJ
/Volumes/DATA/AI/tensorflow /Volumes/DATA/AI/tensorflow
/Volumes/DATA/AI/tensorflow
2018年 8月13日 星期一 02時26分09秒 CST : === Building wheel
warning: no files found matching '*.dll' under directory '*'
warning: no files found matching '*.lib' under directory '*'
warning: no files found matching '*.h' under directory 'tensorflow/include/tensorflow'
warning: no files found matching '*' under directory 'tensorflow/include/Eigen'
warning: no files found matching '*.h' under directory 'tensorflow/include/google'
warning: no files found matching '*' under directory 'tensorflow/include/third_party'
warning: no files found matching '*' under directory 'tensorflow/include/unsupported'
2018年 8月13日 星期一 02時26分42秒 CST : === Output wheel file is in: /tmp/tensorflow_pkg
三、安裝
首先找到具體的文件名稱
➜ tensorflow git:(master) ll /tmp/tensorflow_pkg
total 98136
-rw-r--r-- 1 mazhiyong wheel 48M 8 13 02:26 tensorflow-1.10.0-cp27-cp27m-macosx_10_13_x86_64.whl
然后啟動安裝
➜ tensorflow git:(master) sudo pip2 install /tmp/tensorflow_pkg/tensorflow-1.10.0-cp27-cp27m-macosx_10_13_x86_64.whl
四、驗證
1、首先切換工作目錄到tensorflow目錄外,切記!
➜ tensorflow git:(master) cd ~
2、調用 Python2:
$ python2
3、驗證
在 Python 交互式 shell 中輸入以下幾行簡短的程序代碼:
# Python
importtensorflow astf
hello =tf.constant('Hello, TensorFlow!')
sess =tf.Session()
print(sess.run(hello))
如果系統輸出以下內容,就說明您可以開始編寫 TensorFlow 程序了:
Hello, TensorFlow!
從二進制安裝方式到源碼編譯方式前后折騰了兩周,終於看到結果啦!