【问题标题】:Error: TensorFlow: tf.enable_eager_execution must be called at program startup错误:TensorFlow:tf.enable_eager_execution 必须在程序启动时调用
【发布时间】:2018-05-11 04:39:48
【问题描述】:

我正在尝试从github修改代码:

我收到此错误:

tf.enable_eager_execution must be called at program startup.

我认为它来自这些代码行:

from __future__ import print_function
import make_dataset

import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()

另外,我认为我需要急切执行的原因是因为在我的 with tf.session 块中:

使用 tf.Session() 作为 sess:

# Run the initializer
sess.run(iterator.initializer)

for step in range(1, num_steps+1):
    batch_x, batch_y = myDataset.batch(4)#line where error occurs
    # Run optimization op (backprop)
    sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})

我得到错误:

RuntimeError: dataset.__iter__() is only supported when eager execution is enabled

因此,在决定更改迭代器或启用急切执行方面的指导会很棒。

非常感谢, 乔什

【问题讨论】:

标签: python tensorflow tensorflow2.0


【解决方案1】:

Eager Execution 设置只能通过重置运行时来更改。 要重置运行时,请在您的菜单中查找如何重置运行时,在我的情况下,我必须导航到 Runtime->Reset all runtimes 并单击 Yes

这有点违反直觉,因为 iPython 执行会记住您第一次执行代码时选择的设置。这意味着如果您在启用 Eager Execution 的情况下实例化 Tensorflow,则从该单元格中删除代码并再次运行它不会禁用 Eager Execution。同样,如果您在未启用 Eager Execution 的情况下实例化 Tensorflow,则将启用 Eager Execution 的代码添加到导入 Tensorflow 的单元块并重新运行该单元将不会启用 Eager Execution。

解决方案是在运行单元之前重置运行时并更新代码。当你这样做时,Tensorflow 将在启用或禁用 Eager Execution 的适当设置的情况下运行。

【讨论】:

    【解决方案2】:

    您可能希望调试 myDataset 而不是使用急切执行,因为您遵循的示例在图形模式下运行。如果您的 myDatasettf.data.Dataset 对象。它的batch 方法将返回一个tf.data.Dataset,它不能被解包到batch_x, batch_y,即图形模式不支持dataset.__iter__()

    一种选择是遵循the guide 中的教程。您可以从“批处理”数据集中make_one_shot_iteratormake_initializable_iterator(需要通过sess.run(iterator.initializer, ...) 初始化)。然后你可以通过batch_x, batch_y = iterator.get_next()获取循环中的每一批次

    【讨论】:

      【解决方案3】:

      参考这个字符串,也许有帮助。

      https://github.com/googlecolab/colabtools/issues/262

      (这是google colab的,但您可以找到其他相关信息)

      【讨论】:

      • 不要链接到外部网址,请在答案中添加相关代码和解释。
      • 该链接提供了对 OP 可能面临的问题的讨论以及可能的修复。我不需要额外的解释和解释。
      【解决方案4】:

      我遇到了同样的错误 - 我必须转到:内核 -> 从我的 Jupyter 笔记本菜单中重新启动并清除输出。

      没有其他东西对我有用。 (甚至没有关闭 Jupyter 笔记本并重新启动它)

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 2018-11-09
        相关资源
        最近更新 更多