关于grid_search中param_grid可以选取哪些参数(以keras为例)

纵饮孤独 提交于 2020-01-30 19:32:48

关于grid_search中param_grid可以选取哪些参数(以keras为例)

最近在学习调参时看到了 grid_search (也就是网格搜索算法)

https://cloud.tencent.com/developer/article/1447855

细节可以见上面这篇文章
官方原话解释是

param_griddict or list of dictionaries
Dictionary with parameters names (string) as keys and lists of parameter settings to try as values, or a list of such dictionaries, in which case the grids spanned by each dictionary in the list are explored. This enables searching over any sequence of parameter settings.

param_grid中传入参数列表

找遍了官方地址也没有看到,找了很多例子有使用kernel、batchsize、epochs、optimizer的,后来在看到一篇文章时发现keras 官方地址给出了详细解释,英文原话如下
When using scikit-learn’s grid_search API, legal tunable parameters are those you could pass to sk_params, including fitting parameters. In other words, you could use grid_search to search for the best batch_size or epochs as well as the model parameters.
意思就是当你用grid_search时,可以使用的参数就是那些可以传入sk_params中的参数,而sk_params是什么呢

keras.wrappers.scikit_learn.KerasRegressor(build_fn=None, **sk_params)

sk_params

*sk_params takes both model parameters and fitting parameters. Legal model parameters are the arguments of build_fn. Note that like all other estimators in scikit-learn, build_fn should provide default values for its arguments, so that you could create the estimator without passing any values to sk_params.

sk_params could also accept parameters for calling fit, predict, predict_proba, and score methods (e.g., epochs, batch_size). fitting (predicting) parameters are selected in the following order:*
可以接受的参数就是用于构建网络模型和用于fit、predict、score等方法中使用的参数,比如epochs和batch_size

举例:

def create_model(optimizer='sgd'):
    model = models.Sequential()  #需要使用
    model.add(Conv2D(64, (3, 3), padding='same', input_shape=x_shape, kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.3))

    model.add(Conv2D(64, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())

    model.add(MaxPooling2D(pool_size=(2, 2)))

    # model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # #model.add(Dropout(0.4))
    #
    # model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())

    # model.add(MaxPooling2D(pool_size=(2, 2)))

    # model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # #model.add(Dropout(0.4))
    #
    # model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # #model.add(Dropout(0.4))
    #
    # model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # model.add(MaxPooling2D(pool_size=(2, 2)))


    model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    # model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    model.add(Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    # model.add(BatchNormalization())

    model.add(MaxPooling2D(pool_size=(2, 2)))


    model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    # model.add(BatchNormalization())
    model.add(Dropout(0.4))

    #model.add(Dropout(0.4))

    model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    # model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2,2)))

    # model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # model.add(Dropout(0.4))
    #
    # model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # #model.add(Dropout(0.4))
    #
    # model.add(Conv2D(256, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # model.add(BatchNormalization())
    # model.add(MaxPooling2D(pool_size=(2,2)))
    #
    model.add(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    model.add(Dropout(0.4))
    #
    model.add(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    model.add(Dropout(0.4))
    #
    # model.add(Conv2D(512, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
    # model.add(Activation('relu'))
    # # model.add(BatchNormalization())
    # model.add(MaxPooling2D(pool_size=(2,2)))

    # model.add(Dropout(0.4))
    model.add(Flatten())
    model.add(Dense(512,kernel_regularizer=regularizers.l2(0.0001)))
    model.add(Activation('relu'))

    # model.add(Dropout(0.4))
    #model.add(Flatten())
    model.add(Dense(512,kernel_regularizer=regularizers.l2(0.0001)))
    model.add(Activation('relu'))
    # model.add(BatchNormalization())

    # model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('sigmoid')) #对于二分类的话需要使用这个
    model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    return model

构建一个模型函数,这里要注意,你需要将你需要搜索的参数定义为函数的参数,否则之后 GridSearchCV 函数会报错,比如此处的 optimizer 便作为 creat_model 的参数传入函数

model = KerasClassifier(build_fn=create_model,)
batch_size = [1,10,32, 64]
epochs = [25, 40,60] #just get the param of what can use
optimizer = ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam'] #可以选择这个东西作为
param_grid = dict(batch_size=batch_size,epochs=epochs,optimizer=optimizer)
grid = GridSearchCV(estimator=model,param_grid=param_grid,n_jobs=1,scoring='accuracy')
x_train,X_test,y_train,y_test = train_test_split(data,label,random_state=10,test_size=0.25) #get the
grid_result = grid.fit(data,label)

由于 batch_size 与 epochs 均为 fit 方法的参数,于是不需要进行传入即可直接使用,这样一来你需要搜索的参数都可以通过函数传参来进行,其他的自己多试几次就能知道能不能使用了

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!