如何使用keras sequential mod使用gridsearchCV调整l2正则化器

2024-05-14 14:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用gridsearchCV来优化超参数kernel\u regularizer,但是gridsearchCV一直告诉我为kernel\u regularizer输入的参数名不是真正的参数

我试过各种各样的参数名,比如l2,kernel\u regularizer,kernel,regularizers.l2,regularizers.l2(),但是没有一个有效。你知道吗

我也在网上查过,但似乎找不到有关这个问题的任何文档

我的序列模型使用核正则化器=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

Tags: 文档模型参数paramistrain序列kernel
1条回答
网友
1楼 · 发布于 2024-05-14 14:35:06

您必须使用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,有一个详细的示例。你知道吗

相关问题 更多 >

    热门问题