Mac OSX 上安裝 TensorFlow [CPU support only]
本文介紹在 Mac OSX
系統(tǒng)上如何安裝 Tensorflow ,但除了操作系統(tǒng)包管理有差異碳抄,其它內(nèi)容使用于其它操作系統(tǒng)愉老。
TensorFlow 可以在 Python 2 中運(yùn)行,但纳鼎,Python 3 才是未來俺夕。所以裳凸,建議大家直接使用 Python 3贱鄙!
注:本文安裝的是 TensorFlow with CPU support only
劝贸;電腦上沒有NVIDIA 顯卡,所以我理解應(yīng)該沒法安裝 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 是一個體驗(yàn)特別好的 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!'
說明已經(jīng)成功安裝 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
信息并不影響學(xué)習(xí) TensorFlow
引有,只是會導(dǎo)致 TensorFlow
運(yùn)行的不夠快。但倦逐,如果你還是不希望看到這些 Warning
譬正,可以用下面的三種方法之一。
第一種僅僅是讓你不再看到 Warning
檬姥,而最后兩種能讓 TensorFlow
運(yùn)行的更快曾我!
設(shè)置 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
更多健民,請參考:https://github.com/yaroslavvb/tensorflow-community-wheels