【问题标题】:TensorFlow Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0'TensorFlow 无法为张量“占位符:0”提供形状值(100、784)
【发布时间】:2017-08-30 17:19:13
【问题描述】:

我正在学习 TensorFLow。因此,为了了解如何制作某些东西,我尝试从源代码复制一些代码并执行它。但我遇到了一条错误消息。所以我尝试了这个网站的一些解决方案,但它不起作用(我在 cmets 中保留了我的测试)。

    """programme 1 """
import tensorflow as tf 
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)





X = tf.placeholder(tf.float32,[None, 28, 28, 1]) #28 * 28 taille image 1 = 1pixel car noir et blanc "X" valeur
W = tf.Variable(tf.zeros([784, 10])) # 28*28 = 784 , 10 -> 0 à 9  "W" = weight = poid
b = tf.Variable(tf.zeros([10])) #chiffre de 0 à 9 a reconnaitre "b" = constante 
init = tf.initialize_all_variables()

#model
Y = tf.nn.softmax(tf.matmul(tf.reshape(X,[-1, 784]), W) + b) #fonction "matmul": produit matriciel "-1": reussite obligatoire

#Place holder
Y_ = tf.placeholder(tf.float32, [None, 10])

#loss function
cross_entropy = -1 * tf.reduce_sum(Y_ * tf.log(Y)) #formule

# % of correct annwer found in batch
is_correct = tf.equal(tf.argmax(Y,1),tf.argmax(Y_,1))
accuracy = tf.reduce_mean(tf.cast(is_correct,tf.float32))

#training step
optimizer = tf.train.GradientDescentOptimizer(0.003) #petit pas
train_step = optimizer.minimize(cross_entropy)

sess = tf.Session()
sess.run(init) 

for i in range(10000):
    #load batch of image and ocrrects answer
    batch_X, batch_Y = mnist.train.next_batch(100)
    batch_X = np.reshape(batch_X, (-1, 784))
    #batch_Y = np.reshape(batch_Y, (-1, 784))

    train_data = {X: batch_X, Y_: batch_Y}

    #train
    sess.run(train_step, feed_dict=train_data)

    a,c = sess.run([accuracy,cross_entropy],feed = train_data)

    test_data = {X:mnist.test.images, Y_:mnist.test.labels}
    a,c = sess.run([accuracy,cross_entropy],feed = test_data)

日志:

    Traceback (most recent call last):
  File "d:\tensorflow\test1.py", line 46, in <module>
    sess.run(train_step, feed_dict=train_data)
  File "C:\Users\Proprietaire\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "C:\Users\Proprietaire\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1100, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (100, 784) for Tensor 'Placeholder:0', which has shape '(?, 28, 28, 1)'
2017-08-30 19:07:37.406994: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

第 46 行是

sess.run(train_step, feed_dict=train_data)

我可以做些什么来解决这个错误?

【问题讨论】:

    标签: python tensorflow deep-learning


    【解决方案1】:

    您收到该错误是因为您输入的内容的形状与 TensorFlow 的预期形状不匹配。要解决此问题,您可能希望将 placeholder:0 的数据重塑为 batch_X (?, 28, 28, 1)。例如,您将执行以下操作:

    batch_X = np.reshape(batch_X, (-1, 28, 28, 1))
    

    【讨论】:

      【解决方案2】:

      你需要重塑 X。

          X = tf.placeholder(tf.float32 , [None ,28 , 28 , 1])
          X = tf.reshape(X , [-1 , 784])
      

      【讨论】:

        【解决方案3】:

        在 TensorFlow 中定义占位符时,会话期间输入的形状应与占位符的形状相同。

        batch_X, batch_Y = mnist.train.next_batch(100) 中,batch_x 是像素值的2D 数组,其形状为[batch_size, 28*28]

        X = tf.placeholder(tf.float32,[None, 28, 28, 1])中,输入占位符被定义为具有[batch_size, 28, 28, 1]4D形状

        您可以 1) 在输入 TensorFlow 之前重塑 batch_x。例如batch_x = np.reshape(batch_x, [-1, 28, 28, 1]) 或 2) 更改占位符的形状。例如X = tf.placeholder(tf.float32,[None, 784])

        我会推荐 2),因为这样可以避免您在 TensorFlow 图表内外进行任何重塑操作。

        【讨论】:

          猜你喜欢
          • 2018-03-01
          • 2018-08-04
          • 2023-03-19
          • 1970-01-01
          • 2018-08-04
          • 1970-01-01
          • 2018-06-06
          • 2022-08-12
          • 2019-03-20
          相关资源
          最近更新 更多