在 Mac 上安装 TensorFlow CPU 版本
Python 版本确认以及pip的安装
因为我之前在Mac上面安装过anaconda 3,所以我电脑里是有Python3.8的
Python 3以上的版本是默认有pip的,因此还在使用Python 2.x版本的朋友们得要自行安装pip
命令如下
sudo easy_install pip
安装 TensorFlow CPU 版本
前提: pip的源默认是谷歌的,速度非常的慢,我刚开始安装的时候尝试过五六遍但是都没成功,后来在网上找到了一篇文说换成国内的源
这里我选择的是清华的源,先别忙着复制下面的代码,先往下看
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple/
后面 -i https://pypi.tuna.tsinghua.edu.cn/simple/ 表示的是访问清华的源
眼看就要到结尾了,结果来了一个报错,说是权限不够,类似下面的这个代码
.................
Consider using the --user option or check the permissions.
因此我在第二次安装的时候在结尾加上了 --user
这个代码各位可以复制
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple/ --user
之后就安装成功了
TensorFlow CPU 2.x 的简单测试
import tensorflow as tf
with tf.compat.v1.Session() as sess:
a = tf.constant(3.0)
b = tf.constant(4.0)
c = a * b
print('Hello TensorFlow!',sess.run(c))
sess.close()
如果你看到了如下输出,恭喜,你的TensorFlow安装成功了!
Hello TensorFlow! 12.0
截图纪念一下哈哈哈