Reputation: 31
I am trying to test my model by implementing GridSearchCV. But I cannot seem to add learning rate and momentum as parameters in GridSearch. Whenever I try to execute the code by adding those, I am getting an error.
Here is the model I've created:
def define_model(optimizers="SGD"):
model = models.Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(32, 32, 3)))
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer=optimizers, metrics='accuracy')
return model
GridSearch that I've implemented:
learn_rate=(0.0001,0.001)
momentum = (0.1, 0.5)
epochs = [5]
batches = [16]
model = KerasClassifier(build_fn=define_model, verbose=2)
param_grid = dict(epochs = epochs, lr = learn_rate, momentum = momentum, batch_size = batches)
grid = GridSearchCV(estimator=model, param_grid= param_grid, n_jobs = 1, cv = 3)
grid_result = grid.fit(trainX, trainY)
print("Best: %f using %s" %(grid_result.best_score_, grid_result.best_params_))
And here is the error I've encountered:
~\anaconda3\envs\tf-gpu\lib\site-packages\tensorflow\python\keras\wrappers\scikit_learn.py in check_params(self, params)
104 else:
105 if params_name != 'nb_epoch':
--> 106 raise ValueError('{} is not a legal parameter'.format(params_name))
107
108 def get_params(self, **params): # pylint: disable=unused-argument
ValueError: lr is not a legal parameter
Upvotes: 3
Views: 3974
Reputation: 511
You don't have optimizer in params. So, you don't need to put it as an argument in your function. Instead, you may need to mention the learning_rate and momentum as arguments in your function and add SGD directly where it should be:
def define_model(lr, momentum):
model = models.Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(32, 32, 3)))
model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer=SGD(lr, momentum), metrics='accuracy')
return model
Upvotes: 1