ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.18k stars 12.92k forks source link

Chapter 13 Question 7 Code #566

Open lkoll opened 4 years ago

lkoll commented 4 years ago

The code provided to produce a CNN seems to error out. I am able to run all of the code up to the very last block.

with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for X_batch, y_batch in shuffle_batch(X_train, y_train, batch_size):
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
        acc_batch = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
        acc_test = accuracy.eval(feed_dict={X: X_test, y: y_test})
        print(epoch, "Last batch accuracy:", acc_batch, "Test accuracy:", acc_test)

        save_path = saver.save(sess, "./my_mnist_model")

Which produces the following error.

ValueError: Cannot feed value of shape (100, 784) for Tensor 'inputs/X:0', which has shape '(?, 1960)'

Has anyone else encountered this or knowledgeable about what could be going on?

ghost commented 4 years ago

566

Here source of the problem lies at the position where you defined placeholder X. It should be like: X = tf.placeholder(tf.float32, shape=[None, n_inputs], name="X") given, height = 28 width = 28 n_inputs = height * width

Notice n_inputs evaluate to 784 here, so X has the shape (?, 784). But in your case X evaluates to shape (?, 1960).

I hope this resolves the error.