首页 > 解决方案 > ValueError:无法为具有形状“(?,10)”的张量“Placeholder_1:0”提供形状(100,)的值

问题描述

我是 tensorflow 的新手,我使用 tensorflow 实现逻辑回归,我的代码与“TensorFlow 入门”一书相同,但是当我运行它时,出现错误:ValueError: Cannot feed value of shape (100,) for张量“Placeholder_1:0”,其形状为“(?,10)”。有人可以帮我吗?

mnist = input_data.read_data_sets("tmp/data/", one_hot=False)

# set the total number of epochs of the training phase:
training_epochs = 25
learning_rate = 0.01
batch_size = 100
display_step = 1

#                           building the model
# define 'x' as the input tensor, it represents the MNIST data
# images of size 28 x 28 = 784 pixels:
x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
evidence = tf.matmul(x, W) + b

activation = tf.nn.softmax(evidence)
# error function
cross_entropy = y * tf.lgamma(activation)
# cost function
cost = tf.reduce_mean(-tf.reduce_sum(cross_entropy, reduction_indices = 1))
# minimize the cost function
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

avg_set = []
epoch_set = []

init = tf.global_variables_initializer()
with tf.Session() as sess:
    for epoch in range(training_epochs):
        avg_cost = 0
        total_batch = int(mnist.train.num_examples / batch_size)
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            # Fit the training using the batch data:
            sess.run(optimizer, feed_dict = {x: batch_xs, y: batch_ys})
            # compute the average loss:
            avg_cost += sess.run(cost, feed_dict = {x: batch_xs,
                                                    y: batch_ys}) / total_batch
        # During the computation, we display a log per epoch step:
        if epoch % display_step == 0:
            print("Epoch: ", '%04d' % (epoch + 1),
                  "cost=", "{:.9f}".format(avg_cost))
    print("Training phase finished.")

    correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("MODEL accuracy: ", accuracy.eval({x: mnist.test.images,
                                             y: mnist.test.labels}))

标签: pythontensorflow

解决方案


推荐阅读