通过pip安装
1. 安装前准备
首先确保mac上已经安装了pip和python的2.7/3.3+版本,默认mac系统已经安装,如果没有安装,请进行安装。
2. 开始安装tensorflow
利用一个简单的命令,分别对应不同的python版本
$ pip install tensorflow # Python 2.7; CPU support
$ pip3 install tensorflow # Python 3.n; CPU support
如果上面的安装失败了,可以利用下面的命令安装。
EXPORT tfBinaryURL2.7=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py2-none-any.whl
EXPORT tfBinaryURL3.x=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py3-none-any.whl
$ sudo pip install --upgrade tfBinaryURL2.7 # Python 2.7
$ sudo pip3 install --upgrade tfBinaryURL3.x # Python 3.n
注意:
这个过程可能会出现下面的错误:
Installing collected packages: setuptools, protobuf, numpy, markdown, tensorflow-tensorboard, funcsigs, pbr, mock, tensorflow
Found existing installation: setuptools 18.5
Uninstalling setuptools-18.5:
这个时候可以利用--ignore-installed
选项把已经安装packages忽略掉,不用卸载重新安装。
3. 简单的测试
如果安装成功了tensorflow,可以通过简单的测试,是否正常工作。
编辑简单的python文件如下:
#!/bin/python
import tensorflow as tf
test = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(test))
运行脚本,输出则安装正确。
注意:
脚本运行后,可能会出现警告如下:
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.
可以通过设置TF_CPP_MIN_LOG_LEVEL
环境变量来解决,
TF_CPP_MIN_LOG_LEVEL
环境变量是tensorflow log不同等级的变量,1代表忽略INFO信息,2代表忽略WARNING信息,3代表忽略ERROR信息。
import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’]=’2’
或者
export TF_CPP_MIN_LOG_LEVEL=2.
4. 卸载tesnsorflow
利用uninstall命令即可
$ pip uninstall tensorflow # Python 2.7
$ pip3 uninstall tensorflow # Python 3.n