![](/img/trans.png)
[英]ValueError: Cannot feed value of shape (50, 10) for Tensor 'Placeholder_1:0', which has shape '(?, 12)'
[英]ValueError: Cannot feed value of shape (50,) for Tensor 'Placeholder_1:0', which has shape '(?, 10)'
我尝试了所有方法,但无法解决问题,如果您能帮助我解决该问题,我将不胜感激。 另外,我对此很陌生,可以通过不同的方法来学习它。
我将MNIST图像调整为[22,22],然后将其调整为[1,484]。 最后,我想输入我的网络模型,但出现错误:ValueError:无法输入张量为'(?,10)'的张量'Placeholder_1:0'的形状(50,)值
我的代码如下:
import tensorflow as tf
import numpy as np
from skimage import transform
tf.reset_default_graph()
from numpy import array
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
x = tf.placeholder(tf.float32, [None, 484])
W = tf.get_variable("weights", shape=[484, 10],
initializer=tf.random_normal_initializer())
b = tf.get_variable("bias", shape=[10],
initializer=tf.random_normal_initializer())
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.2).minimize(cross_entropy)
batch_size=50
for _ in range(10000):
batch_img, batch_label = mnist.train.next_batch(batch_size)
imgs = batch_img.reshape((-1, 28, 28, 1))
print(imgs.shape[0])
resized_imgs = np.zeros((imgs.shape[0], 22, 22, 1))
for i in range(imgs.shape[0]):
resized_imgs[i, ..., 0] = transform.resize(imgs[i, ..., 0],
(22,22))
image = array(resized_imgs).reshape(imgs.shape[0], 484)
print(image.shape)
with tf.Session() as sess:
sess.run(train_step, feed_dict={x: image, y_: batch_label})
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}))
print ("done with training")
非常感谢您的帮助,并在此先感谢。
您需要将OneHot标签设置为(50, 10)
50,10 (50, 10)
形状以匹配该形状,例如:
mnist = input_data.read_data_sets('/Users/xiachen/IdeaProjects/scala99/model/tensorflow', one_hot=True)
您还需要注意会话范围以进行预测 ,
并且您应该在训练tf.global_variables_initializer().run()
之前初始化变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.