# 网格搜索

网格搜索是一种超参数调优方法：它将每个超参数的取值范围离散化，在所有取值组合上训练并评估模型，返回交叉验证得分最高的那个组合。它是超参数优化（HPO）家族中穷举式的全因子成员：与[随机搜索](https://www.edgepedia.org/zh/random-search)或贝叶斯搜索不同，它不会漏掉预定义网格中的任何部分。拟合一次网格搜索，既能得到一个最佳设置，也能得到整个网格上完整的得分记录，这使它成为机器学习工作流中的标准基线和最终精调工具。

| 关键事实 | 详情 |
|---|---|
| 产出什么 | 一个最佳参数设置（best_params_）、其平均交叉验证得分（best_score_），以及所有组合的完整得分记录 cv_results_ <sup>[1](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV)</sup> |
| 试验次数 | 若有 K 个超参数、每个超参数有取值集合 L，则试验次数为这 K 个集合的乘积，随超参数数量呈指数增长 <sup>[2](https://www.jmlr.org/papers/v13/bergstra12a.html)</sup> |
| 网格规模 | 若 d 为最大离散化域的基数、m 为超参数个数，则网格有 \( O(d^{m}) \) 种配置，即便 m 的大小中等也不切实际 <sup>[3](https://arxiv.org/html/2410.22854)</sup> |
| 典型默认值（scikit-learn） | 5 折交叉验证（分类目标用 StratifiedKFold）；默认使用估计器自带的 score 方法，具体随估计器而异，除非设置了 scoring 参数 <sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup> |
| 主要批评 | 伯格斯特拉与本吉奥从经验和理论上证明，随机选取的试验比网格试验更高效，因为网格搜索在不重要的维度上分配了过多试验 <sup>[2](https://www.jmlr.org/papers/v13/bergstra12a.html)</sup> |
| 快速变体 | 逐次减半类方法（HalvingGridSearchCV）先用少量资源评估所有候选，之后每轮将存活候选的资源乘以一个因子，通常为 3 <sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup> |

## 工作原理

网格搜索是对超参数的全因子设计。用户为每个超参数指定一个有限取值集合，方法对这些集合的[笛卡尔积](https://www.edgepedia.org/zh/cartesian-product)进行评估：每一种可能的组合都会被训练并评分。<sup>[5](https://aiche.onlinelibrary.wiley.com/doi/10.1002/widm.1484)</sup> 数值型和整型取值通常在其箱约束内等距分布，每个超参数不同取值的个数称为网格的分辨率。<sup>[5](https://aiche.onlinelibrary.wiley.com/doi/10.1002/widm.1484)</sup>

代价直接来自枚举。若每个超参数 k 有取值集合 L，则试验次数为

\[ S = \prod_{k=1}^{K} \left| L^{(k)} \right| \]

这是 K 个集合的乘积，随超参数数量呈指数增长，即维数灾难。<sup>[2](https://www.jmlr.org/papers/v13/bergstra12a.html)</sup> 等价地，若 d 为最大离散化域、m 为超参数个数，则网格规模为 \( O(d^{m}) \)。<sup>[3](https://arxiv.org/html/2410.22854)</sup>

每个组合都通过交叉验证评分，被选中的参数是在留出数据上使得分最大化的那些。<sup>[1](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV)</sup> 因此输出既是一个单一最佳设置，也是网格上的一个得分曲面，按组合逐一存储。<sup>[1](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV)</sup>

## 操作步骤

一次 scikit-learn 网格搜索由五个部分组成：一个估计器、一个参数空间、一种搜索或采样候选的方法、一套交叉验证方案，以及一个评分函数。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup> 实践中：

1. 将网格定义为字典或字典列表，把参数名映射到设置列表；字典列表可跨越多个网格。<sup>[1](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV)</sup> 学习率这类连续超参数，宜在对数尺度而非线性尺度上离散化。<sup>[3](https://arxiv.org/html/2410.22854)</sup>
2. 选择交叉验证方案。默认为 5 折（0.22 版起由 3 折改为 5 折），二分类或多分类目标用 StratifiedKFold，其余情况用 KFold。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup>
3. 选择评分指标。默认情况下（scoring=None）使用估计器自带的 score 方法，对许多分类器是准确率、对许多回归器是 R²，但随估计器而异；替代方案通过 scoring 参数设置。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup>
4. 拟合。所有可能的组合都会被评估，保留最佳组合。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup> 当 refit=True（默认）时，会用找到的最佳参数在整个数据集上重新拟合一个估计器，并以 best_estimator_ 的形式提供，可直接用于预测。<sup>[1](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV)</sup>
5. 并行与验证。计算可用 n_jobs=-1 并行运行，所得模型应在搜索期间未见的留出样本上评估。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup>

## 起源

其主要替代方案随机搜索，早在 20 世纪 50 年代就作为全局优化方法被考虑过。<sup>[3](https://arxiv.org/html/2410.22854)</sup>

在伯格斯特拉与本吉奥 2012 年的研究重塑该领域之前，网格搜索一直是机器学习中的传统默认做法：该研究从经验和理论上表明，随机选取的试验在超参数优化上比网格上的试验更高效，并且网格搜索不适合在新数据集上配置算法，因为不同数据集上重要的超参数不同。<sup>[2](https://www.jmlr.org/papers/v13/bergstra12a.html)</sup> 后来的一篇综述将这项工作视为催生自动化 HPO 正式研究的契机。<sup>[6](https://www.jiclt.com/article/a-comparative-study-of-hyperparameter-optimization-techniques-for-machine-learning-models-469/)</sup>

## 变体

**随机化搜索。** RandomizedSearchCV 从参数空间上的分布中抽取固定数量的候选设置，而非枚举所有组合。它的两大好处是：预算可以独立于参数个数和可能取值数来选择；加入不影响性能的参数不会降低效率；n_iter（默认 10）在运行时间与解的质量之间权衡。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup> 用于超参数优化的随机搜索由詹姆斯·伯格斯特拉与[约书亚·本吉奥](https://www.edgepedia.org/zh/yoshua-bengio)研究并推广（《机器学习研究杂志》，2012 年）。<sup>[2](https://www.jmlr.org/papers/v13/bergstra12a.html)</sup>

**逐次减半。** 逐次减半算法来自多臂老虎机文献，佐哈尔·卡尔宁、托默·科伦与奥伦·索梅赫于 2013 年提出，凯文·贾米森与阿米特·塔尔沃卡于 2015 年将其用于超参数优化。<sup>[7](https://doi.org/10.48550/arxiv.1502.07943)</sup> 它运行一场锦标赛：所有候选先用少量资源评估，只有一部分带着更多资源进入下一轮，每轮中每个候选的资源乘以因子，同时候选数除以同一因子；因子取 3 通常效果良好。资源通常是训练样本数，但也可以是 n_estimators 之类的任意数值参数，其调度为 \( n_{\mathrm{resources}\_i} = \mathrm{factor}^{i} \cdot \mathrm{min\_resources} \) 与 \( n_{\mathrm{candidates}\_i} = n_{\mathrm{candidates}} / (\mathrm{factor}^{i}) \)。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup> scikit-learn 以仍属实验性的 HalvingGridSearchCV 与 HalvingRandomSearchCV 提供这些方法，从 sklearn.model_selection 导入前需要先执行 'from sklearn.experimental import enable_halving_search_cv'，它们在找到好的参数组合上可能快得多。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup>

**Hyperband 与 BOHB。** Hyperband 由李丽莎及其同事提出（《机器学习研究杂志》，2018 年），它扩展了逐次减半，通过在初始候选数不同的多个分组上运行逐次减半，来解决其 \( n \) 与 \( B/n \) 之间的权衡。<sup>[8](https://jmlr.csail.mit.edu/papers/volume18/16-558.html)</sup> BOHB 由斯特凡·法尔克纳、亚伦·克莱因与弗兰克·胡特提出（arXiv，2018 年），将[贝叶斯优化](https://www.edgepedia.org/zh/bayesian-optimization)与 Hyperband 结合。<sup>[9](https://doi.org/10.48550/arxiv.1807.01774)</sup>

**由粗到细与旋转网格。** 多尺度由粗到细方法先应用粗网格确定一个好区域，再在该区域上应用更细的网格，部分缓解了指数代价。<sup>[10](https://www.aimspress.com/aimspress-data/mbe/2024/6/PDF/mbe-21-06-275.pdf)</sup> 旋转网格搜索保留确定性采样，但旋转网格，使各点沿每一维度投影到不同的值上。<sup>[11](https://www.ijml.org/vol12/1110-IJMLC-5378.pdf)</sup>

## 应用

网格搜索是机器学习工作流中的标准基线和最终精调工具。2025 年的一项综述将网格搜索与随机搜索列为最常用的两种配置选择方法，其中网格搜索更适合低维问题。<sup>[12](https://arxiv.org/pdf/2504.06207v1.pdf)</sup> 因此它被用于小搜索空间、在有望区域周围做最终网格精调，以及作为更新型 HPO 方法的基准参照点。<sup>[6](https://www.jiclt.com/article/a-comparative-study-of-hyperparameter-optimization-techniques-for-machine-learning-models-469/)</sup>

## 局限与替代方法

**指数代价。** 对一个 10 维、每维 5 个取值的空间，网格搜索需要 \( 5^{10} \) 次评估，这使其只能用于低维问题或最终精调。<sup>[6](https://www.jiclt.com/article/a-comparative-study-of-hyperparameter-optimization-techniques-for-machine-learning-models-469/)</sup>

**在无关维度上浪费试验。** 若 10 个超参数中只有 2 个实质性影响性能，网格搜索仍会穷举评估其余 8 个的所有组合。<sup>[6](https://www.jiclt.com/article/a-comparative-study-of-hyperparameter-optimization-techniques-for-machine-learning-models-469/)</sup> 这源于大多数 HPO 问题的低有效维数，随机搜索利用了这一点，网格搜索则没有。<sup>[5](https://aiche.onlinelibrary.wiley.com/doi/10.1002/widm.1484)</sup>

**分辨率固定。** 网格分辨率必须事先指定，事后细化它比向随机搜索中添加样本更复杂。<sup>[5](https://aiche.onlinelibrary.wiley.com/doi/10.1002/widm.1484)</sup>

**对验证数据的过拟合。** 针对用于选择的同一批数据调参有过度拟合的风险，因为关于评估数据的知识可能泄漏进模型；scikit-learn 建议为搜索准备一个开发集、为最终指标准备一个独立的评估集。<sup>[4](https://sklearn.org/stable/modules/grid_search.html)</sup>

**轴向对齐。** 当组合许多维度时，网格沿每一单独维度只探测少数不同值，这在响应各向异性（即沿不同轴尺度不同）时有害。<sup>[11](https://www.ijml.org/vol12/1110-IJMLC-5378.pdf)</sup>

**网格搜索与随机搜索。** 伯格斯特拉与本吉奥发现，在 32 维配置空间上纯随机搜索，在七个数据集中的四个上媲美手工加网格配置的[深度信念网络](https://www.edgepedia.org/zh/deep-belief-network)，并在一个上胜出。<sup>[2](https://www.jmlr.org/papers/v13/bergstra12a.html)</sup> 一个实例把浪费讲得很具体：两个超参数上分辨率为 3 的网格产生 9 种配置，但若只有一个参数重要，9 次评估中只有 3 次提供有意义的信息，而随机搜索给出该参数的 9 个不同值。<sup>[5](https://aiche.onlinelibrary.wiley.com/doi/10.1002/widm.1484)</sup> 随机搜索在适当的采样与正则性假设下收敛，而逐步细化的网格搜索在连续目标、紧致搜索域上、当网格变得稠密时同样收敛。<sup>[3](https://arxiv.org/html/2410.22854)</sup>

**网格搜索与贝叶斯优化。** 在 MRBI 数据集上，詹姆斯·伯格斯特拉及其同事提出的树结构 Parzen 估计器（NeurIPS，2011 年）在 200 次试验下得到 44% 的误差，随机搜索则为 50%。<sup>[13](https://proceedings.neurips.cc/paper_files/paper/2011/file/86e8f7ab32cfd12577bc2619bc635690-Paper.pdf)</sup>

没有哪种方法普遍占优。对于评估代价高昂的低维问题，基于 GP 的贝叶斯优化仍是参考方法，而网格搜索适合小搜索空间和最终网格精调。<sup>[6](https://www.jiclt.com/article/a-comparative-study-of-hyperparameter-optimization-techniques-for-machine-learning-models-469/)</sup> 领域内逐渐形成共识：随机搜索应被视为 HPO 的基线方法，网格搜索则保留为适用于小空间与精调的经典技术。<sup>[3](https://arxiv.org/html/2410.22854)</sup>

## References

1. [GridSearchCV, scikit-learn 1.9.0 documentation (API)](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV)
2. [Random Search for Hyper-Parameter Optimization (Bergstra & Bengio, JMLR 13(10):281-305, 2012)](https://www.jmlr.org/papers/v13/bergstra12a.html)
3. [Hyperparameter Optimization in Machine Learning (Feurer & Hutter monograph, 2024)](https://arxiv.org/html/2410.22854)
4. [3.2. Tuning the hyper-parameters of an estimator, scikit-learn 1.9.0 documentation](https://sklearn.org/stable/modules/grid_search.html)
5. [Hyperparameter optimization: Foundations, algorithms, best practices, and open challenges (Bischl et al., 2023, WIREs Data Mining and Knowledge Discovery)](https://aiche.onlinelibrary.wiley.com/doi/10.1002/widm.1484)
6. [A Comparative Study of Hyperparameter Optimization Techniques for Machine Learning Models (JICLT)](https://www.jiclt.com/article/a-comparative-study-of-hyperparameter-optimization-techniques-for-machine-learning-models-469/)
7. [Jamieson, Kevin, Talwalkar, Ameet (2015). Non-stochastic Best Arm Identification and Hyperparameter Optimization. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1502.07943)
8. [Hyperband: A Novel Bandit-Based Approach to Hyperparameter Optimization (Li, Jamieson, DeSalvo, Rostamizadeh, Talwalkar; JMLR 18(185):1-52, 2018)](https://jmlr.csail.mit.edu/papers/volume18/16-558.html)
9. [Falkner, Stefan, Klein, Aaron, Hutter, Frank (2018). BOHB: Robust and Efficient Hyperparameter Optimization at Scale. arXiv (Cornell University).](https://doi.org/10.48550/arxiv.1807.01774)
10. [Hyperparameter optimization: Classics, acceleration, online, multi-objective, and tools (Mathematical Biosciences and Engineering, 2024)](https://www.aimspress.com/aimspress-data/mbe/2024/6/PDF/mbe-21-06-275.pdf)
11. [Rotated Grid Search for Hyperparameter Optimization (International Journal of Machine Learning and Computing)](https://www.ijml.org/vol12/1110-IJMLC-5378.pdf)
12. [An experimental survey and Perspective View on Meta-Learning for Automated Algorithms Selection and Parametrization (2025)](https://arxiv.org/pdf/2504.06207v1.pdf)
13. [Algorithms for Hyper-Parameter Optimization (Bergstra, Bardenet, Bengio, Kégl, NeurIPS 2011)](https://proceedings.neurips.cc/paper_files/paper/2011/file/86e8f7ab32cfd12577bc2619bc635690-Paper.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026; Oct 21, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Machine learning methods › Ensemble, boosting, and transfer methods › Model selection, hyperparameter tuning, and validation

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/grid-search

English original: https://www.edgepedia.org/grid-search
