整数或布尔值的 TensorFlow 占位符变量不起作用



我在TensorFlow中使用以下代码片段有条件地从一个或另一个源提取数据:

if __name__ == '__main__':

with tf.device("/gpu:0"):
    with tf.Graph().as_default():
        with tf.variable_scope("cifar_conv_model"):
            is_train = tf.placeholder(tf.int32) # placeholder for whether to pull from train or val data
            keep_prob = tf.placeholder(tf.float32) # dropout probability
            x, y = tf.cond(tf.equal(is_train, tf.constant(1, dtype=tf.int32)), distorted_inputs, inputs)
            output = inference(x, keep_prob)
            cost = loss(output, y)
            global_step = tf.Variable(0, name='global_step', trainable=False)
            train_op = training(cost, global_step)
            eval_op = evaluate(output, y)
            summary_op = tf.merge_all_summaries()
            saver = tf.train.Saver()
            summary_writer = tf.train.SummaryWriter("conv_cifar_logs/", graph_def=sess.graph_def)
            init_op = tf.initialize_all_variables()
            sess = tf.Session()
            sess.run(init_op)
            tf.train.start_queue_runners(sess=sess)
            # Training cycle
            for epoch in range(training_epochs):
                avg_cost = 0.
                total_batch = int(cifar10_input.NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN/batch_size)
                # Loop over all batches
                for i in range(total_batch):
                    # Fit training using batch data
                    _, new_cost = sess.run([train_op, cost], feed_dict={is_train: 1, keep_prob: 0.5})
                    # Compute average loss
                    avg_cost += new_cost/total_batch
                    print "Epoch %d, minibatch %d of %d. Average cost = %0.4f." %(epoch, i, total_batch, avg_cost)
随着线程的爆发,

我不断收到错误呕吐,但反复出现的主题是以下错误:

InvalidArgumentError: You must feed a value for placeholder tensor 'cifar_conv_model/Placeholder' with dtype int32
     [[Node: cifar_conv_model/Placeholder = Placeholder[dtype=DT_INT32, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
     [[Node: cifar_conv_model/Placeholder/_151 = _HostRecv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_407_cifar_conv_model/Placeholder", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Caused by op u'cifar_conv_model/Placeholder', defined at:
  File "convnet_cifar.py", line 134, in <module>
    is_train = tf.placeholder(tf.int32) # placeholder for whether to pull from train or val data
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 743, in placeholder
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 607, in _placeholder
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2040, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1087, in __init__
    self._traceback = _extract_stack()

任何帮助将不胜感激!

这里提到了一个解决方案:http://andyljones.tumblr.com/。

您必须更改 tf 的占位符is_train = tf.placeholder(tf.int32)。变量:is_train = tf.Variable(True, name='training')。因此,您将能够在 sess.run(tf.initialize_all_variables()) 上进行调用进行初始化,然后再进行调用tf.train.start_queue_runners(sess=sess)

最新更新