Python 避免GridSearchCV中的某些参数组合
我正在使用scikit learn的Python 避免GridSearchCV中的某些参数组合,python,machine-learning,parameters,scikit-learn,grid-search,Python,Machine Learning,Parameters,Scikit Learn,Grid Search,我正在使用scikit learn的GridSearchCV在参数空间上迭代以优化模型。具体来说,我用它来测试神经网络中的不同超参数。网格如下所示: params = {'num_hidden_layers': [0,1,2], 'hidden_layer_size': [64,128,256], 'activation': ['sigmoid', 'relu', 'tanh']} 问题是,当hiddennum\u hidden\u layers设置为0
GridSearchCV
在参数空间上迭代以优化模型。具体来说,我用它来测试神经网络中的不同超参数。网格如下所示:
params = {'num_hidden_layers': [0,1,2],
'hidden_layer_size': [64,128,256],
'activation': ['sigmoid', 'relu', 'tanh']}
问题是,当hiddennum\u hidden\u layers
设置为0
时,我会运行冗余模型。它将运行一个包含0个隐藏层和64个单元的模型,另一个包含128个单元,另一个包含256个单元。所有这些模型都是等效的,因为没有隐藏层。这是非常低效的,这意味着我需要编写更多的代码来消除结果中的冗余
有没有办法防止这种参数组合,也许是通过传递一组参数?建议使用两个参数网格
所以你可以这样做:
param_grid = [
{'num_hidden_layers': [1,2],
'hidden_layer_size': [64,128,256],
'activation': ['sigmoid', 'relu', 'tanh']},
{'num_hidden_layers': [0],
'hidden_layer_size': [64],
'activation': ['sigmoid', 'relu', 'tanh']}
]
允许您将字典列表传递给参数:
参数网格:dict或字典列表
以参数名称(字符串)作为键和参数列表的字典
要尝试作为值的参数设置,或此类字典的列表,
在这种情况下,列表中每个字典跨越的网格是
探索。这允许搜索任何参数序列
设置
因此,您可以将这些词典指定为原始词典的某些子词典。因此,您可以避免不相关的组合。我应该补充一点,这个问题与这里提到的不同,用户希望跳过会产生错误的“禁止”组合: