我试图添加非常简单的MNIST示例(https://github.com/tensorflow/tensorflow/blob/r1.8/tensorflow/examples/tutorials/layers/cnn_mnist.py)以适合我遇到的问题。 问题似乎出在我输入的维度上(我相信)
原始MNIST数据具有以下属性(在第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'>
我的数据具有以下形状
Train Data: (10681, 9216)
Train Data: <class 'numpy.ndarray'>
Train Labe: (10681,)
Train Labe: <class 'numpy.ndarray'>
尤其是784 = 28 * 28和9216 = 96 * 96。因此,原始行(31)读为
input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])
我将其替换为
input_layer = tf.reshape(features["x"], [-1, 96, 96, 1])
但是,当我运行此命令时,出现的错误是:
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].
出什么问题了? 附言 我在Ubuntu16.04上使用Python3.5.2和Tensorflow1.5.0 运行原始的MNIST示例不会出错。
答案 0 :(得分:1)
问题
实际上,这是一个尺寸问题。它与您的一般网络体系结构有关,后者与您的新输入大小不匹配。您采用了cnn_mnist.py中的体系结构。这是第72行引起的问题:
pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) // 7*7*64=3136
该体系结构采用[-1, 7, 7, 64]
的形状,但是您正在传递[-1,24,24,64]
。
如何修复
更改pool2_flat
以匹配您的实际输出形状:
pool2_flat = tf.reshape(pool2, [-1, 24 * 24 * 64])