Prerequisite
- Mac 系统
- 已安装有Anaconda
TensorFlow 环境安装
- Command + Space 调出搜索框,在搜索框中输入“Teminal” 调出终端
- 在终端中输入如下命令行对 tensorflow 环境进行安装
conda create -n tensorflow python=3.7
- 安装成功后出现如下显示
# To activate this environment, use
#
# $ conda activate tensorflow
#
# To deactivate an active environment, use
#
# $ conda deactivate
- 打开 Anaconda 查看环境配置中是否有 TensorFlow 选项
- 选中 Tensorflow 环境后,点击左侧Home选项,回到 Home 页安装 Spyder
- 在 Spyder 的 Console 框输入如下命令行
import tensorflow as tf
若在 import tensorflow 时报如下错误
No module named "tensorflow"
- 打开终端(terminal)
- 查看 tensorflow 环境下的 conda list
(tensorflow) GNdeMacBook-Pro:~ gn$ conda list
- 查找列表中是否有 TensorFlow 包,若无则需安装
- 输入如下命令行
conda install tensorflow
以上就结束了 TensorFlow 的环境配置以及 Tensorflow 环境下 Tensorflow 包的安装。重新打开Anaconda 中的 Spyder 进行 import tensorflow as tf
就会发现能够成功的执行该命令。 到此问题解决 0.0
补充
新的问题来了,我们在 import TensorFlow as tf
成功后,请尝试一下以下操作:
# 载入tensorflow
import tensorflow as tf
# 任意定义一个常量
stringA = tf.constant("My name is JOY", dtype=tf.string)
# 注意:不能直接print(stringA), 如果直接print我们会得到以下结果: Tensor("Const_1:0", shape=(), dtype=string)
# 我们需要在session中运行出我们想要的结果
sess = tf.Session()
就在此时,有了一个如下报错:
RuntimeError: The Session graph is empty. Add operations to the graph before calling run().
报错原因:我们用的是TensorFlow 2.0, 这个问题的出现是由于2.0版本和1.0版本的不同所导致的。解决方法有如下:
import v1 版本
将import tensorflow as tf
替换掉,替换成如下命令:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
补充材料参考链接:
https://stackoverflow.com/questions/37383812/tensorflow-module-object-has-no-attribute-placeholder