学习tensorflow之mac上安装tensorflow

11/24/2015来源:python人气:6306

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

可以先不用理会,不影响最终执行结果.

参考

关于作者