學習tensorflow之mac上安裝tensorflow

背景

聽說谷歌的第二代機器學習的框架 tensorflow 開源了,我也心血來潮去探探大牛的產品.怎奈安裝就折騰了一天,現在整理出來備忘. 

tensorflow官方網站給出的安裝步驟很簡單:

# Only CPU-version is available at the moment.
$ pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

安裝pip

用到了一個 pip 的工具,查了一下pip類似RedHat裏面的yum,安裝Python包非常方便. 

好吧,那就裝一個pip.pip的安裝方法也很簡單,就是前提需要先安裝python.然後去下載 pip的安裝包 ,pip安裝命令如下: 

qyfmac$ tar zxvf pip-7.1.2.tar.gz
qyfmac$ cd pip-7.1.2
qyfmac$ python setup.py install

安裝時報錯了:

qyfmac$ python setup.py install
running install
Checking .pth file support in /Library/Python/2.7/site-packages/
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
	[Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-38643.pth'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
	/Library/Python/2.7/site-packages/
Perhaps your account does not have write access to this directory?  If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account.  If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.
For information on other options, you may wish to consult the
documentation at:
  https://pythonhosted.org/setuptools/easy_install.html
Please make the appropriate changes for your system and try again.

裏面有個 Permission denied 意思是權限不足,我們價格sudo繼續執行. 

qyfmac$ sudo python setup.py install

安裝完後執行命令 pip freeze 列出安裝的packages驗證一下pip安裝好沒. 

qyfmac$ pip freeze
altgraph==0.10.2
bdist-mpkg==0.5.0
bonjour-py==0.3
macholib==1.5.1
matplotlib==1.3.1
modulegraph==0.10.4
numpy==1.10.1
py2app==0.7.3
...

列出了好多包,我唯一沒搞懂的就是我什麼時候裝了這麼多包.

安裝tensorflow

到了我們的主角出場了.執行安裝命令安裝tensorflow.

qyfmac$ pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

悲劇依舊發生了,googleapis.com這個鬼要弄把梯子才能訪問,下載各種超時.

黃天不負苦心人,有一有心人已經下好並上傳的了百度雲 http://pan.baidu.com/s/1ntjaMnf 密碼:sznb. 

下載下來安裝之.

qyfmac$ pip install --upgrade tensorflow-0.5.0-py2-none-any.whl

安裝virtualenv

virtualenv是python的沙箱工具.我們畢竟是在自己機器上做實驗,爲了不來回修改各種環境變量,我們一般還是弄個沙箱完比較好.測試完直接刪除就行,不用再去改各種配置文件.

用pip命令來安裝:

qyfmac$ sudo pip install --upgrade virtualenv

安裝好後創建一個工作目錄,我直接在home裏創建了個文件夾.

qyfmac$ virtualenv --system-site-packages ~/tensorflow

然後進入目錄激活沙箱.

qyfmac$ cd ~/tensorflow
qyfmac$ source bin/activate 
(tensorflow) qyfmac$ 

在virtualenv裏安裝tensorflow

把下載下來的 tensorflow-0.5.0-py2-none-any.whl 文件放到 ~/tensorflow 目錄裏. 

進入沙箱後,執行命令來安裝tensorflow在沙箱中.

(tensorflow) qyfmac$ pip install --upgrade tensorflow-0.5.0-py2-none-any.whl

運行tensorflow

我是在virtualenv裏運行的.直接在系統裏執行方式是一樣的.

照着官方文檔敲了個 簡單例子 . 

(tensorflow) qyfmac$ python
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello,TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello)
Hello, TensorFlow!
>>> 

在敲 see = tf.Session() 這行時會報一個錯 

can't determine number of CPU cores: assuming 4 
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4

可以先不用理會,不影響最終執行結果.

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章