【问题标题】:InvalidArgumentError while coding MNIST tutorial编码 MNIST 教程时出现 InvalidArgumentError
【发布时间】:2018-02-06 17:30:48
【问题描述】:

这些是我的第一个 tensorflow 步骤,如果其他人遇到与我相同的问题以及是否有解决方法,我想知道。

我正在编写 mnist 教程,我当前的 code-sn-p 是:

#placeholder for input
x = tf.placeholder(tf.float32,[None,784]) # None means a dimension can be of any length

#Weights for the model: 784 pixel maps to ten results
W = tf.Variable(tf.zeros([784,10]))

#bias
b = tf.Variable( tf.zeros([10])) 

#implementing the model
y = tf.matmul(x,W) + b

#implementing cross-entropy
y_ = tf.placeholder(tf.float32,[None,10])

#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
cross_entropy = tf.reduce_mean(
     tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess=tf.InteractiveSession()
tf.global_variables_initializer().run()

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
for _ in range(1000):
    batch_xs, batch_xy64 = mnist.train.next_batch(100)
    batch_xy = batch_xy64.astype(np.float32)
    sess.run(train_step , feed_dict={x:batch_xs,y:batch_xy})

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

print (sess.run(accuracy,feed_dict={x:mnist.test.images, y_:mnist.test.labels}))

首先我尝试了 MNIST 描述中的 cross_entropy 和提供的源代码中的那个,这没有任何区别。

请注意,我明确尝试强制转换 batch_xy,因为它以浮点 64 的形式返回。

这似乎也是问题所在,因为 session.run float32 张量和变量似乎是预期的。

就我所看到的调试代码而言,mnist 中的 labes 以 float64 形式返回 - 这也许可以解释我的错误:

... 文件“/home/braunalx/python-workspace/LearnTensorFlow/firstSteps/MNIST_Start.py”,第 40 行,在 mnist_run y_ = tf.placeholder(tf.float32,[None,10]) 占位符中的文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py”,第 1548 行 返回 gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name) ... InvalidArgumentError(参见上面的回溯):您必须为占位符张量“Placeholder_1”提供一个值,其 dtype 为 float 和 shape [?,10] [[节点:Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

提供的 mnist 数据有问题吗?

【问题讨论】:

    标签: tensorflow mnist


    【解决方案1】:

    错误表明您没有为所需的占位符提供值。在这行sess.run(train_step , feed_dict={x:batch_xs,y:batch_xy}) 上将y 替换为y_

    【讨论】:

    • 哦 - 我真愚蠢 - 非常感谢.... - 我在这个地方思考了几个小时,却没有意识到。这样就解决了。
    • 当然——我想,我已经这样做了。在 stackoverflow 上的第一天。
    猜你喜欢
    • 2018-01-04
    • 2018-11-05
    • 2017-04-13
    • 1970-01-01
    • 2021-06-30
    • 2018-09-27
    • 2017-10-11
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多