1. 安裝Tensorflow
環境: macOS Sierra 10.12.6
2. 安裝方式
在tensorflow的官方指導下,利用virtualenv安裝。
3. 安裝步驟
3.1 安裝virtualenv工具。
$sudo pip install --upgrade virtualenv
3.2 為tensorflow創建環境目錄
$ virtualenv --system-site-packages ~/tensorflow$cd~/tensorflow
注釋:上面的命令行都是根據tensorflow的官網教程安裝的,忘記記錄了,下面開始本人的記實操作步驟。
3.3 進入virtualenv環境并安裝tensorflow(本人是在VPN環境下安裝的,不知道非VPN環境是否被墻)
[wai@waideMacBook ~/tensorflow]$cd bin/
[wai@waideMacBook ~/tensorflow/bin]$source activate
(tensorflow)[wai@waideMacBook ~/tensorflow/bin]$pip install --upgrade tensorflow
Collecting tensorflow
Downloading tensorflow-1.3.0-cp27-cp27m-macosx_10_11_x86_64.whl (39.4MB)
100% |████████████████████████████████| 39.4MB 20kB/s
Collecting six>=1.10.0 (from tensorflow)
Using cached six-1.10.0-py2.py3-none-any.whl
中間省去各種 Collecting...
Building wheels for collected packages: html5lib, markdown
Running setup.py bdist_wheel for html5lib ... done
Stored in directory: /Users/wai/Library/Caches/pip/wheels/6f/85/6c/56b8e1292c6214c4eb73b9dda50f53e8e977bf65989373c962
Running setup.py bdist_wheel for markdown ... done
Stored in directory: /Users/wai/Library/Caches/pip/wheels/bf/46/10/c93e17ae86ae3b3a919c7b39dad3b5ccf09aeb066419e5c1e5
Successfully built html5lib markdown
Installing collected packages: six, protobuf, backports.weakref, numpy, werkzeug, html5lib, markdown, bleach, tensorflow-tensorboard, funcsigs, pbr, mock, tensorflow
Found existing installation: six 1.4.1
Not uninstalling six at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, outside environment /Users/wai/tensorflow
Found existing installation: numpy 1.8.0rc1
Not uninstalling numpy at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, outside environment /Users/wai/tensorflow
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 funcsigs-1.0.2 html5lib-0.9999999 markdown-2.6.9 mock-2.0.0 numpy-1.13.1 pbr-3.1.1 protobuf-3.4.0 six-1.10.0 tensorflow-1.3.0 tensorflow-tensorboard-0.1.6 werkzeug-0.12.2
4. 初用tensorflow,hello tensorflow!
[wai@waideMacBook ~/tensorflow/bin]$source activate
(tensorflow)[wai@waideMacBook ~/tensorflow/bin]$python
Python 2.7.10 (default, Feb? 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
注:因為在virtualenv下安裝,所以只能在該環境的python中import tensorflow。
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-09-07 22:42:56.980323: 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-09-07 22:42:56.980373: 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-09-07 22:42:56.980392: 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-09-07 22:42:56.980410: 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.
>>> print(sess.run(hello))
Hello, TensorFlow!
果不其然,雖然都是按照官方指導來操作的,但是還是多多少少遇到一些WARN信息。上面總的意思就是說剛才安裝的tensorflow沒有采用這四個計算效率更高的庫(SSE4.2/AVX/AVX2/FMA)來進行編譯,原因為我僅僅是通過pip install來直接安裝的。pip install這種安裝方式其實就是把人家預編譯的tensorflow下載下來然后放置到我電腦的python PATH中,而別人預編譯的環境沒有采用這四個庫來進行編譯。如果我想在使用tensorflow的過程中獲得更快的計算效率,就得下載tensorflow的源碼,通過build的方式來安裝。這一步以后再做吧,先熟悉環境。暫且記錄下來。之后搞定了再更新。
當然畢竟只是WARN,hello tensorflow是可以正常print的。如果要臨時消除這些煩人的WARN,可以通過修改環境變量來屏蔽。
TF(tensorflow)有一個環境變量TF_CPP_MIN_LOG_LEVEL,默認為0:顯示所有lOG,往上分別可以設置為:
1:屏蔽INFO級別LOG
2:屏蔽WARN級別LOG
3:屏蔽ERROR級別LOG
>>> import os
>>> os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
WARN被屏蔽啦。