【发布时间】:2017-11-15 17:32:42
【问题描述】:
初学者并尝试在我的 tensorflow prgm 中使用 tensorboard。正如我在教程中看到的那样,我添加了 tensorboard refs,但我收到以下错误消息:
InvalidArgumentError(参见上面的回溯):您必须为占位符张量“x”提供一个值,其 dtype 为 float [[节点: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)
【问题讨论】:
-
代码对我来说运行完美。你有哪个版本的 tensorflow?
-
我在 win 10 下运行 Tensorflow 1.4.0。但我的代码现在运行正常,按照 MattScarpino 的建议进行。感谢收看。
标签: python tensorflow tensorboard