gpt4 book ai didi

python - reshape 时的 ​​Tensorflow 尺寸错误

转载 作者:行者123 更新时间:2023-12-01 09:16:21 25 4
gpt4 key购买 nike

我正在尝试调整非常简单的 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 示例不会出现错误。

最佳答案

问题

这确实是一个维度问题。它与您的一般网络架构有关,该架构与您的新输入大小不匹配。您采用了 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])

关于python - reshape 时的 ​​Tensorflow 尺寸错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com