问题提出:
在使用学习深度学习的过程中,经常会碰到v1版本代码的书,而新版本python适配的TensorFlow又是v2版本,直接导入tensorflow(如下面代码)
import tensorflow as tf
g1=tf.Graph()
with g1.as_default():
a=tf.get_variable(‘a’,[2],initializer=tf.ones_initializer())
b = tf.get_variable(‘b’, [2], initializer=tf.zeros_initializer())
with tf.Session(graph=g1) as sess:
tf.global_variables_initializer().run()
with tf.variable_scope("",reuse=True):
print(sess.run(tf.get_variable(‘a’)))
print(sess.run(tf.get_variable(‘b’)))
会出现:AttributeError: module ‘tensorflow’ has no attribute 'get_variable’的报错。
解决方案1:
卸载v2版本TensorFlow,重新下载v1版本TensorFlow,同时适配各类软件及软件包,较为麻烦。由于v2都能配置好,卸掉重新配置v1版本也不是什么问题。因此在这里不予讨论。
解决方案2:
禁止v2版本的代码,使用v1版本的代码。只需要把第一行修改成
即把import tensorflow as tf改为:
import tensorflow.compat.v1 as