我正在使用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))
)中逐渐蔓延
答案 0 :(得分:2)
您在Python函数定义中缺少冒号:
def fashion_model(): #<--
执行完此操作后,代码应运行。在Google Colaboratory中运行此程序,您会看到生成了模型摘要:
强烈建议在卷积层之后使用Dropout层。卷积层的全部目的是利用空间邻域内的像素来提取正确的特征以馈入密集层。辍学会破坏这种关系,从而使您的模型无法成功学习这些功能。
有关更多详细信息,请参见Reddit上的讨论:https://www.reddit.com/r/MachineLearning/comments/42nnpe/why_do_i_never_see_dropout_applied_in/