1. 程式人生 > >InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Y' with dtype float

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Y' with dtype float

  我發現一個問題,當你使用Tensorboard進行視覺化操作時: 如果你定義了

       MERGED = tf.summary.merge_all();

  這個操作,之後如果你單獨使用SESS.run([MERGED]),那麼就會報上面的這個錯誤;

 

  此時你應該改成和其他的豬op一起進行SESS.run([TRAIN,MERGED]), 改了之後就不會再報這個錯誤,

       具體原因我也很難解釋清楚。之前針對這個錯誤,查了挺長時間,有一些解決方法,但都沒有解決我的問題:

https://stackoverflow.com/questions/35114376/error-when-computing-summaries-in-tensorflow

https://blog.csdn.net/lyrassongs/article/details/75012464

  

  後來我是參考了一份Github上一份程式,按它的樣子改才改過來了。

# -*- coding: utf-8 -*-
"""
Created on Wed Oct 31 17:07:38 2018

@author: LiZebin
"""

from __future__ import print_function
import numpy as np
import tensorflow as tf

tf.reset_default_graph()
SESS = tf.Session()

LOGDIR = "logs/"

X = np.arange(0, 1000, 2, dtype=np.float32)
Y = X*2.3+5.6
X_ = tf.placeholder(tf.float32, name="X")
Y_ = tf.placeholder(tf.float32, name="Y")
W = tf.get_variable(name="Weights", shape=[1],
                    dtype=tf.float32, initializer=tf.random_normal_initializer())
B = tf.get_variable(name="bias", shape=[1],
                    dtype=tf.float32, initializer=tf.random_normal_initializer())
PRED = W*X_+B
LOSS = tf.reduce_mean(tf.square(Y_-PRED))
tf.summary.scalar("Loss", LOSS)
TRAIN = tf.train.GradientDescentOptimizer(learning_rate=0.0000001).minimize(LOSS)
WRITER = tf.summary.FileWriter(LOGDIR, SESS.graph)
MERGED = tf.summary.merge_all()

SESS.run(tf.global_variables_initializer())
for step in range(20000):
    c1, c2, loss, RS, _ = SESS.run([W, B, LOSS, MERGED, TRAIN], feed_dict={X_:X, Y_:Y})   ####如果單獨在後面寫RS=SESS.run(MERGED)就會報之前那個錯誤
    WRITER.add_summary(RS)
    if step%500 == 0:
        temp = "c1=%s, c2=%s, loss=%s"%(c1, c2, loss)
        print(temp)
SESS.close()