python - tensorflow 张量板错误: you must feed a value for placeholder tensor

标签 python tensorflow tensorboard

初学者并尝试在我的tensorflow prgm中使用tensorboard。我添加了张量板引用,正如我在教程中看到的那样,但我收到以下错误消息:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x' with dtype float [[Node: x = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

错误似乎与我在训练循环中添加的这一行有关。如果没有这一行,程序不会抛出错误:

summary = sess.run(merged_summary_op, {x: x_train, y_prim: y_train})

如果有人可以检查我下面的代码并提供帮助,谢谢:

# -*- coding: utf-8 -*-

import tensorflow as tf
sess = tf.Session()

# parms
a = tf.Variable([2.0], dtype=tf.float32, name="a")
x = tf.placeholder(tf.float32, name="x")
b = tf.Variable([1.0], dtype=tf.float32, name="b")

# model : y=ax+b
with tf.name_scope('Model'):
    y = tf.add ((tf.multiply(a, x)), b)

# info for TensorBoard 
writer = tf.summary.FileWriter("D:\\tmp\\tensorflow\\logs", sess.graph)

# loss fct - mean square error
with tf.name_scope('cost'):
    y_prim = tf.placeholder(tf.float32)
    cost = tf.reduce_sum(tf.square(y - y_prim))

# optimizer = gradientdescent
with tf.name_scope('GradDes'):
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(cost)

# train datas 
x_train = [1, 2, 3, 4]
y_train = [5.2, 8.4, 11.1, 14.7]

# summary for Tensorboard
tf.summary.scalar("cost", cost)
merged_summary_op = tf.summary.merge_all()

# init vars
init = tf.global_variables_initializer()

# train loop
sess.run(init)  
for i in range (500):
    sess.run([train, cost], feed_dict={x: x_train, y_prim: y_train})
    summary = sess.run(merged_summary_op, {x: x_train, y_prim: y_train})
    a_found, b_found, curr_cost = sess.run([a, b, cost], feed_dict={x:x_train, y_prim: y_train}) 
    print("iteration :", i, "a: ", a_found, "b: ", b_found, "cost: ",curr_cost)

最佳答案

不要在单独的 sess.run 中执行合并摘要操作。试试这个:

a_found, b_found, curr_cost, summary = sess.run([a, b, cost, merged_summary_op], feed_dict={x:x_train, y_prim: y_train}) 

session 运行后,您需要调用 FileWriter 的 add_summary 方法:

writer.add_summary(summary)
writer.flush()

关于python - tensorflow 张量板错误: you must feed a value for placeholder tensor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47313838/

相关文章:

python - 对 Flask 应用程序类进行单元测试

tensorflow - 用 bazel 构建 tensorflow 不会产生静态库文件?

tensorflow - TensorBoard 2.0.0 不更新训练标量

tensorflow - Tensorboard:从命令行导出 CSV 文件

python - 使用 IronPython 中的 ManagementClass.Getinstances()

python - python 程序的 emacs 语法高亮问题

python - 使用 python 和 tensorflow 从图像中识别数字

python - 模糊图像的特定部分

python - 添加新事件/日志后刷新 TensorBoard 的最佳方法是什么?

python - 在 python 中计算 numpy 的欧氏距离