具有keras的卷积神经网络给出错误,UnboundLocalError:赋值之前引用了局部变量'a'

时间:2018-12-29 11:31:30

标签: python keras deep-learning

我在下面写代码,但在下面给出错误 “ UnboundLocalError:分配前引用了局部变量'a'” 每次我使用keras.layers.BatchNormalization()时,编程都会给我这个错误。我该怎么办?怎么了?

def make_CNN_model():

    model = Sequential()
    # input layer transformation (BatchNormalization + Dropout)
    model.add(layers.BatchNormalization(name='inputlayer',input_shape=(28,28,1)))
    model.add(layers.Dropout(name='Droupout_inputlayer',rates=0.3))

    # convolutional layer (Conv2D + MaxPooling2D + Flatten + Dropout)
    model.add(layers.Conv2D(filiters=32,activation='relu', name="Convoluationlayer_1",kernal_size=(3,3),border_mode='same'))
    model.add(layers.MaxPooling2D(name='MaxPooling_1'))
    model.add(layers.Flatten(name="Flaten_1"))
    model.add(layers.Dropout(rate=0.3))

    # fully connected layer (Dense + BatchNormalization + Activation + Dropout)
    model.add(layers.Dense(name="FullyConnectedLayer_1",units=50))
    model.add(layers.BatchNormalization())
    model.add(layers.Activation('relu'))
    model.add(layers.Dropout(rate=0.3))

    # output layer (Dense + BatchNormalization + Activation)
    model.add(layers.Dense(name = "Outputlayer", units=10))
    model.add(layers.BatchNormalization())
    model.add(layers.Activation('sigmod'))

    return model

model = make_CNN_model()
model.compile(
    optimizer='Adam',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)
summary = model.fit(
    X_train, y_train_onehot,
    batch_size=5000,
    epochs=5,
    validation_split=0.2,
    verbose=1,
    callbacks=[time_summary]
)

3 个答案:

答案 0 :(得分:0)

我可以在model.add(layers.Dropout(name='Droupout_inputlayer',rates=0.3))中看到一些非常明显的拼写错误,例如'rates'而不是'rate'。

然后,在model.add(layers.Conv2D(filiters=32,activation='relu', name="Convoluationlayer_1",kernal_size=(3,3),border_mode='same'))中使用“过滤”代替“过滤器”,使用“ kernal_size”代替“ kernel_size”。

最后,在model.add(layers.Activation('sigmod'))中使用'sigmod'而不是'sigmoid'。

我在您的代码中没有看到任何变量a,因此,如果您是我,请务必先解决您的拼写错误,因为它们可能会导致此问题。

答案 1 :(得分:0)

def make_CNN_model():

model = Sequential()
# input layer transformation (BatchNormalization + Dropout)
model.add(layers.BatchNormalization(name='inputlayer',input_shape=(28,28,1)))
model.add(layers.Dropout(name='Droupout_inputlayer',rate=0.3))

# convolutional layer (Conv2D + MaxPooling2D + Flatten + Dropout)
model.add(layers.Conv2D(filters=32,activation='relu', name="Convoluationlayer_1",kernel_size=(3,3),border_mode='same'))
model.add(layers.MaxPooling2D(name='MaxPooling_1'))
model.add(layers.Flatten(name="Flaten_1"))
model.add(layers.Dropout(rate=0.3))

# fully connected layer (Dense + BatchNormalization + Activation + Dropout)
model.add(layers.Dense(name="FullyConnectedLayer_1",units=50))
model.add(layers.BatchNormalization())
model.add(layers.Activation('relu'))
model.add(layers.Dropout(rate=0.3))

# output layer (Dense + BatchNormalization + Activation)
model.add(layers.Dense(name = "Outputlayer", units=10))
model.add(layers.BatchNormalization())
model.add(layers.Activation('sigmoid'))

return model

答案 2 :(得分:0)

我在终端上写了下面的代码,然后再次安装python 3,问题解决了。

$ conda install -c conda-forge tensorflow