0

我正在尝试使用 GridSearchCV 在 CNN 中找到最佳参数,但是当我尝试找到学习率和批量大小的最佳组合时,代码不起作用(如果我使用 epochs 而不是学习率,它会起作用)。知道为什么它不起作用吗?

# NEURAL NETWORK
# ======================================================================================================================
# region Now we build and train the CNN=================================================================================
vgg16_model = keras.applications.vgg16.VGG16()  # We import VGG16 model to copy f rom it the structure


def create_model():
    model = Sequential()  # We create our model with Sequential
    for layer in vgg16_model.layers:  # For each layer of VGG16 we add the same layer to our model
        model.add(layer)

    model.layers.pop()  # We remove the last layer to change it to what we need
    for layers in model.layers:  # We make the layers comming from VGG16 not trainables
        layers.trainable = False

    model.add(Dense(2, activation='softmax'))  # We add the last layer to have only 2 outputs: Cracked, Uncracked
    opt = Adam(lr=lrn_rate)
    model.compile(optimizer = opt, loss='categorical_crossentropy', metrics=['accuracy'])

    return model


model = KerasClassifier(build_fn=create_model,epochs=2, verbose=0)


# ====================================================================================
# define the grid search parameters
batch_size = [16, 32]
# epochs = [2,5]
lr=[0.1,0.2]
param_grid = dict(batch_size=batch_size, learn_rate=lr)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1, cv=3)

X, Y = train_batches.next()    # Batch of images to be analyzed
#grid_result = grid.fit_generator(train_imgs, train_labels )

grid_result = grid.fit(X,Y)

我得到的错误是“ValueError: learn_rate is not a legal parameter”,但我这样做了,就像我发现的一个例子一样,它适用于时代,但不适用于学习率。

4

1 回答 1

0

尝试:def create_model(lrn_rate):

于 2021-02-08T15:56:16.823 回答