在Mac上從源代碼安裝TensorFlow
來自專欄 沉思錄
緣起
作為一個喜歡裝逼的程序員,這年頭不整一下 TensorFlow,都感覺和時代脫節了。
雖然使用pip 安裝TF比較簡單,但這種方式沒有針對本地環境做優化。調用時會警告說你的機器支持一些可加速運算的指令但編譯時沒有啟用,讓你心癢難耐。
於是在一個月黑風高的夜晚,我終於下手從源碼編譯安裝TF。
環境
機器為Mid 2014,升級到maxOS 10.13,已經安裝了brew和git。本人採用系統自帶的Python 2.7.14。
安裝bazel
$ brew install bazel
安裝完後檢查一下是否成功
$ bazel versionBuild label: 0.7.0-homebrewBuild target: bazel-out/darwin_x86_64-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jarBuild time: Thu Oct 19 09:13:24 2017 (1508404404)Build timestamp: 1508404404Build timestamp as int: 1508404404
下載源碼
執行以下命令下載TensorFlow源代碼
$ git clone https://github.com/tensorflow/tensorflow
配置
進入TensorFlow源碼目錄,運行./configur
$ cd tensorflow $ ./configure
基本上一路回車用默認選項就好了,反正看起來比較牛的選項 macOS 都一如既往的不支持
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]:Google Cloud Platform support will be enabled for TensorFlow.Do you wish to build TensorFlow with Hadoop File System support? [Y/n]:Hadoop File System support will be enabled for TensorFlow.Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: nNo Amazon S3 File System support will be enabled for TensorFlow.Do you wish to build TensorFlow with XLA JIT support? [y/N]:No XLA JIT support will be enabled for TensorFlow.Do you wish to build TensorFlow with GDR support? [y/N]:No GDR support will be enabled for TensorFlow.Do you wish to build TensorFlow with VERBS support? [y/N]:No VERBS support will be enabled for TensorFlow.Do you wish to build TensorFlow with OpenCL support? [y/N]:No OpenCL support will be enabled for TensorFlow.Do you wish to build TensorFlow with CUDA support? [y/N]:No CUDA support will be enabled for TensorFlow.Do you wish to build TensorFlow with MPI support? [y/N]:No MPI support will be enabled for TensorFlow.
編譯
執行下面的命令開始編譯
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
然後我的 MacBook Pro 進入供暖模式。在漫長的57分鐘之後終於編譯完畢:
Target //tensorflow/tools/pippackage:buildpip_package up-to-date:bazel-bin/tensorflow/tools/pippackage/buildpip_packageINFO: Elapsed time: 3477.645s, Critical Path: 106.73
之後可以創建pip包
bazel-bin/tensorflow/tools/pippackage/buildpippackage /tmp/tensorflowpk
生成一個.whl文件在 /tmp/tensorflow_pkg 中。
安裝
沒有安裝過wheel包的話,要先裝它
pip install wheel
然後安裝新創建的whl
pip install /tmp/tensorflowpkg/tensorflow-1.4.0rc1-cp27-cp27m-macosx106intel.wh
安裝成功:
Successfully installed
Hello TensorFlow
忙活了半天,終於可以寫個 TensorFlow 版本的 Hello World來玩玩了
import tensorflow as tfhello = tf.constant(Hello, TensorFlow!)sess = tf.Session()print(sess.run(hello))
然後出現令人激動的
Hello, TensorFlow!
參考
https://www.tensorflow.org/install/install_sources
推薦閱讀:
※TensorFlow應用實戰 | TensorFlow基礎知識
※TensorFlow初步(2)
※使用TensorFlow分類手寫數字
※TensorFlow學習筆記之四——源碼分析之基本操作
※評論上的情感分析:主題與情感詞抽取
TAG:TensorFlow | 機器學習 |