【问题标题】:ValueError: Negative dimension size caused by subtracting 5 from 4ValueError:由 4 减去 5 导致的负尺寸大小
【发布时间】:2020-05-18 11:52:45
【问题描述】:

我在 Keras 中声明输入层时收到此错误消息。 我查看了一些类似的问题,但没有一个解决方案对我有帮助。

这是我的代码:

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].

【问题讨论】:

  • 您的最大池化层过多地降低了图像的维度,使其消失(负维度)...尝试从您的网络中删除它们
  • 最简单的解决方法是将padding=same 添加到所有卷积层和池化层。如果在有此类问题的任何其他问题中没有提到这一点,我会感到惊讶。

标签: python tensorflow keras neural-network keras-layer


【解决方案1】:

为了社区在此处添加解决方案的利益,尽管它存在于 cmets 中

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"))

【讨论】:

    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 2020-10-08
    • 2020-07-12
    • 2017-05-29
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多