0

I'm new to tensorflow and try to learn how to visualize with tensorboard. I have the following code for a neural network with one hidden layer. When I tried to run the code in my jupyter notebook, it gave me the error like this:

StatusNotOK Traceback (most recent call last) StatusNotOK: Invalid argument: You must feed a value for placeholder tensor 'y_2' with dtype float and shape [20,10] [[Node: y_2 = Placeholderdtype=DT_FLOAT, shape=[20,10], _device="/job:localhost/replica:0/task:0/cpu:0"]]

However, when I comment this line: summary = session.run (merged, feed_dict=feed_dict) This program runs OK. What went wrong? tried hard but could not figure out. Help appreciated.

n_features = x_train.shape[1]
n_samples = x_train.shape[0]
n_labels = 10
n_hidden = 200
epoch_train = 200
learning_rate = 0.01
batch_size = 20

#build graph
x_tr = tf.placeholder(tf.float32, shape=(None, n_features), name='x')
y_tr = tf.placeholder(tf.float32, shape=(None, n_labels), name='y')


w1 = tf.Variable (tf.truncated_normal([n_features, n_hidden]),    name='weight1')
b1 = tf.Variable (tf.zeros([n_hidden]), name='bias1')
w2 = tf.Variable (tf.truncated_normal([n_hidden, n_labels]), name = 'weight2')
b2 = tf.Variable(tf.zeros([n_labels]), name='bias2')

w1_hist = tf.histogram_summary('weight1', w1)
w2_hist = tf.histogram_summary('weight2', w2)
b1_hist = tf.histogram_summary('bias1', b1)
b2_hist = tf.histogram_summary('bias2', b2)
y_hist = tf.histogram_summary('y', y_tr)


with tf.name_scope('hidden') as scope:    
    z1 = tf.matmul(x_tr, w1)+b1
    a1 = tf.nn.relu (z1)

with tf.name_scope('output') as scope:    
    z2 = tf.matmul(a1, w2)+b2
    a2 = tf.nn.softmax (z2)

with tf.name_scope('cost') as scope:
    loss = tf.reduce_mean (tf.nn.softmax_cross_entropy_with_logits(z2, y_tr))
    cost_summ = tf.scalar_summary ('cost', loss)

with tf.name_scope('train') as scope:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

def acc (pred, y):
    return (np.mean(np.argmax(pred, 1)==np.argmax(y,1)))


#computing

with tf.Session() as session:

session.run(tf.initialize_all_variables())

merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter (' ./logs/logs_1')

for epoch in range (epoch_train):

    offset = epoch*batch_size % (x_train.shape[0]-batch_size)
    x_tr_batch = x_train[offset:offset+batch_size, :]
    y_tr_batch = y_train[offset:offset+batch_size, :]
    feed_dict = {x_tr:x_tr_batch, y_tr:y_tr_batch}

    _, cost, prediction = session.run ([optimizer, loss, a2], feed_dict=feed_dict)

    summary = session.run (merged, feed_dict=feed_dict)
    writer.add_summary(summary,epoch)

    if epoch % 20 ==0:
        print ('training accuracy:', acc(prediction, y_tr_batch))
        print ('cost at epoch {} is:'.format(epoch), cost)
pred_ts = session.run (a2, feed_dict = {x_tr:x_test})
print ('test accuracy is:', acc(pred_ts, y_test))
Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91
zesla
  • 11,155
  • 16
  • 82
  • 147
  • This is weird indeed. TensorFlow is looking for a placeholder names `y_2` whereas you only have one placeholder which has name `y`. It looks like as if there was a second "hidden" placeholder y_tr. Can you look at the entire code and make sure there are no other placeholders? – Olivier Moindrot Jun 03 '16 at 19:57
  • Also, `merged = tf.merge_all_summaries()` might take into account previous code so make sure there is nothing executed before what you posted. – Olivier Moindrot Jun 03 '16 at 19:57
  • I cleared all namespace in my jupyter notebook and rerunned. I got this: StatusNotOK Traceback (most recent call last) StatusNotOK: Invalid argument: You must feed a value for placeholder tensor 'x_2' with dtype float [[Node: x_2 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] I am totally confused. – zesla Jun 03 '16 at 20:19
  • Now it seems you have another hidden placeholder named `x` – Olivier Moindrot Jun 03 '16 at 20:24
  • I closed the notebook and restarted it. run the code, still not working. any error in my code? I cannot figure it out. again if I comment these two lines, it works. summary = session.run (merged, feed_dict=feed_dict) writer.add_summary(summary,epoch) – zesla Jun 03 '16 at 20:32

1 Answers1

3

Following the discussion in the comments, I think the merged summary merged = tf.merge_all_summaries() is taking into account previous summaries (coming from I don't know where), which depend on placeholders not initialized.

The quickest way to fix that (apart from finding and removing these previous and unused placeholders) is to use tf.merge_summary(...) with all the summaries you want to add:

merged = tf.merge_summary([w1_hist, b1_hist, w2_hist, b2_hist, y_hist, cost_summ])
Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91