Tuning without fooling yourself
Choose what to search, how far, and how you'll know when to stop. Compare grid, random and Bayesian search, read the trials, and report what you tried.
What runs in CodeSplash
You will be able to
- Separate parameters from hyperparameters and say which to tune
- Choose a search method and a budget
- Read a tuning run's trials and judge what the search bought
Which settings?
ReadTraining learns parameters, like weights and splits. You pick hyperparametersHyperparameterA setting chosen before training (depth, learning rate, penalty) rather than learned from the data.: the settings that fix how it learns.
A few matter a lot. For boosting: rounds and rate, tree size, rows per leaf. Many don't matter much on a given dataset.
So don't search them all. Start with the defaults the app marks: the tune dialog ticks those for you under Hyperparameters to search.
Grid or random?
PredictPredict first
Two hyperparameters, and only one of them really matters. You can afford 9 trials. Grid search gives 3 values each. Random search draws 9 pairs. Which explores the important one better?
Reveal
Random. The grid tries only 3 distinct values of the one that matters, while random tries 9. With more settings the gap widens, which is why random search beats grids in high dimensions, as Bergstra and Bengio showed in 2012.
Tune by hand first
ExploreTwo settings that interact: Learning rate (boosting) and Trees so far. Find three different pairs that all get within 10% of the lowest New-data MSE. Then try a higher Tree depth.
DExplorable · Bagging and boosting
How do many weak trees combine into a strong model?
With up to 200 trees available, at how many trees will error on new data be lowest?
Method
Commit your prediction above to see the result
Make your prediction to see the result.
You've found a ridge, not a peak. Good settings often lie along a line, and that's why one fixed value and an early stop can replace a big search.
Search methods
ReadThe app proposes every candidate itself, so a tuning run in Python and the same run in R see the same candidates.
| Search | What it does |
|---|---|
| Bayesian (TPE) | Learns from earlier trials where good settings are; usually the most efficient |
| Random search | Independent draws: a strong, simple baseline |
| Quasi-random (Sobol) | Covers the space more evenly than random |
| Grid | A few values per setting, every combination |
Weak trials stop after the first folds, using a median rule, so the budget goes further. The presets give Quick, Standard and Thorough.
Reading the result
ReadThe Tuning tab shows Best settings, Which hyperparameters mattered and every trial.
View data
| trial | ROC AUC | status |
|---|---|---|
| 1 | 0.8416 | complete |
| 2 | 0.7488 | complete |
| 3 | 0.8209 | complete |
| 4 | 0.8574 | complete |
| 5 | 0.7799 | complete |
| 6 | 0.8580 | complete |
| 7 | 0.8472 | complete |
| 8 | 0.7604 | complete |
| 9 | 0.7422 | complete |
| 10 | 0.8088 | complete |
| 11 | 0.8602 | complete |
| 12 | 0.8702 | complete |
| 13 | 0.8166 | pruned |
| 14 | 0.8471 | complete |
| 15 | 0.8407 | complete |
| 16 | 0.8531 | complete |
| 17 | 0.8381 | complete |
| 18 | 0.8755 | complete |
| 19 | 0.8387 | complete |
| 20 | 0.8472 | complete |
| 21 | 0.8902 | complete |
| 22 | 0.8606 | complete |
| 23 | 0.8275 | failed |
| 24 | 0.8522 | complete |
| 25 | 0.8472 | complete |
| 26 | 0.8377 | pruned |
| 27 | 0.8512 | complete |
| 28 | 0.8413 | pruned |
| 29 | 0.8441 | pruned |
| 30 | 0.8721 | complete |
| 31 | 0.8457 | pruned |
| 32 | 0.8498 | pruned |
| 33 | 0.8576 | complete |
| 34 | 0.8575 | complete |
| 35 | 0.8799 | complete |
| 36 | 0.8251 | pruned |
| 37 | 0.8195 | pruned |
| 38 | 0.8358 | pruned |
| 39 | 0.8675 | complete |
| 40 | 0.8666 | complete |
| 41 | 0.8509 | pruned |
| 42 | 0.8983 | complete |
| 43 | 0.8760 | complete |
| 44 | 0.8549 | pruned |
| 45 | 0.8975 | complete |
| 46 | 0.8746 | complete |
| 47 | 0.8207 | pruned |
| 48 | 0.8513 | pruned |
| 49 | 0.8608 | complete |
| 50 | 0.8708 | complete |
| 51 | 0.9000 | complete |
| 52 | 0.8818 | complete |
| 53 | 0.8875 | complete |
| 54 | 0.8625 | complete |
| 55 | 0.8608 | complete |
| 56 | 0.8899 | complete |
| 57 | 0.8511 | pruned |
| 58 | 0.9000 | complete |
| 59 | 0.8462 | pruned |
| 60 | 0.8412 | pruned |
Dots are trials, and the step line is the best so far. A long flat stretch means more trials will mostly add selection bias. Check whether the best values sit at the edge of a range: that's a sign to widen it.
Then look at the gain. If tuning moved the score by less than the fold-to-fold spread, the default was fine.
Your budget
ReflectYour next model has 2,000 rows and takes a minute to fit on five folds. You have an hour. Which learner would you tune, which three settings, with which preset? How will you report how many trials you ran, so a reader can judge the winner's score?
Check your understanding
CheckpointCheck yourself
Which is a hyperparameter of a random forest?
Show the answer
Answer: b
Thresholds are learned from the data. You set Minimum rows per leaf before training.
Check yourself
The best Learning rate is at the very bottom of the search range. What should you do?
Show the answer
Answer: a
The real optimum may lie outside. Widen the range, but count the extra trials as more selection.
Check yourself
A tuned model's cross-validated AUC is 0.84, and its test AUC is 0.81. The default model's cross-validated AUC is 0.83. What's the fair summary?
Show the answer
Answer: b
The best inner score includes a selection effect, and the one honest look is the test set. Retuning to the test score would spend it.
Check yourself
A grid has 3 values for each of 3 hyperparameters. How many trials is that?
Show the answer
Answer: 27
. Each added setting multiplies the count, and that's why grids don't scale.
Run a study
In the appSelect a learner in the Learners step, as the walkthrough does for {{target}}, and press Tune…. Choose Search, a preset, and the hyperparameters. The result joins the leaderboard: open it to read the Tuning tab. Robustness can also show Hyperparameter sensitivity from the trials.
import pandas as pd
from scipy.stats import loguniform, randint
from sklearn.model_selection import RandomizedSearchCV, StratifiedKFold
from xgboost import XGBClassifier
df = pd.read_csv("pima.csv")
X, y = df.drop(columns="diabetic"), df["diabetic"]
space = {"learning_rate": loguniform(0.01, 0.3), "max_depth": randint(2, 7),
"n_estimators": randint(50, 400), "min_child_weight": loguniform(1, 20)}
search = RandomizedSearchCV(XGBClassifier(), space, n_iter=15, scoring="roc_auc", random_state=1,
cv=StratifiedKFold(5, shuffle=True, random_state=1)).fit(X, y)
print(round(search.best_score_, 3), search.best_params_)library(xgboost)
df <- rbind(MASS::Pima.tr, MASS::Pima.te)
X <- as.matrix(df[, setdiff(names(df), "type")])
y <- as.numeric(df$type == "Yes")
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(X)))
trial <- function() {
p <- list(learning_rate = exp(runif(1, log(0.01), log(0.3))), max_depth = sample(2:6, 1),
min_child_weight = exp(runif(1, 0, log(20))), objective = "binary:logistic")
rounds <- sample(50:400, 1)
auc <- sapply(1:5, function(k) {
fit <- xgb.train(p, xgb.DMatrix(X[fold != k, ], label = y[fold != k]), nrounds = rounds, verbose = 0)
as.numeric(pROC::auc(y[fold == k], predict(fit, X[fold == k, ]), quiet = TRUE))
})
c(auc = mean(auc), rate = p$learning_rate, depth = p$max_depth, rounds = rounds)
}
res <- t(replicate(15, trial()))
round(res[which.max(res[, "auc"]), ], 3)Recap
Recap- Tune the few settings that matter, starting from good defaults.
- Random or Bayesian search beats a grid once there are many settings.
- Read the trials, watch for edges and flat stretches, and report how many you ran.
Concepts in this lesson