避免 GridSearchCV 中的某些参数组合
Avoid certain parameter combinations in GridSearchCV
我正在使用 scikit-learn 的 GridSearchCV
迭代参数 space 来调整模型。具体来说,我用它来测试神经网络中的不同超参数。格子如下:
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 个单元。所有这些模型都是等价的,因为没有隐藏层。这是非常低效的,这意味着我需要编写更多代码来消除结果中的冗余。
有没有办法防止这种参数组合,也许是通过传递一个参数元组?
sklearn documentation建议两个参数网格。
所以你可以这样做:
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']}
]
GridSearchCV 允许您将字典列表传递给参数:
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.
因此您可以将这些词典指定为您的原始词典的某些子词典。因此,您可以避免不相关的组合。
我正在使用 scikit-learn 的 GridSearchCV
迭代参数 space 来调整模型。具体来说,我用它来测试神经网络中的不同超参数。格子如下:
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 个单元。所有这些模型都是等价的,因为没有隐藏层。这是非常低效的,这意味着我需要编写更多代码来消除结果中的冗余。
有没有办法防止这种参数组合,也许是通过传递一个参数元组?
sklearn documentation建议两个参数网格。
所以你可以这样做:
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']}
]
GridSearchCV 允许您将字典列表传递给参数:
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.
因此您可以将这些词典指定为您的原始词典的某些子词典。因此,您可以避免不相关的组合。