2
votes

Beginner and trying to use tensorboard in my tensorflow prgm. I added tensorboard refs as I saw in the tutorials, but I get the error message below :

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

Error seems related to this line I added in my training loop. Without this line the program throws no errors:

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

Thanks if somebody can check my code below and help:

# -*- 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)
2
The code runs perfectly for me. Which version of tensorflow do you have? - grovina
I am running Tensorflow 1.4.0 under win 10. But my code is running OK now , doing as MattScarpino proposed. Thanks for watching. - Pierre Grignou

2 Answers

1
votes

Don't execute the merged summary operation in a separate sess.run. Try this instead:

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

After the session runs, you need to call the add_summary method of your FileWriter:

writer.add_summary(summary)
writer.flush()
1
votes

Yes, I have the same problem with your code and I solved it by resetting the graph adding this to the beginning of your graph definition

tf.reset_default_graph()

I think you are doing everything correctly