Hyperparameter Selection in Machine Learning
How would you choose hyperparameters in a machine learning model? Walk through the main strategies, their trade-offs, and practical guidelines for when to use each.
Hints
- Think of hyperparameter tuning as a search problem. The key trade-off is cost per evaluation vs. the dimensionality of the search space -- this determines which search strategy is appropriate.
- Random search dominates grid search when only a few hyperparameters matter, because grid search wastes evaluations on irrelevant dimensions. For expensive evaluations, Bayesian optimization is more sample-efficient than either.
- Evaluation hygiene is as important as the search algorithm: always use cross-validation (walk-forward for time series) and a strictly held-out test set. Tuning on the test set invalidates your performance estimates.
Worked Solution
How to Think About It: Hyperparameter selection is fundamentally an optimization problem with a noisy, expensive objective (model performance on held-out data). The core tension is efficiency vs. thoroughness: you want to find a good setting without wasting compute. In practice, most practitioners follow a two-stage approach -- a cheap, broad search to find promising regions, followed by a more targeted refinement. The other critical discipline is evaluation hygiene: your hyperparameter search must not touch the test set, or your reported performance is meaningless.
Key Insight: Hyperparameter tuning is itself a search over a space. The right algorithm for that search depends on the dimensionality and cost of evaluation -- not on personal preference.
The Method:
1. Grid Search - Define a discrete grid of candidate values for each hyperparameter. - Train and evaluate the model at every combination on a validation set. - Choose the combination with the best validation score. - Cost: $O(n^k)$ evaluations for $k$ hyperparameters with $n$ values each. - When to use: small search spaces (1-2 hyperparameters, few candidate values). Exhaustive but impractical for $k \geq 3$.
2. Random Search - Sample hyperparameter combinations randomly from specified distributions (e.g., log-uniform for learning rate). - Evaluate each sample on the validation set. - When to use: medium-dimensional search spaces. Bergstra and Bengio (2012) showed that random search outperforms grid search when only a few hyperparameters matter -- grid search wastes evaluations on irrelevant dimensions.
3. Bayesian Optimization - Maintain a probabilistic surrogate model (typically a Gaussian Process) of the objective function. - Use an acquisition function (Expected Improvement, Upper Confidence Bound) to select the next point to evaluate. - Update the surrogate after each evaluation. - When to use: expensive model evaluations (deep learning, large datasets), moderate-dimensional search. Tools: Optuna, Hyperopt, scikit-optimize. - Advantage: most sample-efficient approach. Disadvantage: overhead from fitting the surrogate.
4. Cross-Validation for Evaluation - For any search strategy, use $k$-fold cross-validation to estimate performance of each hyperparameter setting -- it reduces variance versus a single validation split. - For time series data: use expanding-window (walk-forward) CV, never standard $k$-fold. Standard $k$-fold leaks future data into training and gives optimistic, invalid estimates.
5. Early Stopping (for iterative models) - During training, monitor validation loss after each epoch. - Stop when validation loss stops improving for a specified number of epochs (patience). - Acts as implicit regularization and automatically selects the number of training iterations.
Practical Considerations: - Always log-scale the learning rate search range (e.g., sample uniformly from $[-5, -1]$ in $\log_{10}$ space). - Use a separate held-out test set that is never touched during hyperparameter search -- not even once. - Start with published defaults or prior work on similar tasks; refine from there rather than searching blindly. - Common high-impact hyperparameters: learning rate, regularization strength $\lambda$, tree depth, number of estimators, dropout rate. - Nested cross-validation is the statistically rigorous approach when both model selection and performance estimation are needed.
Answer: Start with random search to identify promising regions of the hyperparameter space, then refine with Bayesian optimization if compute permits. Use $k$-fold cross-validation (or time-series CV for sequential data) to evaluate each candidate. Never tune on the test set.
Intuition
Hyperparameter tuning is an instance of a broader principle in machine learning: you need a separate loop for each level of decision-making. Model parameters are learned from training data; hyperparameters control the learning process and must be selected using held-out data. Violating this separation -- even once, even accidentally -- inflates reported performance and leads to models that underperform in production.
The practical shift in the field has been toward Bayesian optimization and automated ML (AutoML) tools, because grid search does not scale and random search ignores information from previous evaluations. In quant finance specifically, where training data is scarce and overfitting is endemic, the walk-forward cross-validation discipline is especially important: a strategy that looks good under standard $k$-fold CV may be entirely an artifact of look-ahead bias.