我对tensorflow
和tensorboard
的实现不熟悉。这是我第一次使用Tensorflow在MNIST数据上实现logistic regression
的经验。我已经成功实现了数据的逻辑回归,现在我正尝试使用tf.summary .fileWriter
将摘要记录到日志文件。
这是我的代码,会影响摘要参数
x = tf.placeholder(dtype=tf.float32, shape=(None, 784))
y = tf.placeholder(dtype=tf.float32, shape=(None, 10))
loss_op = tf.losses.mean_squared_error(y, pred)
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar("loss", loss_op)
tf.summary.scalar("training_accuracy", accuracy_op)
summary_op = tf.summary.merge_all()
这就是我训练模型的方式
with tf.Session() as sess:
sess.run(init)
writer = tf.summary.FileWriter('./graphs', sess.graph)
for iter in range(50):
batch_x, batch_y = mnist.train.next_batch(batch_size)
_, loss, tr_acc,summary = sess.run([optimizer_op, loss_op, accuracy_op, summary_op], feed_dict={x: batch_x, y: batch_y})
summary = sess.run(summary_op, feed_dict={x: batch_x, y: batch_y})
writer.add_summary(summary, iter)
添加摘要行以获取合并摘要后,我遇到了以下错误
InvalidArgumentError (see above for traceback):
You must feed a value for placeholder tensor 'Placeholder_37'
with dtype float and shape [?,10]
此错误指向Y
y = tf.placeholder(dtype=tf.float32, shape=(None, 10))
您能帮我做错什么吗?
答案 0 :(得分:1)
从错误消息中,您似乎正在某种jupyter环境中运行代码。尝试重新启动内核/运行时,然后再次运行所有内容。在jupyter中,以图形模式运行两次代码无法正常工作。如果我在下面运行代码,则第一次不会返回任何错误,而在第二次运行时(不重新启动内核/运行时),则崩溃的方式与您的崩溃相同。
我太懒了,无法检查实际模型,所以我pred=y
。 ;)
但是下面的代码不会崩溃,因此您应该能够使其适应您的需求。我已经在Google Colab中对其进行了测试。
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
x = tf.placeholder(dtype=tf.float32, shape=(None, 784), name='x-input')
y = tf.placeholder(dtype=tf.float32, shape=(None, 10), name='y-input')
pred = y
loss_op = tf.losses.mean_squared_error(y, pred)
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy_op = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.name_scope('summaries'):
tf.summary.scalar("loss", loss_op, collections=["train_summary"])
tf.summary.scalar("training_accuracy", accuracy_op, collections=["train_summary"])
with tf.Session() as sess:
summary_op = tf.summary.merge_all(key='train_summary')
train_writer = tf.summary.FileWriter('./graphs', sess.graph)
sess.run([tf.global_variables_initializer(),tf.local_variables_initializer()])
for iter in range(50):
batch_x, batch_y = mnist.train.next_batch(1)
loss, acc, summary = sess.run([loss_op, accuracy_op, summary_op], feed_dict={x:batch_x, y:batch_y})
train_writer.add_summary(summary, iter)