解决 Keras CNN 模型中 Conv1D 导致的输出形状为0错误

在使用 Keras 构建 CNN 模型时,有时会遇到这样的错误提示:

ValueError: One of the dimensions in the output is <= 0 due to downsampling in conv1d_1. Consider increasing the input size. Received input shape [None, 1, 64] which would produce output shape with a zero or negative value in a dimension.

这个错误提示是由于在第二个卷积层使用了2倍的最大池化,导致输出的形状大小为0。

解决方法:

  1. 减少最大池化层的步幅: 尝试将最大池化层的步幅从2改为1,例如:
model.add(MaxPooling1D(2, strides=1))
  1. 增加输入数据的大小: 尝试增加输入数据的长度,例如:
prices = np.expand_dims(prices, axis=2)

具体解释:

这个错误通常发生在 Conv1D 层之后使用了 MaxPooling1D 层,而且 MaxPooling1D 层的步幅大于1的情况下。例如,在上面代码中,第二个 Conv1D 层的输出形状为 [None, 1, 64],其中 1 表示时间步长,64 表示特征数量。

当使用步幅为2的最大池化层时,输出形状会变为 [None, 1, 32]。然而,由于时间步长 1 小于步幅 2,因此输出形状的第一个维度(时间步长)会变为0。

总结:

为了避免此错误,建议在使用 MaxPooling1D 层时,确保其步幅小于等于输入数据的长度。如果需要使用更大的步幅,则需要增加输入数据的大小。

解决 Keras CNN 模型中 Conv1D 导致的输出形状为0错误

原文地址: https://www.cveoy.top/t/topic/oZUe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录