我正在尝试使用eval()来了解每个学习步骤中发生的事情。
但是,如果我在tf.matmul操作上使用eval(),那么我会收到错误You must feed a value for placeholder tensor
。
如果我删除了eval(),那么一切都会按预期正常工作。
num_steps = 3001
with tf.Session(graph=graph) as session:
tf.global_variables_initializer().run()
writer = tf.summary.FileWriter("/home/ubuntu/tensorboard", graph=tf.get_default_graph())
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions, summary = session.run([optimizer, loss, train_prediction, summary_op], feed_dict=feed_dict)
writer.add_summary(summary, step)
# If I removed this line, then it would work
loss.eval()
batch_size = 128
graph = tf.Graph()
with graph.as_default():
with tf.name_scope('tf_train_dataset'):
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size))
with tf.name_scope('tf_train_labels'):
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
with tf.name_scope('tf_valid_dataset'):
tf_valid_dataset = tf.constant(valid_dataset)
with tf.name_scope('tf_test_dataset'):
tf_test_dataset = tf.constant(test_dataset)
with tf.name_scope('weights'):
weights = tf.Variable(tf.truncated_normal([image_size * image_size, num_labels]))
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([num_labels]))
with tf.name_scope('logits'):
logits = tf.matmul(tf_train_dataset, weights) + biases
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf_train_labels))
tf.summary.scalar("loss", loss)
with tf.name_scope('optimizer'):
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
with tf.name_scope("train_prediction"):
train_prediction = tf.nn.softmax(logits)
with tf.name_scope("valid_prediction"):
valid_prediction = tf.nn.softmax(tf.matmul(tf_valid_dataset, weights) + biases)
with tf.name_scope("test_prediction"):
test_prediction = tf.nn.softmax(tf.matmul(tf_test_dataset, weights) + biases)
with tf.name_scope("correct_prediction"):
correct_prediction = tf.equal(tf.argmax(tf_train_labels,1), tf.argmax(train_prediction,1))
with tf.name_scope("accuracy"):
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar("training_accuracy", accuracy)
summary_op = tf.summary.merge_all()
确切的错误是:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'tf_train_dataset/Placeholder' with dtype float and shape [128,784]
[[Node: tf_train_dataset/Placeholder = Placeholder[dtype=DT_FLOAT, shape=[128,784], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
有没有人有更好的方法来记录变量?我已经尝试过tensor_summary,但它没有在网站上显示。
全部谢谢
答案 0 :(得分:3)
除了AllenLavoie的评论,您实际上可以通过eval提供字典。
loss.eval(feed_dict=feed_dict)
TensorFlow奇怪的API并不知道我之前已经提供了字典。
因此:_, l, predictions, summary = session.run([optimizer, loss, train_prediction, summary_op], feed_dict=feed_dict)
即使在 loss.eval()