我正在尝试使用GridSearchCV在CNN中找到最佳参数,但是当我尝试找到学习率和批处理大小的最佳组合时,代码将无法正常工作(如果我使用学习率而不是学习率,时代)。知道为什么它不起作用吗?
# 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不是合法参数”,但正如我在示例中发现的那样,它适用于时代,但不适用于学习率。
答案 0 :(得分:0)
试试: def create_model(lrn_rate):