我正在尝试GridsearchCV
,但我想在param grid
. 这是我的网格搜索代码:
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasClassifier
def create_model(input_dim=25, activation='relu', units=100, optimizer = 'adam', init='he_normal', dropout_rate=0.33):
model = Sequential()
model.add(Dense(input_dim=input_dim,
units=units,
kernel_initializer=init,
activation=activation))
model.add(Dropout(dropout_rate))
model.add(Dense(1, kernel_initializer=init, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
return model
model = KerasClassifier(build_fn=create_model, epochs=10, batch_size=64, verbose=1)
#lr = [0.01, 0.001, 0.0001] # [x/100 for x in range(1, 10)] #learning rate for optimizer
units = [int(train_X.shape[1]/2), train_X.shape[1], train_X.shape[1]*2, train_X.shape[1]*3]
batch_size = [32, 64, 128, 256]
optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam']
epochs = [50, 100, 200]
init = ['uniform', 'lecun_uniform', 'normal', 'zero', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']
activation = ['softmax', 'softplus', 'softsign', 'relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
dropout_rate = [0.2, 0.3, 0.4, 0.5]
cv = [(slice(None), slice(None))]
param_grid = dict(units=units, batch_size=batch_size, optimizer=optimizer, epochs=epochs,
init=init, activation=activation, dropout_rate=dropout_rate)
grid = GridSearchCV(cv=cv, estimator=model, param_grid=param_grid, n_jobs=1)
grid_result = grid.fit(train_X, train_y, validation_data=(valid_X, valid_y))
当我运行他的代码时,即使KerasClassifier
有参数epochs=10
,网格也不会运行 10 个 epoch,而是运行[50, 100, 200]
我在网格中提供的。就像epochs=10
被覆盖了一样。
现在我想做的是,我想activation functions
在第一层使用不同的,但保留Sigmoid
在输出层。我担心的是,参数会被来自网格的activation='sigmoid'
参数覆盖吗?activation = ['softmax', 'softplus', 'softsign', 'relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear']
我希望这对你更有意义。