在交互式 IPython 终端中连续运行的 Tensorflow Op 命名

Tensorflow Op Naming for Consecutive Runs in Interactive IPython terminal

我注意到在 Tensorflow 脚本的后续 运行 中,您的图形操作会获得编号名称,例如:

loss = tf.reduce_mean(tf.nn.l2_loss(y - pred), name="l2_loss")

会得到名字:

l2_loss
l2_loss_1
l2_loss_2
...
l2_loss_N

因为您在同一 IPython 会话中继续进行相同的 运行。这不会那么烦人,除非稍后在脚本中要保存摘要时:

x_sample, y_sample = get_sample(X, Y)
feed = {x: x_batch, y: y_batch}
trainer.run(feed_dict=feed)
summary_str = summary_op.eval(feed_dict=feed)

您将遇到以下故障:

InvalidArgumentError: You must feed a value for placeholder tensor 'x_input' with dtype float ....

有没有办法(在脚本或其他东西的顶部)取消所有这些旧的、过时的 Op 定义并使用当前的 运行 并正确遵守 name=... 创建变量、占位符和常量时的必要条件?

您可能希望将 tf.reset_default_graph() 添加到设置图表的脚本开头。这将从默认图中删除所有现有的张量、变量和操作。这样脚本之前的执行就不会受到干扰。

不过,我不确定您收到的错误消息是否与此有关。您确定您没有忘记为占位符 x_input 提供值吗?您正在为占位符 x 提供数据,应该是 x_input?

对于您的后续问题,由于重新定义了交互式会话,您可能会得到 AssertionError

sess = InteractiveSession()
sess = InteractiveSession()
Exception AssertionError: AssertionError("Nesting violated for default stack of <type 'weakref'> objects") in ...

您可以通过先关闭会话来避免这种情况,sess.close()