簡體   English   中英

ValueError:無法為形狀為((?,10)'的張量'Placeholder_1:0'提供形狀(50,)的值

[英]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.

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