Mac上Anaconda+Tensorflow安装

终于准备开始学习谷歌的深度学习框架tensorflow了,花了一上午的时间,终于配置好了,下面就是详细的步骤:

首先大家可以参考一下官方的安装tensorflow的方法:
https://www.tensorflow.org/install/install_mac

官方提供了
1、virtualenv
2、”native” pip
3、Docker
4、installing from sources, which is for experts and is documented in a separate guide.(也就是Installing with Anaconda)
四种方法,我们采用最后一种方法,因为参考《TensorFlow实战》中是采用这种方法(书里面说Anaconda是python的一个科学计算发行版,里面集成了很多的依赖库,所以提供了一个很好的编译环境)。

安装Anaconda

所以,首先我们就需要下载Anaconda,官方网站为:
https://www.continuum.io/downloads
mac系统,python2.7版本对应的下载链接有以下两种:
这里写图片描述
上面一种是图形界面的Anaconda,下面一种是命令行形式的Anaconda,我选择的是上面一种(作为程序员中的菜鸟还是觉得图形界面更加舒服点)。

下载完成以后,会在根目录下面多一个anaconda的文件夹(安装过程中没有指定路径的前提下,不过最好就安装在这里):
这里写图片描述

安装好Anaconda之后,可以进入终端利用Anaconda的包管理工具来进行一些简单的操作:
查询安装信息
$ conda info

查询当前已经安装的库
$ conda list

安装库(*代表库名称)
$ conda install ***

更新库
$ conda update ***

还有一个问题就是Anaconda仓库镜像,很多地方说官方下载很慢,需要换成清华的镜像,但是奇怪的是我换了之后还下载不了了,换成官方的之后倒是又能够下了,此处还是摆出来给有需要的同学吧:

$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
$ conda config --set show_channel_urls yes
$ conda install numpy   #测试是否添加成功

之后会自动在用户根目录生成“.condarc”文件,可以在终端用
$ ls -a
命令查看该文件,如果要删除镜像,直接删除“.condarc”文件即可:
$ rm .condarc

至此,安装Anaconda的任务就完成了,有需要了解conda的使用方法的,也可以查看下面这篇博文:http://www.cnblogs.com/harvey888/p/5465452.html

在Anaconda环境中安装TensorFlow

此处采用pip方式来进行安装:
pip方式需要首先激活conda环境:

$ source activate tensorflow  

然后根据要安装的不同tensorflow版本选择对应的一条环境变量设置export语句(操作系统,Python版本,CPU版本还是CPU+GPU版本)

# Ubuntu/Linux 64-bit, CPU only, Python 2.7  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.2.1-cp27-none-linux_x86_64.whl  

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7  
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.2.1-cp27-none-linux_x86_64.whl  

# Mac OS X, CPU only, Python 2.7:  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py2-none-any.whl  

# Mac OS X, GPU enabled, Python 2.7:  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-1.2.1-py2-none-any.whl  

# Ubuntu/Linux 64-bit, CPU only, Python 3.4  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.2.1-cp34-cp34m-linux_x86_64.whl  

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4  
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.2.1-cp34-cp34m-linux_x86_64.whl  

# Ubuntu/Linux 64-bit, CPU only, Python 3.5  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.2.1-cp35-cp35m-linux_x86_64.whl  

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5  
# Requires CUDA toolkit 7.5 and CuDNN v5. For other versions, see "Install from sources" below.  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.2.1-cp35-cp35m-linux_x86_64.whl  

# Mac OS X, CPU only, Python 3.4 or 3.5:  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.2.1-py3-none-any.whl  

# Mac OS X, GPU enabled, Python 3.4 or 3.5:  
(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-1.2.1-py3-none-any.whl  

看到上面每个版本中的tensorflow后面都有一个1.2.1的版本号,我们如果需要最新的版本,可以到https://github.com/tensorflow/tensorflow/中去查看,进入网站之后,根据具体情况点击链接查看:
这里写图片描述
比如我要安装的是Mac CPU-only中的python2,就可以点击这个链接来查看:
这里写图片描述
可以看到后面的最新地址是1.2.1,就可以将上面的地址改成这个最新的版本号,再用下面的命令进行安装:

(tensorflow)$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-1.2.1-cp27-none-linux_x86_64.whl 

# Python 2 的选择下面的命令来进行安装  
(tensorflow)$ pip install --ignore-installed --upgrade $TF_BINARY_URL  

# Python 3 的选择下面的命令来进行安装 
(tensorflow)$ pip3 install --ignore-installed --upgrade $TF_BINARY_URL  

(该命令行的用户名前面的(tensorflow)是因为前面激活了tensorflow而出现的)。

然后就可以测试安装了:

$ source activate tensorflow  
(tensorflow)$  # Your prompt should change. 

# Run Python programs that use TensorFlow.  

$ python
Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
# 以上程序没有报错的话就是安装成功了

# When you are done using TensorFlow, deactivate the environment.  
(tensorflow)$ source deactivate  

本步骤参考博文:http://blog.csdn.net/nxcxl88/article/details/52704877

猜你喜欢

转载自blog.csdn.net/sinat_28731575/article/details/74616230