【问题标题】:tensorflow InvalidArgumentError: You must feed a value for placeholder tensor with dtype floattensorflow InvalidArgumentError:您必须为具有 dtype 浮点数的占位符张量提供一个值
【发布时间】:2016-09-13 23:14:48
【问题描述】:

我是 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:您必须为占位符张量提供一个值 'Placeholder_54' dtype float [[节点:Placeholder_54 = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]] 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)

我不知道它为什么抱怨。希望有人能帮忙,谢谢

【问题讨论】:

  • 如果您使用的是 jupyter 笔记本,请在定义变量之前尝试运行 tf.reset_default_graph()。我遇到了一些类似的问题,这是帮助解决问题的原因之一。

标签: python types tensorflow


【解决方案1】:

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

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

确实,您的代码创建了两个 tf.placeholder() 张量 xy 构建模型的其余部分之后,因此看起来很可能:

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2018-12-29
          • 2019-01-23
          • 2018-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-04
          • 2017-11-02
          相关资源
          最近更新 更多