1

cnn 建模时出错。使用 dropout 时,会出现以下错误消息。

这是错误信息


UnboundLocalError: local variable 'a' referenced before assignment

模型

def getModel(input_shape,filter_size=32,pool_size=(2,2),dropout=0.2): 

model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=input_shape, activation='elu', kernel_initializer="he_normal", padding='same', kernel_regularizer=regularizers.l2(0.01)))

我想在 maxpooling 之后使用 dropout

model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(dropout))

model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same',strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))

model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))

model.add(Conv2D(16, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))

model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))

model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))

这是平坦的区域

model.add(Flatten())
model.add(Dense(126, kernel_initializer="glorot_normal" ,kernel_regularizer=regularizers.l2(0.01)))
model.add(Activation('tanh'))
model.add(Dense(classes))
model.add(Activation('sigmoid'))

编译

model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',  #SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
              metrics=['accuracy'])
return model

模型拟合

np.random.seed(42)
hist = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs, verbose = 1, validation_split = .2)
4

1 回答 1

0

我无法弄清楚这里的“a”是什么,因此错误,但我认为以下代码应该有所帮助:

model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal",padding='same',strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(32, (2, 3), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(Conv2D(32, (2, 2), kernel_initializer="he_normal", padding='same', strides=1, kernel_regularizer=regularizers.l2(0.02)))
model.add(Activation('elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
于 2018-11-13T05:46:51.347 回答