我尝试了所有的一切,但我无法解决问题,如果你能帮助我解决这个问题,我将非常感激。此外,我是新手,并用不同的方法学习。
我将我的MNIST图像调整为[22,22],然后将它们重新塑造为[1,484]。最后我想提供我的网络模型,但我收到一个错误: ValueError:无法为Tensor'Plankholder_1:0'提供形状值(50,),其形状为'(?,10)'
我的代码如下:
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")
非常感谢您的帮助和提前感谢。
答案 0 :(得分:0)
您需要将 OneHot 标签设置为(50, 10)
形状以匹配形状,例如:
mnist = input_data.read_data_sets('/Users/xiachen/IdeaProjects/scala99/model/tensorflow', one_hot=True)
您还需要关注预测的会话范围,
你应该在训练tf.global_variables_initializer().run()
之前初始化变量。