Sub topic 13
Planning the compute budget
This chapter assumes you can run GridSearchCV and RandomizedSearchCV and that you know what k-fold cross-validation does. Tuning cost is easy to underestimate, because it multiplies. Estimate it before you start the search.
Count the fits
The unit of cost is one model fit. The total is:
total fits = settings tried × folds (+ 1 final refit)
A grid multiplies the value counts of every hyperparameter, so it grows exponentially with the number of hyperparameters. Random search lets you choose the number of trials directly.
| Strategy | Settings | Folds | Fits |
|---|---|---|---|
| Grid, 3 hyperparameters × 5 values | 125 | 5 | 625 |
| Grid, 4 hyperparameters × 5 values | 625 | 5 | 3,125 |
| Random search, 30 trials | 30 | 5 | 150 |
Time one fit first
Time a single cross-validated evaluation, then scale it. This code measures the cost of one setting and projects the total for two strategies.
import time
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_breast_cancer(return_X_y=True)
pipe = make_pipeline(StandardScaler(), SVC())
start = time.perf_counter()
cross_val_score(pipe, X, y, cv=5)
per_setting = time.perf_counter() - start
grid_size = 3 * 3
random_trials = 30
print(f"One setting (5 folds): {per_setting:.3f} s")
print(f"Grid of {grid_size}: about {grid_size * per_setting:.2f} s")
print(f"Random, {random_trials} trials: about {random_trials * per_setting:.2f} s")Divide the projected time by the number of parallel workers you can use. Set n_jobs=-1 to use all cores. The division is only approximate, because memory limits and process start-up cost reduce the gain.
Ways to spend less
- Cap trials, not ranges. Random search with a fixed
n_itergives a predictable cost. It also tends to find good settings in few trials when only a few hyperparameters matter. - Search coarse, then refine. Run a wide, cheap search first. Then narrow the ranges around the best region and search again.
- Use fewer folds while exploring. Use 3 folds early and 5 or 10 for the final comparison.
- Use less data or fewer iterations early. Successive halving, covered in the pruning chapter, discards weak settings after a small budget.
- Prefer cheap models first. If a linear model is within noise of a large ensemble, the cheaper model leaves more budget for other work.
Common errors
- Adding one more hyperparameter to a grid without recomputing the cost. Each new hyperparameter multiplies the total.
- Forgetting that nested cross-validation multiplies cost by the outer fold count. A 5-fold outer loop around a 3-fold inner grid of 9 settings costs 5 × (9 × 3 + 1) = 140 fits.
- Starting a long run with no time estimate and no way to stop it. Set a trial or time limit, and log the results as the run progresses.
Exercise
You tune 4 hyperparameters with 6 values each in a grid, using 5-fold cross-validation. One fit takes 2 seconds. How many fits does the search need (ignoring the final refit), roughly how long does it take on one core, and how long on 8 cores if parallelism is perfect?
Show answer
The grid has 6^4 = 1,296 settings. With 5 folds that is 6,480 fits. At 2 seconds each, that is 12,960 seconds, or about 3.6 hours on one core. On 8 cores it takes about 1,620 seconds, or about 27 minutes. Random search with 60 trials would need only 300 fits, or 10 minutes on one core.