Reputation: 25
I am trying to addapt the very simple MNIST example (https://github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/examples/tutorials/layers/cnn_mnist.py) to suit a problem that I have. The problem seems to be somewhere in the dimension of my input ( i believe)
Where the original MNIST data has the following properties (inserted at line 125):
print(train_data.shape) # (55000, 784)
print(type(train_data)) # <class 'numpy.ndarray'>
print(train_labels.shape) # (55000, )
print(type(train_labels)) # <class 'numpy.ndarray'>
my data has the following shape
Train Data: (10681, 9216)
Train Data: <class 'numpy.ndarray'>
Train Labe: (10681,)
Train Labe: <class 'numpy.ndarray'>
in particular we have that 784=28*28 and 9216=96*96.Thus, where the original line (31) reads
input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
i replaced it with
input_layer = tf.reshape(features["x"], [-1, 96, 96, 1])
However when I run this, the error that I get is:
Traceback (most recent call last):
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line686, in _call_cpp_shape_fn_impl
input_tensors_as_shapes, status)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension size must be evenly divisible by 3136 but is 3686400 for 'Reshape_1' (op: 'Reshape') with input shapes: [100,24,24,64], [2] and with input tensors computed as partial shapes: input[1] = [?,3136].
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tensorfuck/learn.py", line 252, in <module>
tf.app.run()
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 124, in run
_sys.exit(main(argv))
File "tensorfuck/learn.py", line 238, in main
hooks=[logging_hook])
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 314, in train
loss = self._train_model(input_fn, hooks, saving_listeners)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 743, in _train_model
features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/estimator/estimator.py", line 725, in _call_model_fn
model_fn_results = self._model_fn(features=features, **kwargs)
File "tensorfuck/learn.py", line 88, in cnn_model_fn
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3997,in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3162, in create_op
compute_device=compute_device)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3208, in _create_op_helper
set_shapes_for_outputs(op)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2427, in set_shapes_for_outputs
return _set_shapes_for_outputs(op)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2400, in _set_shapes_for_outputs
shapes = shape_func(op)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2330, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line627, in call_cpp_shape_fn
require_shape_fn)
File "/home/j-pc/.local/lib/python3.5/site-packages/tensorflow/python/framework/common_shapes.py", line691, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Dimension size must be evenly divisible by 3136 but is 3686400 for 'Reshape_1' (op: 'Reshape') with input shapes: [100,24,24,64], [2] and with input tensors computed as partial shapes: input[1] = [?,3136].
Whats the problem? P.S. I use Python3.5.2 and Tensorflow1.5.0 on Ubuntu16.04 Running the original MNIST example gives no errors.
Upvotes: 1
Views: 2887
Reputation: 10719
Problem
Indeed it is a dimensionality problem. It has to do with your general network architecture, which does not match your new input size. You adopted the architecture from cnn_mnist.py. Here line 72 is causing the issue:
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) // 7*7*64=3136
The architecture assumes a [-1, 7, 7, 64]
shape, but you are passing [-1,24,24,64]
.
How to fix it
change pool2_flat
to match your acutal output shape:
pool2_flat = tf.reshape(pool2, [-1, 24 * 24 * 64])
Upvotes: 1