Python 运行合并摘要操作时占位符的提要值出错

Python 运行合并摘要操作时占位符的提要值出错,python,tensorflow,Python,Tensorflow,我是tensorflow的新手,尝试学习如何使用tensorboard进行可视化。我有下面的代码用于一个有一个隐藏层的神经网络。当我试图在jupyter笔记本中运行代码时,它给了我如下错误: StatusNotOK回溯(最近一次呼叫上次) StatusNotOK:无效参数:必须为带有dtype float和shape[20,10]的占位符张量“y_2”提供一个值 [[Node:y_2=Placeholderdtype=DT_FLOAT,shape=[20,10],_device=“/job:lo

我是tensorflow的新手,尝试学习如何使用tensorboard进行可视化。我有下面的代码用于一个有一个隐藏层的神经网络。当我试图在jupyter笔记本中运行代码时,它给了我如下错误:

StatusNotOK回溯(最近一次呼叫上次) StatusNotOK:无效参数:必须为带有dtype float和shape[20,10]的占位符张量“y_2”提供一个值 [[Node:y_2=Placeholderdtype=DT_FLOAT,shape=[20,10],_device=“/job:localhost/replica:0/task:0/cpu:0”]

但是,当我注释这一行时:summary=session.run(合并,feed\u dict=feed\u dict) 这个程序运行正常。 出了什么问题?我努力了,但还是想不出来。谢谢你的帮助

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))

根据评论中的讨论,我认为合并摘要
merged=tf.merge\u all\u summaries()
考虑了以前的摘要(来自我不知道在哪里),这些摘要依赖于未初始化的占位符

解决此问题的最快方法(除了查找和删除这些以前的和未使用的占位符外)是使用
tf.merge_summary(…)
和所有要添加的摘要:

merged=tf.合并汇总([w1历史、b1历史、w2历史、b2历史、y历史、成本汇总])

根据评论中的讨论,我认为合并摘要
merged=tf.merge\u all\u summaries()
考虑了以前的摘要(来自我不知道在哪里),这些摘要依赖于未初始化的占位符

解决此问题的最快方法(除了查找和删除这些以前的和未使用的占位符外)是使用
tf.merge_summary(…)
和所有要添加的摘要:

merged=tf.合并汇总([w1历史、b1历史、w2历史、b2历史、y历史、成本汇总])

这确实很奇怪。TensorFlow正在寻找一个占位符名称
y_2
,而您只有一个占位符名称
y
。看起来好像有第二个“隐藏”占位符y\u tr。你能看看整个代码并确保没有其他占位符吗?另外,
merged=tf.merge\u all\u summaries()
可能会考虑以前的代码,因此请确保在发布之前没有执行任何操作。我清除了jupyter笔记本中的所有命名空间,然后重新运行。我得到了以下信息:StatusNotOK Traceback(最近一次调用上次)StatusNotOK:无效参数:必须为带有dtype float[[Node:x_2=placeholder[dtype=DT_float,shape=[],[U device=“/job:localhost/replica:0/task:0/cpu:0”]()]的占位符张量“x_2”提供一个值我完全糊涂了。现在你似乎有另一个名为
x
的隐藏占位符。我关闭了笔记本并重新启动了它。运行代码,但仍然不工作。我的代码有错误吗?我想不出来。同样,如果我评论这两行,它是有效的。summary=session.run(合并,feed\u dict=feed\u dict)writer.add\u summary(summary,epoch)这确实很奇怪。TensorFlow正在寻找一个占位符名称
y_2
,而您只有一个占位符名称
y
。看起来好像有第二个“隐藏”占位符y\u tr。你能看看整个代码并确保没有其他占位符吗?另外,
merged=tf.merge\u all\u summaries()
可能会考虑以前的代码,因此请确保在发布之前没有执行任何操作。我清除了jupyter笔记本中的所有命名空间,然后重新运行。我得到了以下信息:StatusNotOK Traceback(最近一次调用上次)StatusNotOK:无效参数:必须为带有dtype float[[Node:x_2=placeholder[dtype=DT_float,shape=[],[U device=“/job:localhost/replica:0/task:0/cpu:0”]()]的占位符张量“x_2”提供一个值我完全糊涂了。现在你似乎有另一个名为
x
的隐藏占位符。我关闭了笔记本并重新启动了它。运行代码,但仍然不工作。我的代码有错误吗?我想不出来。同样,如果我评论这两行,它是有效的。summary=session.run(合并,feed\u dict=feed\u dict)writer.add\u summary(摘要,epoch)是的,现在它可以工作了!!非常感谢你。仍然对为什么合并所有摘要不起作用感到困惑,即使在我清除名称空间并重新启动jupyter笔记本之后。我不使用jupyter笔记本,所以我不能说!是的,现在它工作了!!非常感谢你。仍然对为什么合并所有摘要不起作用感到困惑,即使在我清除名称空间并重新启动jupyter笔记本之后。我不使用jupyter笔记本,所以我不能说!