gpt4 book ai didi

python - CNN 喀拉斯 : ValueError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:34 24 4
gpt4 key购买 nike

我在使用 Keras 时遇到此错误:是因为input_size不大于过滤器吗?

如果input_shape=(64,64,3))),则不会出现错误。

 ``ValueError: Negative dimension size caused by subtracting 3 from 2 for 
'conv2d_24/convolution' (op: 'Conv2D') with input shapes: [?,2,2,128],
[3,3,128,128].

我的代码在这里:

from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

最佳答案

默认图层填充是有效,这意味着没有填充。这样维度就从 32 减少到 16,而不是 15。您可以使用 padding='same' 代替。在这种情况下,输出的长度与原始输入的长度相同。

from keras import layers
from keras import models
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',padding='same',
input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu',padding='same'))
model.add(layers.MaxPooling2D((2, 2),padding='same'))
model.add(layers.Conv2D(128, (3, 3), activation='relu',padding='same'))
model.add(layers.MaxPooling2D((2, 2),padding='same'))
model.add(layers.Conv2D(128, (3, 3), activation='relu',padding='same'))
model.add(layers.MaxPooling2D((2, 2),padding='same'))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

关于python - CNN 喀拉斯 : ValueError: Negative dimension size caused by subtracting 3 from 2 for 'conv2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731589/

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