Mac OSX 上安裝 TensorFlow [CPU support only]

Mac OSX 上安裝 TensorFlow [CPU support only]

本文介紹在 Mac OSX 系統(tǒng)上如何安裝 Tensorflow ,但除了操作系統(tǒng)包管理有差異,其它內容使用于其它操作系統(tǒng)。

TensorFlow 可以在 Python 2 中運行,但,Python 3 才是未來。所以,建議大家直接使用 Python 3!

注:本文安裝的是 TensorFlow with CPU support only ;電腦上沒有NVIDIA 顯卡,所以我理解應該沒法安裝 TensorFlow with GPU support

安裝 Python 3

$ brew install python3

安裝 virtualenv

安裝 virtualenv

$ sudo pip install -U virtualenv virtualenvwrapper

將下面命令加入 ~/.bashrc~/.zshrc,比如我使用zsh,所以加到 ~/.zshrc 文件末尾:

$ echo 'test -f /usr/local/bin/virtualenvwrapper.sh && source /usr/local/bin/virtualenvwrapper.sh' >> ~/.zshrc

安裝 TensorFlow

$ mkvirtualenv -p python3 tensorflow
$ pip install -U tensorflow

安裝 IPython

IPython 是一個體驗特別好的 Python 交互式終端,安裝:

$ workon tensorflow
$ pip install ipython

測試是否安裝成功

$ ipython
In [1]: import tensorflow as tf
In [2]: hello = tf.constant('Hello, TensorFlow!')
In [3]: sess = tf.Session()
In [4]: print(sess.run(hello))
b'Hello, TensorFlow!'

看到成功輸出 b'Hello, TensorFlow!' 說明已經成功安裝 TensorFlow !

這行之后:

In [3]: sess = tf.Session()

可能會看到類似下面的輸出:

2017-06-14 13:50:49.512831: 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.
2017-06-14 13:50:49.512872: 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-06-14 13:50:49.512881: 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-06-14 13:50:49.512894: 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-06-14 13:50:49.512903: 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.

上面輸出中的 W 表示 警告 (Warning),提示從源碼編譯并開啟一些編譯選項后可以加快CPU計算速度。

三種辦法避免這類錯誤

上面的 Warning 信息并不影響學習 TensorFlow ,只是會導致 TensorFlow 運行的不夠快。但,如果你還是不希望看到這些 Warning ,可以用下面的三種方法之一。

第一種僅僅是讓你不再看到 Warning,而最后兩種能讓 TensorFlow 運行的更快!

設置 tensorflow log level,避免 warning 輸出

TF_CPP_MIN_LOG_LEVEL

  • It defaults to 0, showing all logs
  • To filter out INFO set to 1
  • WARNINGS, additionally 2
  • and to additionally filter out ERROR logs set to 3
$ ipython
In [1]: import os
In [2]: os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
In [3]: import tensorflow as tf
In [4]: hello = tf.constant('Hello, TensorFlow!')
In [5]: sess = tf.Session()
In [6]: print(sess.run(hello))
b'Hello, TensorFlow!'
安裝別人編譯好的 TensorFlow

移步lakshayg/tensorflow-build

更多,請參考:https://github.com/yaroslavvb/tensorflow-community-wheels

自己編譯 TensorFlow

請參考官方文檔:Installing TensorFlow from Sources

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容