Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow-eval()错误:必须为占位符tensor提供一个值_Python_Tensorflow - Fatal编程技术网

Python Tensorflow-eval()错误:必须为占位符tensor提供一个值

Python Tensorflow-eval()错误:必须为占位符tensor提供一个值,python,tensorflow,Python,Tensorflow,我试图使用eval()来理解每个学习步骤中发生的事情 但是,如果我在tf.matmul操作中使用eval(),则会得到一个错误,您必须为占位符张量提供一个值 如果我删除了eval(),那么一切都将按预期正常工作 num_steps = 3001 with tf.Session(graph=graph) as session: tf.global_variables_initializer().run() writer = tf.summary.FileWriter("/home

我试图使用eval()来理解每个学习步骤中发生的事情

但是,如果我在tf.matmul操作中使用eval(),则会得到一个错误
,您必须为占位符张量提供一个值

如果我删除了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不知道我已经事先输入了字典

因此:
\uL,预测,摘要=会话.run([optimizer,loss,train\u prediction,summary\u op],feed\u dict=feed\u dict)


即使将其放置在
loss.eval()

之前也不起作用如果您只想在图形执行期间打印张量的值,可以将一个
tf.print
节点附加到图形()。只需确保在图形()中使用
Print
操作的输出即可。@AllenLavoie tf.Print prints打印到控制台,而不是iPython笔记本,有什么方法可以更改吗?感谢
tf.InteractiveSession()
(而不是
tf.Session()
)为我修复了此问题。@AllenLavoie再次感谢您的帮助。当我改为InteractiveSession时,它给了我一个关于InteractiveSession的
属性错误:\uuuu exit\uuuu
。@AllenLavoie实际上我注释掉了这行,并将其替换为
session=tf。InteractiveSession(graph=graph)
,但是,它仍然打印到控制台而不是笔记本。Tensorflow确实需要比这更好的文档。。。