首先安装pip,打开终端,python2.7路径下,输入sudo easy_install pip(python3路径下,输入curl https://bootstrap.pypa.io/get-pip.py|python3
安装成功后,可以通过 pip --version 查看版本
接下来下载并安装 Anaconda,可以选择官网https://www.anaconda.com/download/#macos,也可以选择清华大学的镜像网址https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive,
安装成功后,可以使用conda --version 查看版本 ,确认是否安装成功。
如果提示找不到此命令,则使用vi ~/.bash_profile命令
编辑类似以下内容:(编辑你自己的路径地址)
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Volumes/固态硬盘/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Volumes/固态硬盘/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "Volumes/固态硬盘/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/Volumes/固态硬盘/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
如果是环境变量没有配置好,在终端输入 echo 'export PATH="/Volumes/固态硬盘/opt/ananconda3/bin:$PATH"' >> ~/.bashrc(路径使用你自己的)
然后执行source ~/.bash_profile source ~/.bashrc
如果发现每次打开终端都要执行以上命令才行,则使用
vi ~/.zshrc 编辑source ~/.bash_profile source ~/.bashrc内容并保存文件即可
安装了 Anaconda,接下来安装 TensorFlow CPU 版本或 GPU 版本。基本上计算机都支持 TensorFlow CPU 版本,而 GPU 版本则要求计算机有一个 CUDA compute capability 3.0 及以上的 NVDIA GPU 显卡(对于台式机而言最低配置为 NVDIA GTX 650)。
CPU 与 GPU 的对比:中央处理器(CPU)由对顺序串行处理优化的内核(4~8个)组成。图形处理器(GPU)具有大规模并行架构,由数千个更小且更有效的核芯组成,能够同时处理多个任务。
对于 TensorFlow GPU 版本,需要先安装 CUDA toolkit 7.0 及以上版本、NVDIA【R】驱动程序和 cuDNN v3 或以上版本。cuDNN 文件需安装在不同的目录中,并需要确保目录在系统路径中。
在终端输入conda create -n tensorflow python=3.5
使用该命令创建conda环境。
激活conda环境:
在终端输入:source activate tensorflow
安装tensorFlow时采用清华大学的镜像,(不采用镜像基本慢的要死,或者超时失败)
GPU版本安装方法:
pip install tensorflow-gpu==2.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
CPU版本安装方法:
pip install tensorflow==2.3 -i https://pypi.tuna.tsinghua.edu.cn/simple
接下来试验一下:
在命令行中输入 python,并输入以下代码:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
message =tf.constant('Welcome to the world of DNN!')
with tf.compat.v1.Session() as sess:
print(sess.run(message).decode())
输出为:Welcome to the world of DNN!
有以上输出说明安装成功了。
接下来在命令行中禁用 conda 环境,在终端使用 source deactivate 命令