避免在GridSearchCV中使用某些参数组合 [英] Avoid certain parameter combinations in GridSearchCV
问题描述
我正在使用scikit-learn的GridSearchCV
遍历参数空间来调整模型.具体来说,我正在使用它来测试神经网络中的不同超参数.网格如下:
I'm using scikit-learn's GridSearchCV
to iterate over a parameter space to tune a model. Specifically, I'm using it to test different hyperparameters in a neural network. The grid is as follows:
params = {'num_hidden_layers': [0,1,2],
'hidden_layer_size': [64,128,256],
'activation': ['sigmoid', 'relu', 'tanh']}
问题是当隐藏的num_hidden_layers
设置为0
时,我最终会运行冗余模型.它将运行一个模型,该模型具有0个隐藏层和64个单位,另一个具有128个单位,另一个具有256个单位.所有这些模型都是等效的,因为没有隐藏层.这是非常低效的,这意味着我需要编写更多代码以消除结果中的冗余.
The problem is that I end up running redundant models when hidden num_hidden_layers
is set to 0
. It will run a model with 0 hidden layers and 64 units, another with 128 units, and another with 256 units. All of these models are equivalent since there is no hidden layer. This is highly inefficient and it means I need to write more code to remove redundancy in the results.
是否可以通过传递参数元组来防止这种参数组合?
Is there a way to prevent such parameter combinations, perhaps by passing a tuple of parameters?
推荐答案
GridSearchCV allows you to pass list of dictionaries to params:
param_grid:字典或词典列表
param_grid : dict 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.
因此,您可以将这些词典指定为原始词典的某些词典.因此,您可以避免不相关的组合.
So you can specify these dictionaries to be certain subdictionaries of your original dictionary. Thus, you could avoid irrelevant combinations.
这篇关于避免在GridSearchCV中使用某些参数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!