NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Optimizing for Generalization in Machine Learning with Cross-Validation\n Gradients

Shane Barratt, Rishi Sharma · DOI

Abstract

Cross-validation is the workhorse of modern applied statistics and machine learning, as it provides a principled framework for selecting the model that maximizes generalization performance. In this paper, we show that the cross-validation risk is differentiable with respect to the hyperparameters and training data for many common machine learning algorithms, including logistic regression, elastic-net regression, and support vector machines. Leveraging this property of differentiability, we propose a cross-validation gradient method (CVGM) for hyperparameter optimization. Our method enables efficient optimization in high-dimensional hyperparameter spaces of the cross-validation risk, the best surrogate of the true generalization ability of our learning algorithm.

Introduction

In supervised machine learning, the primary objective is generalization—the ability of a model to perform accurately on unseen data. Since future data is unavailable during training, cross-validation (CV) serves as a critical proxy for estimating generalization error. Traditionally, tuning hyperparameters, such as the regularization strength in Lasso or kernel parameters in SVMs, involves exhaustive "guess-and-check" strategies like Grid Search or Random Search. However, these methods suffer from the "curse of dimensionality," where the search space grows exponentially with the number of hyperparameters. This paper introduces a method to calculate exactly how infinitesimal changes in hyperparameters affect the cross-validation error, transforming CV from a black-box metric into a differentiable objective.

The Cross-Validation Gradient Method (CVGM)

The core innovation of this work is the Cross-Validation Gradient Method (CVGM). By recognizing that optimal model parameters are differentiable functions of hyperparameters in many common algorithms, the authors leverage implicit differentiation to navigate the hyperparameter space efficiently.

Figure 1: Visualization of model fits across different cross-validation folds during the optimization process.

The CVGM process involves:

  1. Implicit Differentiation: For models solved via convex cost functions (e.g., Logistic Regression), the Karush-Kuhn-Tucker (KKT) optimality conditions are used to derive the gradient of model weights with respect to hyperparameters.
  2. K-Fold Integration: The dataset is split into $K$ folds. For each fold, the model is trained, and the gradient of the validation loss relative to the hyperparameters is calculated using the chain rule.
  3. Gradient Descent Optimization: These gradients are averaged across folds, allowing hyperparameters to be updated via standard gradient descent. This enables the simultaneous tuning of hundreds of hyperparameters, a feat unattainable with traditional grid search.

Experimental Evaluation

The authors evaluated CVGM using several synthetic datasets to establish ground truth performance.

Synthetic Classification: The "Rings" Dataset

A key test involved a 2D dataset with non-linearly separable concentric rings. CVGM was used to optimize a 322-parameter neural network that acted as a feature transformer (kernel) for a logistic regression model.

Figure 3: Top: Decision boundary for the non-linearly separable "rings" dataset optimized via CVGM. Bottom: Corresponding feature space transformations.

The method achieved an accuracy of 89.8%, nearly reaching the theoretical Bayes optimal accuracy of 89.4%. This demonstrates CVGM's ability to perform high-dimensional "meta-learning" effectively.

Regression and Efficiency

In Elastic-net regression experiments on small datasets ($N=30$), CVGM achieved a test loss of 1.080, outperforming Exhaustive Search (1.207) and matching or exceeding Random Search efficiency. The method proved particularly robust in low-data regimes where standard deep learning models often overfit.

Discussion and Future Work

CVGM demonstrates that cross-validation is not a black box but a smooth surface amenable to calculus-based optimization. By making the "outer loop" of hyperparameter selection as efficient as the "inner loop" of model training, it bridges the gap between simple linear models and complex feature learning.

However, the method currently requires the underlying learning algorithm to be a convex optimization problem, limiting its direct application to the internal layers of standard non-convex deep neural networks. Additionally, calculating gradients involves the Hessian matrix, which can be computationally intensive for very large-scale models. Future research will focus on scaling these techniques to larger, real-world datasets and exploring applications in non-convex settings.

Summary updated

BibTeX
@misc{barratt2018optimizing,
  author = {Shane Barratt and Rishi Sharma},
  title = {{Optimizing for Generalization in Machine Learning with Cross-Validation\textbackslash\{\}n Gradients}},
  year = {2018},
  eprint = {1805.07072},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1805.07072},
}
Figure