I'm implementing this (https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py) by TensorFlow. My code is as follows.
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
if __name__ == '__main__':
mnist = input_data.read_data_sets('data', one_hot=True)
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
sess = tf.InteractiveSession()
x_image = tf.reshape(x, [-1,28,28,1])
W_conv1 = weight_variable([3, 3, 1, 32])
b_conv1 = bias_variable([32])
W_conv2 = weight_variable([3, 3, 32, 32])
b_conv2 = bias_variable([32])
W_fc1 = weight_variable([12*12*32, 128])
b_fc1 = bias_variable([128])
W_fc2 = weight_variable([128, 10])
b_fc2 = bias_variable([10])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
h_pool = max_pool_2x2(h_conv2)
keep_prob1 = tf.placeholder("float")
h_drop1 = tf.nn.dropout(h_pool, keep_prob1)
h_flat = tf.reshape(h_drop1, [-1, 12*12*32])
h_fc1 = tf.nn.relu(tf.matmul(h_flat, W_fc1) + b_fc1)
keep_prob2 = tf.placeholder("float")
h_drop2 = tf.nn.dropout(h_fc1, keep_prob2)
y_conv = tf.nn.softmax(tf.matmul(h_drop2, W_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.initialize_all_variables())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x: batch[0], y_: batch[1], keep_prob1: 1.0, keep_prob2: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob1: 0.25, keep_prob2: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob1: 1.0, keep_prob2: 1.0}))
When I ran, the following error occurred at a flatten layer. I tried to match the input shape, but the error message said "a tensor with 313600 values" which I had no idea where this came from.
Traceback (most recent call last):
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 972, in _do_call
return fn(*args)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 954, in _run_fn
status, run_metadata)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py", line 66, in __exit__
next(self.gen)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 313600 values, but the requested shape requires a multiple of 4608
[[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dropout/mul, Reshape_1/shape)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "mnist_tensorflow.py", line 60, in <module>
x: batch[0], y_: batch[1], keep_prob1: 1.0, keep_prob2: 1.0})
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 559, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3761, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 717, in run
run_metadata_ptr)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 915, in _run
feed_dict_string, options, run_metadata)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
target_list, options, run_metadata)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 313600 values, but the requested shape requires a multiple of 4608
[[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dropout/mul, Reshape_1/shape)]]
Caused by op 'Reshape_1', defined at:
File "mnist_tensorflow.py", line 44, in <module>
h_flat = tf.reshape(h_drop1, [-1, 12*12*32])
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1977, in reshape
name=name)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/username/Projects/projectname/keras/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 313600 values, but the requested shape requires a multiple of 4608
[[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](dropout/mul, Reshape_1/shape)]]
I've checked the similar question (Error: Tensorflow CNN dimension) and I confirmed that I defined the target image for reshaping/flattening. Please let me know if you have any ideas.
To flatten the tensor, we're going to use the TensorFlow reshape operation. So tf. reshape, we pass in our tensor currently represented by tf_initial_tensor_constant, and then the shape that we're going to give it is a -1 inside of a Python list.
The mistake was very simple. I found that I should set 'VALID', not 'SAME' in conv2 so that I can make 12, 12, 32 shape before flatting operation. Thank you turtle to help me out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With