ValueError: Negative dimension size caused by subtracting 5 from 4
ValueError: Negative dimension size caused by subtracting 5 from 4
我在 Keras 中声明输入层时收到此错误消息。
我查看了一些类似的问题,但 none 的解决方案对我有所帮助。
这是我的代码:
model = Sequential()
model.add(Conv2D(32, kernel_size=(5,5), strides = 1, activation="relu", input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Conv2D(64, kernel_size=(5,5), strides = 1, activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Conv2D(128, kernel_size=(5,5), strides = 1, activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
完整的错误信息:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1653 try:
-> 1654 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1655 except errors.InvalidArgumentError as e:
InvalidArgumentError: Negative dimension size caused by subtracting 5 from 4 for '{{node conv2d_17_1/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](max_pooling2d_9_1/Identity, conv2d_17_1/Conv2D/ReadVariableOp)' with input shapes: [?,20,4,32], [5,5,32,128].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
14 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1655 except errors.InvalidArgumentError as e:
1656 # Convert to ValueError for backwards compatibility.
-> 1657 raise ValueError(str(e))
1658
1659 return c_op
ValueError: Negative dimension size caused by subtracting 5 from 4 for '{{node conv2d_17_1/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](max_pooling2d_9_1/Identity, conv2d_17_1/Conv2D/ReadVariableOp)' with input shapes: [?,20,4,32], [5,5,32,128].
为了社区的利益,尽管它出现在评论中,但在此处添加解决方案
model = Sequential()
model.add(Conv2D(32, kernel_size=(5,5), strides = 1, padding='same', activation="relu", input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same', data_format='channels_last'))
model.add(Conv2D(64, kernel_size=(5,5), strides = 1, padding='same', activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same', data_format='channels_last'))
model.add(Conv2D(128, kernel_size=(5,5), strides = 1, padding='same', activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same', data_format='channels_last'))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
我在 Keras 中声明输入层时收到此错误消息。 我查看了一些类似的问题,但 none 的解决方案对我有所帮助。
这是我的代码:
model = Sequential()
model.add(Conv2D(32, kernel_size=(5,5), strides = 1, activation="relu", input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Conv2D(64, kernel_size=(5,5), strides = 1, activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Conv2D(128, kernel_size=(5,5), strides = 1, activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), data_format='channels_first'))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))
完整的错误信息:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1653 try:
-> 1654 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1655 except errors.InvalidArgumentError as e:
InvalidArgumentError: Negative dimension size caused by subtracting 5 from 4 for '{{node conv2d_17_1/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](max_pooling2d_9_1/Identity, conv2d_17_1/Conv2D/ReadVariableOp)' with input shapes: [?,20,4,32], [5,5,32,128].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
14 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1655 except errors.InvalidArgumentError as e:
1656 # Convert to ValueError for backwards compatibility.
-> 1657 raise ValueError(str(e))
1658
1659 return c_op
ValueError: Negative dimension size caused by subtracting 5 from 4 for '{{node conv2d_17_1/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true](max_pooling2d_9_1/Identity, conv2d_17_1/Conv2D/ReadVariableOp)' with input shapes: [?,20,4,32], [5,5,32,128].
为了社区的利益,尽管它出现在评论中,但在此处添加解决方案
model = Sequential()
model.add(Conv2D(32, kernel_size=(5,5), strides = 1, padding='same', activation="relu", input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same', data_format='channels_last'))
model.add(Conv2D(64, kernel_size=(5,5), strides = 1, padding='same', activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same', data_format='channels_last'))
model.add(Conv2D(128, kernel_size=(5,5), strides = 1, padding='same', activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same', data_format='channels_last'))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))