Skip to main content
 首页 » 编程设计

python之Tensorflow之评估()错误 : You must feed a value for placeholder tensor

2024年08月21日30tuyile006

我正在尝试使用 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,但网站上没有显示。

谢谢大家

请您参考如下方法:

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

也不起作用