首页 文章

张量流中的InvalidArgumentError(softmax mnist)

提问于
浏览
0

当我试图用tensorflow完成softmax回归时,出现了一些问题如下:

tensorflow.python.framework.errors_impl.InvalidArgumentError:您必须使用dtype float [[Node:Placeholder_1 = Placeholderdtype = DT_FLOAT,shape = [],_ device =“/ job:localhost / replica)为占位符张量'Placeholder_1'提供值: 0 /任务:0 / CPU:0" ]]

从上面的描述中,我理解问题是参数类型错误 . 但在我的代码中,我的数据类型与占位符相同 .

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

m = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, [None, 784])
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, w)+b)
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
tf.global_variables_initializer()

for i in range(1000):
    batch_xs, batch_ys = m.train.next_batch(100)
    train_step.run({x: batch_xs, y: batch_ys})

correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: m.test.images, y: m.test.labels}))

I think the problem is caused by the type of batch_xs(float32) and batch_ys(float32).

关于如何解决这个问题的任何建议?

1 回答

  • 1

    问题是由于您将 y 而不是 y_ 传递到 accuracy.eval 调用的feed_dict中 .

    这样,您将覆盖 y 的值,并且不使用占位符 y_ .

    只需将行更改为

    print(accuracy.eval({x: m.test.images, y_: m.test.labels}))
    

相关问题