簡體   English   中英

張量流中的InvalidArgumentError(softmax mnist)

[英]InvalidArgumentError in tensorflow (softmax mnist)

當我嘗試使用張量流完成softmax回歸時,出現了以下一些問題:

tensorflow.python.framework.errors_impl.InvalidArgumentError:
您必須使用dtype float [[Node:Placeholder_1 = Placeholderdtype = DT_FLOAT,shape = [],_device =“ / job:localhost / replica:0 / task:0 / cpu:0”]為占位符張量'Placeholder_1'提供一個值。 ]

通過上面的描述,我知道問題是參數類型錯誤。 但是在我的代碼中,我的數據類型與占位符相同。

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}))

我認為問題是由batch_xs(float32)和batch_ys(float32)的類型引起的。

關於如何解決這個問題有什么建議嗎?

該問題是由你傳遞造成的事實y代替y_到的feed_dict accuracy.eval電話。

這樣,您將覆蓋y的值,並且不使用占位符y_

只需將行更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM