Python 整数或布尔值的TensorFlow占位符变量isn';行不通
我使用TensorFlow中的以下代码片段有条件地从一个源或另一个源提取数据:Python 整数或布尔值的TensorFlow占位符变量isn';行不通,python,tensorflow,Python,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
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()
InvalidArgumentError:必须为数据类型为int32的占位符张量“cifar\U conv\U模型/占位符”提供一个值
[[Node:cifar\u conv\u model/Placeholder=Placeholder[dtype=DT\u INT32,shape=[],[u device=“/job:localhost/replica:0/task:0/cpu:0”]()]
[[Node:cifar\u conv\u model/Placeholder/\u 151=\u HostRecv[client\u terminated=false,recv\u device=“/job:localhost/replica:0/task:0/gpu:0”,send\u device=“/job:localhost/replica:0/task:0/cpu:0”,send\u device\u化身=1,tensor\u name=“edge\u 407\u cifar\u conv\u model/Placeholder”,tensor=DT\u INT32,\u device=“/job:localhost/replica:0/task:0/gpu:0”]
由以下位置定义的“cifar\u conv\u模型/占位符”引起:
文件“convnet_cifar.py”,第134行,在
is_train=tf.placeholder(tf.int32)#是否从train或val数据中提取的占位符
占位符中的文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/ops/array_ops.py”,第743行
名称=名称)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/ops/gen_array_ops.py”,第607行,在占位符中
名称=名称)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/ops/op_def_library.py”,第655行,在apply_op
op_def=op_def)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/framework/ops.py”,第2040行,在create_op中
初始值=自身值。\默认值\初始值,初始值=初始值)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/framework/ops.py”,第1087行,在__
self.\u traceback=\u extract\u stack()
任何帮助都将不胜感激 这里提到了一种解决方案: 您必须为tf.变量更改占位符
is_train=tf.placeholder(tf.int32)
:is_train=tf.Variable(True,name='training')
。
因此,在进行调用之前,您可以通过调用sess.run(tf.initialize\u all\u variables())进行初始化tf.train.start\u queue\u runner(sess=sess)