简体   繁体   English

如何使用keras序列模型使用gridsearchCV调整l2正则化器

[英]How to tune l2 regularizer using gridsearchCV using keras sequential model

I'm trying to tune the hyperparameter, kernel_regularizer, using gridsearchCV but gridsearchCV keeps telling me that the parameter names I'm entering for kernel_regularizer aren't real parameters 我正在尝试使用gridsearchCV调整超参数kernel_regularizer,但gridsearchCV一直告诉我,我为kernel_regularizer输入的参数名称不是真正的参数

I've tried various parameter names such as l2, kernel_regularizer, kernel, regularizers.l2, regularizers.l2( ) but none have worked. 我已经尝试了各种参数名称,如l2,kernel_regularizer,kernel,regularizers.l2,regularizers.l2(),但没有一个有效。

I've also looked online but can't seem to find any documentation of this issue 我也看过网上但似乎找不到任何关于这个问题的文档

My sequential model uses kernel_regularizer=l2(0.01) 我的顺序模型使用kernel_regularizer = l2(0.01)

param_grid = {'kernel_regularizer': [0.01,0.02,0.03]}

grid = GridSearchCV(...)
grid.fit(x_train, y_train) #this is where I get the error: 
                           #ValueError: kernel is not a legal parameter

You have to wrap your model using KerasClassifier for sklearn GridSearchCV to work. 您必须使用KerasClassifier为您的模型包装sklearn GridSearchCV才能工作。

def get_model(k_reg):
    model = Sequential()
    model.add(Dense(1,activation='sigmoid', kernel_regularizer=k_reg))
    model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy'])
    return model

param_grid = {
    'k_reg': [ regularizers.l2(0.01), regularizers.l2(0.001), regularizers.l2(0.0001)]
}

my_classifier = KerasClassifier(get_model, batch_size=32)
grid = GridSearchCV(my_classifier, param_grid)

grid.fit(np.random.rand(10,1),np.random.rand(10,1))

Keras Doc , has a detailed example. Keras Doc ,有一个详细的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM