我正在使用 CNN 训练 Fashion MNIST 数据。由于过度拟合,我尝试添加 Dropout 层。但它不起作用

在我添加 Dropout 之前,模型运行良好。

def fashion_model()
    batch_size = 64
    epochs = 20
    num_classes = 10
    fashion_drop_model = Sequential()
    fashion_drop_model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',padding='same',input_shape=(28,28,1)))
    fashion_drop_model.add(LeakyReLU(alpha=0.1))
    fashion_drop_model.add(MaxPooling2D((2, 2),padding='same'))
    fashion_drop_model.add(Dropout(0.25))

    fashion_drop_model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
    fashion_drop_model.add(LeakyReLU(alpha=0.1))
    fashion_drop_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
    fashion_drop_model.add(Dropout(0.25))

    fashion_drop_model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
    fashion_drop_model.add(LeakyReLU(alpha=0.1))
    fashion_drop_model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
    fashion_drop_model.add(Dropout(0.4))

    fashion_drop_model.add(Flatten())
    fashion_drop_model.add(Dense(128, activation='linear'))
    fashion_drop_model.add(LeakyReLU(alpha=0.1))
    fashion_drop_model.add(Dropout(0.3))
    fashion_drop_model.add(Dense(num_classes, activation='softmax'))

    return fashion_drop_model.summary()

fashion_model()

我得到的错误是:UnboundLocalError: local variable 'a' referenced before assignment
PS:在对代码逐行进行简短演练后,我认为错误在第 8 行( fashion_drop_model.add(Dropout(0.25)) )中蔓延

最佳答案

您的 Python 函数定义中缺少一个冒号:

def fashion_model(): #<--

完成此操作后,代码应该会运行。在 Google Colaboratory 中运行它,您会看到生成了模型摘要:

python - 如何在 CNN 中添加 Dropout-LMLPHP

笔记

强烈建议不要在卷积层之后使用 Dropout 层。卷积层的重点是利用空间邻域内的像素来提取正确的特征以输入密集层。 Dropout 会破坏这种关系,从而阻止您的模型成功学习这些特征。

有关更多详细信息,请参阅 Reddit 上的此讨论:https://www.reddit.com/r/MachineLearning/comments/42nnpe/why_do_i_never_see_dropout_applied_in/

关于python - 如何在 CNN 中添加 Dropout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56439066/

10-12 18:59