tf.global_variables_initializer()及tf.Session()在tensorflow version 2.10中AttributeError解决办法

问题来源:吴恩达《深度学习》L2W3作业

tf.global_variables_initializer()及tf.Session()在tensorflow version 2.10中AttributeError解决办法。

问题发现

吴恩达深度学习L2W3作业参考答案中的代码在tensorflow1.8版本中没有出现运行错误,但tensorflow2.10版运行代码时却出现错误,如下。

y_hat = tf.constant(36,name="y_hat")
y = tf.constant(39,name ="y")
loss = tf.Variable((y-y_hat)**2,name ="loss")
init =tf.global_variables_initializer()
with tf.Session() as session: 
    session.run(init)
    print(session.run(loss))

错误提示:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
AttributeError                            Traceback (most recent call
last)  in ()
     2 y = tf.constant(39,name ="y")
     3 loss = tf.Variable((y-y_hat)**2,name ="loss")
     4 init =tf.global_variables_initializer()
     5with tf.Session() as session:

 AttributeError: module 'tensorflow' has no attribute 'global_variables_initializer'

查了相关帖子后,根据提示将对应位置的tf.Session() as session、tf.global_variables_initializer()分别改成了tf.compat.v1.Session() as session、tf.compat.v1.global_variables_initializer(),运行后仍出现错误提示。如下:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
 in ()
      4 init =tf.compat.v1.global_variables_initializer()
      5 with tf.compat.v1.Session() as session:
----> 6     session.run(init)
      7     print(session.run(loss))

~\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    958     try:
    959       result = self._run(None, fetches, feed_dict, options_ptr,
--> 960                          run_metadata_ptr)
    961       if run_metadata:
    962         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\AppData\Roaming\Python\Python36\site-packages\tensorflow_core\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1106       raise RuntimeError('Attempted to use a closed Session.')
   1107     if self.graph.version == 0:
-> 1108       raise RuntimeError('The Session graph is empty.  Add operations to the '
   1109                          'graph before calling run().')
   1110 

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

原因分析:

我一开始用的tensorflow版本是1.X的,后来更新到了version2.10。显然报错是因为tensorflow版本问题引起的。若想查看自己tensorflow 版本,输入 :

help(tf.version)
##运行后输出以下版本信息,可以看到是version = 2.10
Help on package tensorflow_core._api.v2.version in tensorflow_core._api.v2:
NAME
    tensorflow_core._api.v2.version - Public API for tf.version namespace.
PACKAGE CONTENTS
DATA
    COMPILER_VERSION = 'MSVC 192428314'
    GIT_VERSION = 'v2.1.0-rc2-17-ge5bf8de410'
    GRAPH_DEF_VERSION = 175
    GRAPH_DEF_VERSION_MIN_CONSUMER = 0
    GRAPH_DEF_VERSION_MIN_PRODUCER = 0
    VERSION = '2.1.0'

解决方案

根据错误提示的最后一行RuntimeError:The Session graph is empty. Add operations to the graph before calling run().大概意思就是:Session的计算图是空白的。在调用run()之前将操作添加到图中。
查询部分帖子,并经过我自己的尝试,发现将前边的变量赋值等操作全都放到 with tf.compat.v1.Session() as session下边时就可以彻底解决问题了。如下:

with tf.compat.v1.Session() as session: 
    y_hat = tf.constant(36,name="y_hat")
    y = tf.constant(39,name ="y")
    loss = tf.Variable((y-y_hat)**2,name ="loss")
    init =tf.compat.v1.global_variables_initializer()
    session.run(init)
    print(session.run(loss))

运行结果:9

参考:
[1]: https://blog.csdn.net/qq_35630121/article/details/103349528?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=1328602.60134.16152097258025791&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

你可能感兴趣的:(tensorflow,深度学习)