Python - TensorFlow / tf ValueError:无法为Tensor'占位符:0'提供形状值(100,784),其形状为'(?,28,28,1)'

时间:2017-09-20 17:39:29

标签: python tensorflow mnist valueerror

我刚开始用tensorflow学习深度学习,所以我想实现我今天阅读的教程link。实际代码是这样的,我试图实现一个非常基本的NN来对0-9中的数字进行分类。

import tensorflow as tf
# load dataset
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])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

init = tf.initialize_all_variables()

# model
Y = tf.nn.softmax(tf.matmul(tf.reshape(X,[-1, 784]), W) + b)
# placeholder fr correct labels
Y_ = tf.placeholder(tf.float32, [None, 10])

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

# % of a correct answer 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))


# optimizer, workhorse of a NN
optimizer = tf.train.GradientDescentOptimizer(0.003)
train_step = optimizer.minimize(cross_entropy)

# start the session
sess = tf.Session() 
sess.run(init)

# feed the data for training
for i in range(10000):
    # load batch of images and correct answers
    batch_X, batch_Y = mnist.train.next_batch(100)
    train_data={X: batch_X, Y_: batch_Y}

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

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

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

但是当我尝试运行时,我会收到这样的错误,

I tensorflow/core/common_runtime/gpu/gpu_device.cc:885] Found device 0 with properties: 
name: GeForce 840M
major: 5 minor: 0 memoryClockRate (GHz) 1.124
pciBusID 0000:03:00.0
Total memory: 1.96GiB
Free memory: 1.72GiB
I tensorflow/core/common_runtime/gpu/gpu_device.cc:906] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:916] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce 840M, pci bus id: 0000:03:00.0)
Traceback (most recent call last):
  File "mnist_v1.py", line 41, in <module>
    sess.run(optimizer, feed_dict=train_data)
  File "/home/gopi34/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/home/gopi34/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 944, 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)'

1 个答案:

答案 0 :(得分:1)

您应该使用大小为A = 3; // at #1 #1 // (does not matter) --> #2 A <= A + 1; // #2 will do A + 1 and wait till the end of the cycle B <= A + 1; // #2 same as above // at the end of the cycle #2 (nba scheduling bucket) before switching to #3 // A and B will be assigned '4' #1 // --> #3 // new values of A and B are available here (4) $display("Non-blocking: A= %d B= %d", A, B ); 的占位符,即线性化每个图像。