使用 TensorFlow 和 Jupyter 实例化 InteractiveSession 时出错

Error instantiating the InteractiveSession using TensorFlow and Jupyter

最近我在我的 Jupyter / IPython 笔记本中使用 TensorFlow 中的 InteractiveSession 时遇到错误。这个问题很容易重新创建,我的整个代码是:

import tensorflow as tf

sess = tf.InteractiveSession()

sess.graph

输出为:

Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.InteractiveSession object at 0x11a4e89b0>>
Traceback (most recent call last):
  File "/Users/.../anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 140, in __del__
    self.close()
  File "/Users/.../anaconda/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 905, in close
    self._default_session.__exit__(None, None, None)
  File "/Users/brad/anaconda/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/Users/.../anaconda/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3215, in get_controller
    assert self.stack[-1] is default
AssertionError: 
Out[3]:
<tensorflow.python.framework.ops.Graph at 0x10685dc50>

所以图形调用有效,但我无法解释或消除此错误。

此错误消息是由 TensorFlow 0.8 及更早版本中的错误引起的。当在 Python 垃圾收集器删除旧的 tf.InteractiveSession 之前创建新的 tf.InteractiveSession 时,会触发该错误。在 TensorFlow 0.9 版本中是 fixed,因此升级到最新版本应该可以解决问题。