【问题标题】:feed_dict in TensorFlow throwing unexpected error (summary operations)TensorFlow 中的 feed_dict 抛出意外错误(汇总操作)
【发布时间】:2016-12-09 21:22:55
【问题描述】:

我的训练脚本,用于训练 TensorFlow 模型,根据在线教程稍作修改:

def train(data_set_dir, train_set_dir):
    data = data_input.read_data_sets(data_set_dir, train_set_dir)

    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)
        # defines placeholders (type=tf.float32)
        images_placeholder, labels_placeholder = placeholder_inputs(batch_size, image_size, channels)

        logits = model.inference(images_placeholder, num_classes)
        loss = loss(logits, labels_placeholder, num_classes)
        train_op = training(loss, global_step, batch_size)

        saver = tf.train.Saver(tf.all_variables()) 
        summary_op = tf.merge_all_summaries()  
        init = tf.initialize_all_variables()
        sess = tf.Session()
        sess.run(init)
        summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)

        for step in range(max_steps):
            start_time = time.time()
            feed_dict = fill_feed_dict(data, images_placeholder, labels_placeholder, batch_size)
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
            # ... continue to print loss_value, run summaries and save checkpoints

上面调用的placeholder_inputs函数是:

def placeholder_inputs(batch_size, img_size, channels):
    images_pl = tf.placeholder(tf.float32,
                                        shape=(batch_size, img_size, img_size, channels), name='images')
    labels_pl = tf.placeholder(tf.float32,
                                        shape=(batch_size, img_size, img_size), name='labels')
    return images_pl, labels_pl

为了澄清,我正在处理的数据是针对分割问题中的每个像素分类的。如上所示,这是一个二分类问题。

而feed_dict函数是:

def fill_feed_dict(data_set, images_pl, labels_pl, batch_size):
    images_feed, labels_feed = data_set.next_batch(batch_size)
    feed_dict = {images_pl: images_feed, labels_pl: labels_feed}
    return feed_dict

我被困在哪里:

tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'labels' with dtype float and shape [1,750,750]
 [[Node: labels = Placeholder[dtype=DT_FLOAT, shape=[1,750,750], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

回溯显示它是由我的placeholder_inputs 函数中的“标签”张量引起的。此外,据我所知,这个错误在两个占位符之间不断变化 - 随机。一次是“标签”[labels_pl] 张量,另一次是我的“图像”[images_pl] 张量。

详细错误:

File ".../script.py", line 32, in placeholder_inputs
  shape=(batch_size, img_size, img_size), name='labels')
File ".../tensorflow/python/ops/array_ops.py", line 895, in placeholder
  name=name)
File ".../tensorflow/python/ops/gen_array_ops.py", line 1238, in _placeholder 
  name=name)
File ".../tensorflow/python/ops/op_def_library.py", line 704, in apply_op
  op_def=op_def)
File ".../tensorflow/python/framework/ops.py", line 2260, in create_op
  original_op=self._default_original_op, op_def=op_def)
File "/tensorflow/python/framework/ops.py", line 1230, in __init__
  self._traceback = _extract_stack()

我尝试过/检查过的内容:

  1. 也将 feed_dict 置于 for 循环之外,但无济于事。

  2. 已验证训练数据目录中有足够的数据对应batch_size的要求。

  3. 指定占位符的 dtype 有多种变体 - 假设 'float' 是堆栈跟踪中的关键。

  4. 交叉检查的数据形状。它们与占位符中指定的完全相同。

也许这是一个比我想象的要简单得多的问题。甚至可能是我在这里看不到的一个小错字。建议?我相信我已经用尽了所有选择。正在寻找能对这个问题有新见解的人。

我参考了this 的错误描述。

更新:

session.run 之前做了一个print feed_dict(正如这里的评论中所建议的那样)并注意到预期值被输入到占位符中:

{<tf.Tensor 'images:0' shape=(1, 750, 750, 3) dtype=float32>:
array([[[[-0.1556225 , -0.13209309, -0.15954407],
     [-0.15954407, -0.12032838, -0.13601466],
     .....
     [-0.03405387,  0.04829907,  0.09535789]]]], dtype=float32),
 <tf.Tensor 'labels:0' shape=(1, 750, 750) dtype=float32>: 
 array([[[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       .....
       [ 0.,  0.,  0., ...,  0.,  0.,  0.]]], dtype=float32)}

还有一些我之前没有提到的: 循环第一次运行。因此,我得到了step = 0 的第一个值的输出,然后在打印了我为step=0 指定的loss_value 语句后立即退出。

更新 2:

我想出了问题所在。打印summary_op。但为什么会这样是我无法理解的。这就是我在 for 循环中打印它的方式:

if step % 100 == 0:
    summary_str = sess.run(summary_op)
    summary_writer.add_summary(summary_str, step)

注释掉这个块就可以了。关于为什么会出错的想法?​​

更新 3:已解决

在下面回答。但我注意到,TensorFlow CIFAR-10 example 做了类似的sess.run,但没有明确提及feed_dict,而且运行良好。那么它究竟是如何工作的呢?

【问题讨论】:

  • Numpy 数组默认为np.float64,但DT_FLOAT 等效于np.float32,所以可以添加.as_type(np.float32)
  • @YaroslavBulatov 是的,也试过了。假设您的意思是,astype(np.float32) 在占位符的输入数组上。
  • 可能会在每次 session.run 调用之前添加一个 print 以确定输入占位符的实际形状和 dtype 是什么
  • @YaroslavBulatov 我怎样才能在session run 之前打印输入的数组?之后我才打印出来。
  • @YaroslavBulatov 我做了一个print feed_dict 作为一个快速而肮脏的解决方案。它们似乎具有预期的 dtype 和形状。在我的问题中更新。

标签: python tensorflow


【解决方案1】:

明显错误。 我没有为在summary_op 上运行的会话指定feed_dict

if step % 100 == 0:
    summary_str = sess.run(summary_op, feed_dict=feed_dict)
    summary_writer.add_summary(summary_str, step)

在会话运行中明确提及feed_dict 调用就可以了。但为什么? TensorFlow CIFAR-10 示例执行了类似的sess.run,但没有明确提及feed_dict,并且运行良好。

【讨论】:

  • 哦,对了,我猜错误消息确实表明您获取的输出取决于未提供的占位符。它可以在其他情况下工作的可能原因是它使用placeholder_with_default,或者摘要不依赖于占位符的值
  • 这澄清了事情。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多