簡體   English   中英

Tensorflow:feed dict錯誤:您必須為占位符張量提供值

[英]Tensorflow: feed dict error: You must feed a value for placeholder tensor

我有一個我無法找到原因的錯誤。 這是代碼:

with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)

        images = tf.placeholder(tf.float32, shape = [FLAGS.batch_size,33,33,1])
        labels = tf.placeholder(tf.float32, shape = [FLAGS.batch_size,21,21,1])

        logits = inference(images)
        losses = loss(logits, labels)
        train_op = train(losses, global_step)
        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 xrange(FLAGS.max_steps):
            start_time = time.time()

            data_batch, label_batch = SRCNN_inputs.next_batch(np_data, np_label,
                                                              FLAGS.batch_size)


            _, loss_value = sess.run([train_op, losses], feed_dict={images: data_batch, labels: label_batch})

            duration = time.time() - start_time

def next_batch(np_data, np_label, batchsize, 
               training_number = NUM_EXAMPLES_PER_EPOCH_TRAIN):

    perm = np.arange(training_number)
    np.random.shuffle(perm)
    data = np_data[perm]
    label = np_label[perm]
    data_batch = data[0:batchsize,:]
    label_batch = label[0:batchsize,:]


return data_batch, label_batch

其中np_data是從hdf5文件讀取的整個訓練樣本,與np_label相同。

運行代碼后,我得到了這樣的錯誤:

2016-07-07 11:16:36.900831: step 0, loss = 55.22 (218.9 examples/sec; 0.585 sec/batch)
Traceback (most recent call last):

  File "<ipython-input-1-19672e1f8f12>", line 1, in <module>
    runfile('/home/kang/Documents/work_code_PC1/tf_SRCNN/SRCNN_train.py', wdir='/home/kang/Documents/work_code_PC1/tf_SRCNN')

  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "/home/kang/Documents/work_code_PC1/tf_SRCNN/SRCNN_train.py", line 155, in <module>
    train_test()

  File "/home/kang/Documents/work_code_PC1/tf_SRCNN/SRCNN_train.py", line 146, in train_test
    summary_str = sess.run(summary_op)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 372, in run
    run_metadata_ptr)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 636, in _run
    feed_dict_string, options, run_metadata)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 708, in _do_run
    target_list, options, run_metadata)

  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 728, in _do_call
    raise type(e)(node_def, op, message)

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [128,33,33,1]
     [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[128,33,33,1], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
     [[Node: truediv/_74 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_56_truediv", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder', defined at:

因此,它顯示對於步驟0,它具有結果,這意味着數據已被輸入到占位符中。

但是為什么下次將數據輸入占位符時會出現錯誤呢?

當我嘗試注釋代碼summary_op = tf.merge_all_summaries() ,代碼工作正常。 為什么會這樣?

當我嘗試注釋代碼summary_op = tf.merge_all_summaries()時,代碼工作正常。 為什么會這樣?

summary_op是一個操作。 如果存在(在您的情況下也是如此)與另一個操作的結果相關的摘要操作(取決於占位符的值),則必須向圖形提供所需的值。

因此,您的行summary_str = sess.run(summary_op)需要要存儲的值的字典。

通常,不是重新執行操作來記錄值,而是運行操作 summary_op一次。

做點什么

if step % LOGGING_TIME_STEP == 0:
    _, loss_value, summary_str = sess.run([train_op, losses, summary_op], feed_dict={images: data_batch, labels: label_batch})
else:
    _, loss_value = sess.run([train_op, losses], feed_dict={images: data_batch, labels: label_batch})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM