仅1个标量的Tensorboard摘要标量错误

时间:2018-07-29 18:46:30

标签: python tensorflow machine-learning tensorboard

我通过Tensorboard显示3个标量。标量中的2个没有问题(cross_entropy和准确性)。但是,当我尝试显示第三个标量(learning_rate)时,出现错误。这是我的代码:

这两个标量没有错误:

cross_entropy = -tf.reduce_sum(y * tf.log(logits))
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=Ylogits, labels=Y_)
cross_entropy = tf.reduce_mean(cross_entropy) * 100

correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(Y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

尝试显示learning_rate标量时出错:

learning_rate = 0.0001 + tf.train.exponential_decay(0.003, step, 2000, 1 / math.e)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)

这是我的张量板代码:

tf.summary.scalar("cross_entropy", cross_entropy)
tf.summary.scalar("training_accuracy", accuracy)
tf.summary.scalar("learning_rate", learning_rate)
tf.summary.image("input", x_image, 3)

with tf.Session() as sess:
    sess.run(initializer)
    merged_summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter("./visualization/1")
    writer.add_graph(sess.graph)

    for i in range(1000):
        batch_x, batch_y = mnist.train.next_batch(100)
        if i % 5 == 0:
            s = sess.run(merged_summary, feed_dict={X: batch_x, Y_: batch_y})
            writer.add_summary(s, i)
        sess.run(train_step, feed_dict={X: batch_x, Y_: batch_y, step: i})
        print(sess.run(accuracy, feed_dict={X: mnist.test.images, Y_: mnist.test.labels}))

这是我得到的错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype int32

如果我删除此行,错误消失并且张量板工作正常:

tf.summary.scalar("learning_rate", learning_rate)

1 个答案:

答案 0 :(得分:1)

好像您没有发布所有代码。有一个step变量,它大概是一个占位符,您没有将其值传递给摘要运行。由于这是计算当前学习率所必需的,因此我认为您可以通过在feed_dict中添加step来解决错误:

s = sess.run(merged_summary, feed_dict={X: batch_x, Y_: batch_y, step: i})