Mac下Tensorflow的安装

我的博客:

柠檬CC

一、用conda创建tensorflow

1、库源问题

参照官方说明我们在anaconda环境下,用conda来安装tensorflow。结果遇到源无法连接的情况。所谓源的概念请参考iphone越狱你添加软件源的情形。

➜  ~ cd anaconda
➜  anaconda sudo  conda create -n tensorflow
Password:
Fetching package metadata ....
WARNING: The remote server could not find the noarch directory for the
requested channel with url: http://pypi.douban.com

It is possible you have given conda an invalid channel. Please double-check
your conda configuration using `conda config --show`.

If the requested url is in fact a valid conda channel, please request that the
channel administrator create `noarch/repodata.json` and associated
`noarch/repodata.json.bz2` files, even if `noarch/repodata.json` is empty.
$ mkdir noarch
$ echo '{}' > noarch/repodata.json
$ bzip2 -k noarch/repodata.json
.

CondaHTTPError: HTTP None None for url <http://pypi.python.com/osx-64/repodata.json>
Elapsed: None

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
ConnectionError(MaxRetryError("HTTPConnectionPool(host='pypi.python.com', port=80): Max retries exceeded with url: /osx-64/repodata.json (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x1119ee710>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))",),)
➜  ~ cd anaconda

2、然后我们这样解决修改源地址,主要在 “.condarc”这个文件里面。我们添加清华源:Index of /anaconda/pkgs/free/

➜  ~ cd anaconda
➜  anaconda sudo conda config --add channels Index of /anaconda/pkgs/free/
Password:
➜  anaconda sudo conda config --set show_channel_urls yes

➜  anaconda sudo conda create -n tensorflow
Fetching package metadata .....
Solving package specifications:
Package plan for installation in environment /Users/xiaobai/anaconda/envs/tensorflow:

Proceed ([y]/n)? y

#
# To activate this environment, use:
# > source activate tensorflow
#
# To deactivate this environment, use:
# > source deactivate tensorflow
#
➜  ~ cd anaconda

我们最好是直接去操作 “.condarc”的文件,将地址改为国内的地址。然后sudo conda config –set show_channel_urls yes。

3、然后进入我们创建的tensorflow的环境,来安装tensorflow。

➜  anaconda source activate tensorflow
(tensorflow) ➜  anaconda sudo pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py3-none-any.whl
The directory '/Users/xiaobai/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/xiaobai/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting tensorflow==1.1.0 from https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py3-none-any.whl
  Downloading https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py3-none-any.whl (30.9MB)
    100% |████████████████████████████████| 30.9MB 21kB/s
Collecting six>=1.10.0 (from tensorflow==1.1.0)
  Downloading six-1.10.0-py2.py3-none-any.whl
Collecting wheel>=0.26 (from tensorflow==1.1.0)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 156kB/s
Collecting werkzeug>=0.11.10 (from tensorflow==1.1.0)
  Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
    100% |████████████████████████████████| 317kB 140kB/s
Collecting numpy>=1.11.0 (from tensorflow==1.1.0)
  Downloading numpy-1.12.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.4MB)
    100% |████████████████████████████████| 4.4MB 49kB/s
Collecting protobuf>=3.2.0 (from tensorflow==1.1.0)
  Downloading protobuf-3.3.0.tar.gz (271kB)
    100% |████████████████████████████████| 276kB 97kB/s
Collecting setuptools (from protobuf>=3.2.0->tensorflow==1.1.0)
  Downloading setuptools-35.0.2-py2.py3-none-any.whl (390kB)
    100% |████████████████████████████████| 399kB 121kB/s
Collecting appdirs>=1.4.0 (from setuptools->protobuf>=3.2.0->tensorflow==1.1.0)
  Downloading appdirs-1.4.3-py2.py3-none-any.whl
Collecting packaging>=16.8 (from setuptools->protobuf>=3.2.0->tensorflow==1.1.0)
  Downloading packaging-16.8-py2.py3-none-any.whl
Collecting pyparsing (from packaging>=16.8->setuptools->protobuf>=3.2.0->tensorflow==1.1.0)
  Downloading pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 56kB/s
Installing collected packages: six, wheel, werkzeug, numpy, appdirs, pyparsing, packaging, setuptools, protobuf, tensorflow
  Running setup.py install for protobuf ... done
Successfully installed appdirs-1.4.3 numpy-1.12.1 packaging-16.8 protobuf-3.3.0 pyparsing-2.2.0 setuptools-35.0.2 six-1.10.0 tensorflow-1.1.0 werkzeug-0.12.2 wheel-0.29.0
➜  anaconda

4、测试成功

不过一些加快cpu指令不能执行,因为我们使用的pip的安装方式,为了填这个坑,不得不卸载,采用源安装。。。。坑啊!白激动了。

  anaconda python
Python 3.6.0 |Anaconda custom (x86_64)| (default, Dec 23 2016, 13:19:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-05-20 23:19:36.415562: 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-05-20 23:19:36.415585: 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-05-20 23:19:36.415591: 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-05-20 23:19:36.415596: 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-05-20 23:19:36.415600: 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)) b'Hello, TensorFlow!'

二、指令集问题:

The TensorFlow library wasn’t compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations。

速度慢总是让人不爽,官方的建议是源码安装,这样不会有指令集的问题。

具体请参考:我的文章

TensorFlow与SSE4.1指令集问题:

    原文作者:引线小白
    原文地址: https://zhuanlan.zhihu.com/p/27026412
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞