为什么TensorFlow会提示我输入的形状和类型与占位符不匹配?

我实在搞不懂。我一直在反复尝试,我知道我可以复制粘贴一个工作的教程,但我更想理解为什么这个不工作。

import tensorflow as tfimport numpy as npimport matplotlib.pyplot as pltfrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets('MNIST_data', one_hot=True)   #simple constantslearning_rate = .01batch_size = 100training_epoch = 10t = 0l = t#gather the datax_train = mnist.train.imagesy_train = mnist.train.labelsbatch_count = int(len(x_train)/batch_size)#Set the variablesY_ = tf.placeholder(tf.float32, [None,10], name = 'Labels')X = tf.placeholder(tf.float32,[None,784], name = 'Inputs')W = tf.Variable(tf.zeros([784,10]))b = tf.Variable(tf.zeros([10]))#Build the graph (Y = WX + b)Y = tf.nn.softmax(tf.matmul(X,W) + b, name = 'softmax')cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0correct_prediction = tf.equal(tf.argmax(Y,1), tf.argmax(Y_,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy) with tf.Session() as sess:    sess.run(tf.global_variables_initializer())    for epoch in range(training_epoch):        for i in range(batch_count):            t += batch_size            print(y_train[l:t].shape)            print(x_train[l:t].shape)            print(y_train[l:t].dtype)            sess.run(train_step,feed_dict={X: x_train[l:t], Y: y_train[l:t]})            l = t        print('Epoch = ', epoch)    print("Accuracy: ", accuracy.eval(feed_dict={X: x_test, Y_: y_test}))     print('Done') 

错误信息如下:

InvalidArgumentError: You must feed a value for placeholder tensor 'Labels_2' with dtype float and shape [?,10]     [[Node: Labels_2 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:GPU:0"]

我明白还有更多需要添加的内容才能让它工作,但我现在想自己摸索。我在Jupyter Notebook上运行这个。我非常确定y_train的形状是(100, 10),类型是float64。

我已经卡了几天,所以非常感谢你的帮助。


回答:

在这行代码中将Y改为Y_:

sess.run(train_step,feed_dict={X: x_train[l:t], Y_: y_train[l:t]})

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注