https://docs.qq.com/sheet/DZkR6cUZpdFJ2bUxS?tab=BB08J2

①AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’

例子:
X = tf.placeholder(tf.float32, shape=(None, n_H0, n_W0, n_C0))
Y = tf.placeholder(tf.float32,shape=(None,n_y))
解决办法一:
X = tf.compat.v1.placeholder(tf.float32, [None, n_H0, n_W0, n_C0])
Y = tf.compat.v1.placeholder(tf.float32, [None, n_y])

解决办法二:
不要使用import tensorflow as tf
替换为:import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

在jupyter上实验,出现警告:
WARNING:tensorflow:From D:\ProgramData\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\compat\v2_compat.py:65: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
且这种方法无法解决问题。。。。

②RuntimeError: tf.placeholder() is not compatible with eager execution.

解决办法:
查到了一个解决方案,放上链接:
https://stackoverflow.com/questions/53429896/how-do-i-disable-tensorflows-eager-execution
在import tensorflow as tf 后加上
tf.compat.v1.disable_eager_execution()
关闭紧急执行。
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
这也可以保证session.run()正常运行

③AttributeError: module ‘tensorflow’ has no attribute ‘reset_default_graph’

使用tf.reset_default_graph()的时候报错,
因为安装的Tensorflow 2不支持Tensorflow1的API而报的错,需要将导入模块的部分进行更改
解决办法一:
将import tensorflow as tf
替换为:import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
但是本人采用这种会警告,且上面试过不适用,因此采取了第二种比较笨的
解决办法二(推荐):
不使用这个命令,而替换使用如下:
from tensorflow.python.framework import ops #这句可不写
ops.reset_default_graph()

两个命令的作用都是:用于清除默认图形堆栈并重置全局默认图形

④AttributeError: module ‘tensorflow’ has no attribute ‘Session’

with tf.Session() as sess_test:
修改为:with tf.compat.v1.Session() as sess_test:

⑤AttributeError: module ‘tensorflow’ has no attribute ‘set_random_seed’

使用tf.set_random_seed(1) 时报错,
解决方法:module ‘tensorflow’ has no attribute ‘set_random_seed’,将tf.set_random_seed(1) 改为 tf.random.set_seed(1)

⑥AttributeError: module ‘tensorflow’ has no attribute ‘get_variable’

使用W1 =tf.get_variable(‘W1’,[4,4,3,8],initializer=tf.contrib.layers.xavier_initializer(seed = 0))的时候报错
解决办法:在tf与get_variable…之间加上compat.v1.

⑦AttributeError: module ‘tensorflow’ has no attribute ‘contrib’

使用W1 =tf.compat.v1.get_variable(‘W1’,[4,4,3,8],initializer=tf.contrib.layers.xavier_initializer(seed = 0))时报错
解决办法:
由于TF2.x删除了contrib,使用initializer = tf.initializers.GlorotUniform(seed=0)来代替initializer=tf.contrib.layers.xavier_initializer(seed=0)
但是这边不知道是否是因为使用了不同的初始化器的原因,导致算出的结果不一样。

⑧AttributeError: module ‘tensorflow’ has no attribute ‘global_variables_initializer’

使用init = tf.global_variables_initializer()命令时,出现错误
解决办法:
在tf与global之间加上compat.v1.