我想最大化一个功能:func(minCount, wordNgrams, lr, epoch, loss)
仅在这些值上使用GridSearch:
`{'minCount': [2, 3],
'wordNgrams': [1, 2, 3, 4, 5],
'lr': [0.1, 0.01, 0.001, 0.0001],
'epoch': [5, 10, 15, 20, 25, 30],
'loss': [hs, ns, softmax]}`
我读过关于sklearn.model_selection.GridSearchCV(estimator, param_grid, ...)
但是,我不知道该放在哪里func(minCount, wordNgrams, lr, epoch, loss)
顺便说一句,我已经阅读了贝叶斯优化(https://github.com/fmfn/BayesianOptimization),但是对如何与string
and int
参数一起使用却一无所知。
根据文档,您有两种解决方案:
def my_scoring_function(func_outputs):
"""
process the outputs of func and return a score.
if func already reutrns the value you want to minimize,
my_scoring_function will be the identity function.
score is the value to optimize
"""
return score
cv = GridSearchCV(estimator=func, param_grid=my_param_grid, scoring=my_scoring_function)
关于贝叶斯优化,如果您的问题满足以下条件,这将很有趣:
非常感谢你!似乎有效,因为没有例外。新问题是如何从中检索最佳参数和最大化的函数值。
'GridSearchCV'对象没有属性'best_estimator_',我不知道,fit(??)应该是什么
确实,您需要先调用fit()方法,然后才能检索best_estimator_属性。拟合方法是触发计算的一种方法。如果您查看文档,fit()方法将“类似数组的形状(n_samples,n_features)”作为输入X。这是您将用于评估功能/训练算法的数据。如果您有目标值(监督学习),则可以将它们作为y传递。如果您不这样做,则只需传递y = None,GridSearchCV将寻求使得分最大化(即得分函数的输出)