Error: Tensorflow CNN dimension
Error: Tensorflow CNN dimension
你好。我是 Tensorflow 的新手,正在尝试使用 CNN 运行 cifar10 数据集。
我的网络由三层构成,例如
- 卷积+最大池化
- 全连接层
- Softmax 层
下面是我的模型的tensorflow代码。
15 def model(X, w, w2, w_o, p_keep_conv, p_keep_hidden):
16
17 layer1 = tf.nn.relu(tf.nn.conv2d(X, w,strides=[1, 1, 1, 1], padding='SAME'))
18 layer1 = tf.nn.max_pool(l1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
19
20 layer1 = tf.reshape(l1,[-1,w2.get_shape().as_list()[0]])
21 layer1 = tf.nn.dropout(l1, p_keep_conv)
22
23 layer2 = tf.nn.relu(tf.matmul(layer1, w2))
24 layer2 = tf.nn.dropout(l4, p_keep_hidden)
25
26 pyx = tf.matmul(layer2, w_o)
27 return pyx
28
输入图像有[-1, 32, 32, 3] shape.(32*32 pixel, RGB)
由于max pooling的filter为[1,2,2,1],stride为[1,2,2,1],输出channel为5,
我认为最大池化层和全连接层之间的权重形式(下面代码中的w2)需要是[5*16*16*3, 125]。
(5:通道,16:32/2 像素,3:rgb,125:输出神经元的数量)
下面是我的参数tensorflow代码
60 trX = trX.reshape(-1, 32, 32, 3) # 32x32x3 input img
61 teX = teX.reshape(-1, 32, 32, 3) # 32x32x3 input img
62
63 X = tf.placeholder("float", [None, 32, 32, 3])
64 Y = tf.placeholder("float", [None, 10])
65
66 w = init_weights([5, 5, 3, 5])
67 w2 = init_weights([5*16*16*3, 125])
68 w_o = init_weights([125, 10])
69
70 p_keep_conv = tf.placeholder("float")
71 p_keep_hidden = tf.placeholder("float")
72
73 py_x = model(X, w, w2, w_o, p_keep_conv, p_keep_hidden)
74
75 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))
76 #train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
77 train_op = tf.train.AdamOptimizer(1e-4).minimize(cost)
78 predict_op = tf.argmax(py_x, 1)
79
但是它显示如下错误。
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 715, in _do_call
return fn(*args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 697, in _run_fn
status, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/contextlib.py", line 66, in __exit__
next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/errors.py", line 450, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool, Reshape/shape)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "convCifar.py", line 99, in <module>
p_keep_conv: 0.8, p_keep_hidden: 0.5})
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 636, in _run
feed_dict_string, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 708, in _do_run
target_list, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 728, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool, Reshape/shape)]]
Caused by op 'Reshape', defined at:
File "convCifar.py", line 82, in <module>
py_x = model(X, w, w4, w_o, p_keep_conv, p_keep_hidden)
File "convCifar.py", line 27, in model
l1 = tf.reshape(l1,[-1,w4.get_shape().as_list()[0]])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1383, in reshape
name=name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2260, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 1230, in __init__
self._traceback = _extract_stack()
我认为问题出在"w2"的维度(最大池化层和全连接层之间的权重)。另外,我不明白6400是怎么发生的。
如何修复错误?
信息少请告知。
谢谢!
错误tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
表明第20行tf.reshape()
的输入张量有多个值不是3840的倍数。
那是因为张量 l1
没有在函数 model
中定义(您可能之前使用过它,它可能有 6400 个值)。您可能想要设置 l1=layer1
。请注意,张量 l4
也未在函数 model
中定义。
如果我的回答不能解决您的错误,请告诉我。
你好。我是 Tensorflow 的新手,正在尝试使用 CNN 运行 cifar10 数据集。 我的网络由三层构成,例如
- 卷积+最大池化
- 全连接层
- Softmax 层
下面是我的模型的tensorflow代码。
15 def model(X, w, w2, w_o, p_keep_conv, p_keep_hidden):
16
17 layer1 = tf.nn.relu(tf.nn.conv2d(X, w,strides=[1, 1, 1, 1], padding='SAME'))
18 layer1 = tf.nn.max_pool(l1, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
19
20 layer1 = tf.reshape(l1,[-1,w2.get_shape().as_list()[0]])
21 layer1 = tf.nn.dropout(l1, p_keep_conv)
22
23 layer2 = tf.nn.relu(tf.matmul(layer1, w2))
24 layer2 = tf.nn.dropout(l4, p_keep_hidden)
25
26 pyx = tf.matmul(layer2, w_o)
27 return pyx
28
输入图像有[-1, 32, 32, 3] shape.(32*32 pixel, RGB)
由于max pooling的filter为[1,2,2,1],stride为[1,2,2,1],输出channel为5,
我认为最大池化层和全连接层之间的权重形式(下面代码中的w2)需要是[5*16*16*3, 125]。
(5:通道,16:32/2 像素,3:rgb,125:输出神经元的数量)
下面是我的参数tensorflow代码
60 trX = trX.reshape(-1, 32, 32, 3) # 32x32x3 input img
61 teX = teX.reshape(-1, 32, 32, 3) # 32x32x3 input img
62
63 X = tf.placeholder("float", [None, 32, 32, 3])
64 Y = tf.placeholder("float", [None, 10])
65
66 w = init_weights([5, 5, 3, 5])
67 w2 = init_weights([5*16*16*3, 125])
68 w_o = init_weights([125, 10])
69
70 p_keep_conv = tf.placeholder("float")
71 p_keep_hidden = tf.placeholder("float")
72
73 py_x = model(X, w, w2, w_o, p_keep_conv, p_keep_hidden)
74
75 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y))
76 #train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
77 train_op = tf.train.AdamOptimizer(1e-4).minimize(cost)
78 predict_op = tf.argmax(py_x, 1)
79
但是它显示如下错误。
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 715, in _do_call
return fn(*args)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 697, in _run_fn
status, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/contextlib.py", line 66, in __exit__
next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/errors.py", line 450, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool, Reshape/shape)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "convCifar.py", line 99, in <module>
p_keep_conv: 0.8, p_keep_hidden: 0.5})
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 636, in _run
feed_dict_string, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 708, in _do_run
target_list, options, run_metadata)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 728, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
[[Node: Reshape = Reshape[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool, Reshape/shape)]]
Caused by op 'Reshape', defined at:
File "convCifar.py", line 82, in <module>
py_x = model(X, w, w4, w_o, p_keep_conv, p_keep_hidden)
File "convCifar.py", line 27, in model
l1 = tf.reshape(l1,[-1,w4.get_shape().as_list()[0]])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1383, in reshape
name=name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 2260, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 1230, in __init__
self._traceback = _extract_stack()
我认为问题出在"w2"的维度(最大池化层和全连接层之间的权重)。另外,我不明白6400是怎么发生的。
如何修复错误?
信息少请告知。 谢谢!
错误tensorflow.python.framework.errors.InvalidArgumentError: Input to reshape is a tensor with 6400 values, but the requested shape requires a multiple of 3840
表明第20行tf.reshape()
的输入张量有多个值不是3840的倍数。
那是因为张量 l1
没有在函数 model
中定义(您可能之前使用过它,它可能有 6400 个值)。您可能想要设置 l1=layer1
。请注意,张量 l4
也未在函数 model
中定义。
如果我的回答不能解决您的错误,请告诉我。