首页 文章

tensorflow InvalidArgumentError:您必须使用dtype float为占位符张量提供值

提问于
浏览
2

我是tensorflow的新手,想要训练分类的逻辑模型 .

# Set model weights
W = tf.Variable(tf.zeros([30, 16]))
b = tf.Variable(tf.zeros([16]))
train_X, train_Y, X, Y = input('train.csv')

#construct model
pred = model(X, W, b)
# Minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(pred), reduction_indices=1))
# Gradient Descent
learning_rate = 0.1
#optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initializing the variables
init = tf.initialize_all_variables()

get_ipython().magic(u'matplotlib inline')
import collections
import matplotlib.pyplot as plt

training_epochs = 200
batch_size = 300
train_X, train_Y, X, Y = input('train.csv')
acc = []
x = tf.placeholder(tf.float32, [None, 30]) 
y = tf.placeholder(tf.float32, [None, 16])
with tf.Session() as sess:
     sess.run(init)
     # Training cycle
     for epoch in range(training_epochs):
         avg_cost = 0.0
         #print(type(y_train[0][0]))
         print(type(train_X))
         print(type(train_X[0][0]))
         print X
         _, c = sess.run([optimizer, cost], feed_dict = {x: train_X, y: train_Y})

抱怨时,feef_dict方法不起作用: InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_54' with dtype float [[Node: Placeholder_54 = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]] Caused by op u'Placeholder_54':

我检查数据类型,获取训练特征数据X:

train_X type: <type 'numpy.ndarray'>
  train_X[0][0]: <type 'numpy.float32'>
  train_X size: (300, 30)
  place_holder info : Tensor("Placeholder_56:0", shape=(?, 30), dtype=float32)

我不知道为什么抱怨 . 希望某人能提供帮助,谢谢

4 回答

  • 2

    从您的错误消息中,缺少占位符的名称_ 'Placeholder_54' -是可疑的,因为这表明在当前的解释器会话中至少创建了54个占位符 .

    没有足够的细节可以肯定地说,但我有一些怀疑 . 您是否在同一个解释器会话中多次运行相同的代码(例如,使用IPython / Jupyter或Python shell)?假设是这种情况,我怀疑你的 cost 张量取决于先前执行该代码时创建的占位符 .

    实际上,您的代码会创建两个 xy after 来构建模型的其余部分,因此看起来可能是:

    • 缺少的占位符是在以前执行此代码时创建的,或者

    • input() 函数在内部调用 tf.placeholder() ,你应该提供这些占位符(可能是张量 XY ?) .

  • 0

    我想我遇到了类似的错误 . 看来你的图形上没有那些张量的x,y,你创建了具有相同名称的占位符,但这并不意味着你的图形中有这些名称的张量 .

    这是我的问题的链接(我回答了我自己..):link

    使用它来获取图表中的所有张量(非常有用):

    [n.name for n in tf.get_default_graph().as_graph_def().node]
    
  • 0

    显示model()的代码 - 我敢打赌它定义了两个占位符:X是placeholder_56,那么placeholder_54来自哪里?

    然后将模型x,y传递给feed_dict,删除你的x,y全局占位符,一切都会工作:)

  • 1

    也许您可以尝试添加图形 with tf.Graph().as_default(): 以避免在运行jupyter notebook或ipython时重新定义占位符 .

相关问题