Mac下通过Anaconda安装Tensorflow
Anaconda
是一个基于python
的科学计算平台,支持 Linux, Mac, Windows
系统,提供了包管理与环境管理的功能,可以很方便地解决多版本python
并存、切换以及各种第三方包安装问题。这个平台里包含有python
,scala
,numpy
等绝大部分主流的用于科学计算的包。
1.安装Anaconda
从官网下载(Mac版)最新版本的安装包
有两种安装方式:
- 通过图形化界面安装,下载的文件格式为
.pkg
Graphical Installer.png - 通过命令行安装,下载的文件格式为
.sh
Command-Lint Installer.png
2.建立一个Tensorflow的运行环境
// 创建环境 $ conda create -n tensorflow python=3.5 // 移除环境 conda remove --name tensorflow --all
目前
Mac
上的Tensorflow
仅仅支持CPU
版本,而且3.0以上版本仅支持3.5版本,所以创建环境的时候一定要加上Python=3.5
。详情可以去Github Tensorflow查看。
设置国内镜像
如果需要安装很多packages
,你会发现conda
下载的速度经常很慢,因为Anaconda.org
的服务器在国外。所幸的是,清华TUNA
镜像源有Anaconda
仓库的镜像,我们将其加入conda
的配置即可:
# 添加Anaconda的TUNA镜像 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ # TUNA的help中镜像地址加有引号,需要去掉 # 设置搜索时显示通道地址 conda config --set show_channel_urls yes
安装Tensorflow
目前仅仅是创建了一个空的环境,仅有与Python3.5
相关的一些包,我们需要先激活环境,然后安装Tensorflow
。
// 激活环境 $ source activate tensorflow // 安装Tensorflow $ pip install tensorflow // 关闭环境 $ source deactivate
3.简单测试是否安装成功
>>> import tensorflow as tf >>> hello = tf.constant('Hello, TensorFlow!') >>> sess = tf.Session() >>> print(sess.run(hello)) Hello,TensorFlow!
4.使用Jupyter notebook
永久更改 Jupyter notebook 默认目录路径
-
创建
Jupyter notebook
的配置文件jupyter_notebook_config.py
,在终端中输入:$ jupyter notebook --generate-config
-
打开
jupyter_notebook_config.py
,找到如下文字:// 打开jupyter_notebook_config.py $ cd .jupyter $ vim jupyter_notebook_config.py // 找到如下文字 ## The default URL to redirect to from `/` #c.NotebookApp.default_url = '/tree'
-
将其修改为:
## The default URL to redirect to from `/` c.NotebookApp.default_url = '/tree/DeepLearning' // 此时,Jupyter notebook的默认目录路径就变成了/home/DeepLearning
该方法只能通过根目录启动
Jupyter notebook
,从其他目录启动会出现Jupyter notebook
网页无法显示的状态。
单次更改 Jupyter notebook 默认目录路径
// 在终端中cd到目标目录: $ cd ~/DeepLearning $ jupyter notebook
5.安装OpenCV
pip install opencv-python
相关文章
作者:一蓑烟羽
链接:https://www.jianshu.com/p/d54546ab315e
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。