Tensorflow - eval() error: You must feed a value for placeholder tensor

匿名 (未验证) 提交于 2019-12-03 01:35:01

问题:

I'm trying to use eval() to understand what is happening in each learning step.

However, if I use eval() on an tf.matmul operation, then I would get an error You must feed a value for placeholder tensor.

If I removed the eval(), then everything would work properly as expected.

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() 

The exact error is:

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"]()]] 

Does anyone have a better way to log the variables? I've tried tensor_summary, but it doesn't show it on the website.

Thanks all

回答1:

Apart from AllenLavoie's comment, you can actually feed the dictionary through eval.

loss.eval(feed_dict=feed_dict) 

TensorFlow's weird API does not know that I've already fed the dictionary beforehand.

Hence: _, l, predictions, summary = session.run([optimizer, loss, train_prediction, summary_op], feed_dict=feed_dict)

Does not work even though it is placed before loss.eval()



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!