我在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()
非常感谢任何帮助!
答案 0 :(得分:2)
这里提到了一个解决方案: http://andyljones.tumblr.com/
您必须更改占位符is_train = tf.placeholder(tf.int32)
以获取tf.Variable:is_train = tf.Variable(True, name='training')
。
因此,您可以在拨打电话之前通过sess.run(tf.initialize_all_variables())进行初始化tf.train.start_queue_runners(sess=sess)
。