A team tries fifty penalty values for a ridge model, keeps the one with the lowest cross-validated error and reports that error as the performance to expect. The winner was picked because its score was the lowest, and part of what made it lowest was luck in those particular folds. This lesson separates choosing a setting from measuring performance. It describes grid search and random search with a stated budget, explains why the best score of a search is optimistic, and shows how nested cross-validation, or a clean test set used once, evaluates the search itself.
Key terms
Hyperparameter tuning
Definition A hyperparameter is a setting fixed before a training algorithm runs, such as a ridge penalty , the number of neighbors or the maximum depth of a tree, while parameters such as coefficients are learned by the fitting. Hyperparameter tuning chooses a setting from a candidate set by estimating the error of each candidate on rows that did not fit it, with a validation set or cross-validation inside the development data, and taking the best estimate under a stated rule for ties,
The test set plays no part. Every learned preprocessing step is refitted inside each training split of the search, and once a setting is chosen, the procedure is refitted with it on all development data. Tuning is part of the training procedure, so an honest estimate of how the final model will perform has to evaluate the tuning as well, on rows the search never used.
In plain words Fitting answers which coefficients to use for given settings, and tuning answers which settings to use. Each candidate is judged on rows it did not learn from, and the test set is kept for the end.
Example Take ridge regression with one input, which minimizes
with the intercept unpenalized. With and , and assuming , the minimizer is
For the constructed training pairs and , the means are and , with and , so . The candidate gives the line and gives . On the validation pair both lines predict , because every ridge line here passes through , so the squared errors tie at and the stated rule picks the smaller penalty . A validation pair would separate them. The first line predicts with error , and the second predicts with squared error .
Grid search
Definition Grid search fixes a finite list of candidate values for each hyperparameter and evaluates every combination, the Cartesian product of the lists. With lists of sizes the grid has candidates. Evaluating each with -fold cross-validation takes fits, and one more fit refits the chosen candidate on all development data. Every candidate is scored on the same folds, so the scores are comparable and the search is reproducible from the lists and the fold assignment. The grid covers only the listed values. If good settings lie outside the listed range, the search cannot find them, and each hyperparameter is tried at only as many distinct values as its own list holds, however many combinations the grid contains.
In plain words Write down the values to try for each setting and test every combination on the same folds. It is simple and transparent, but its cost multiplies with every setting added, and it never looks between or beyond the listed values.
Example Two penalties and three feature sets give candidates, and four folds per candidate make fits before the final refit. The worked example of this lesson uses the grid . Penalty lists usually spread their values over orders of magnitude, such as , because in the formula above a penalty acts through its size relative to , which grows with the spread of the input and the number of rows.
Random search
Definition Random search draws settings independently from a stated probability distribution over the hyperparameter space, with a recorded seed, and evaluates each setting like a grid candidate. Its result depends on the distribution, the ranges, and the seed, so all of them are reported. For a positive scale setting such as a penalty or a step size, a log-uniform distribution on an interval with makes uniform on , so every factor of inside the range gets the same probability. A uniform distribution on the same interval with puts the fraction of its draws in the top factor of , from to , which is about percent when is tiny compared with . If a region of settings has probability under the sampling distribution, independent draws land in it at least once with probability . When only some hyperparameters matter, draws from a continuous distribution give distinct values of each one with probability , while a grid of candidates over several hyperparameters tries far fewer distinct values of each.
In plain words Instead of a fixed lattice, sample the settings at random from ranges you believe are sensible, and sample scale settings by their order of magnitude. Random search is no better than its ranges, and with a recorded seed it is as reproducible as a grid.
Example On the interval from to , a log-uniform draw falls at or below with probability , since that part covers three of the six factors of . A uniform draw on the same interval does so with probability . A region holding percent of the sampling probability is hit at least once by independent draws with probability . With two hyperparameters, a grid tries values of each, while random draws try values of each.
Search budget
Definition The search budget is the amount of searching a tuning procedure is allowed. It covers the number of candidates, the validation rows or folds per candidate, the resulting number of fits and the computing time, and the number of times results are inspected and the search is revised. Tuning is part of the procedure being evaluated, so the budget is reported together with the search space, the sampling distributions and seeds, the metric, the fold design, the tie rule and the final refit. A larger budget can find better settings, but it also gives chance more opportunities to make some candidate’s score look better than its true performance. Every revision of the search made after looking at evaluation results counts as searching, and the results that guided it become development data.
In plain words Say how hard you searched. A best score from two candidates and a best score from two thousand carry different amounts of luck, and a reader can judge a result only when the size and design of the search are on record.
Example The nested example below has outer folds, candidates and inner fits per candidate, so the inner searches take fits and the outer refits add , for fits in the estimate. Producing the final model takes a further search on all rows, fits, and refit. In general, outer folds, candidates and inner folds cost fits for the nested estimate.
Nested cross-validation
Definition Nested cross-validation estimates the performance of a whole tuning procedure. The development data are split into outer folds. For each outer fold , its outer training rows are split again into inner folds, and the complete search, with its candidates, preprocessing, budget and tie rule, runs by cross-validation on alone and selects . The procedure is refitted with on all of , giving , and scored on the held-out outer fold ,
or the size-weighted mean when the folds differ in size. No row of enters any inner fit, inner score, preprocessing step or selection for that outer fold. Different outer folds may select different settings, which is expected, because the object being evaluated is the search procedure and not one fixed setting. The estimate describes the search applied to training sets of about of the development rows. The deployable model comes from running the same search on all development data, selecting one setting and refitting. Groups and time order have to be respected by the inner folds as well as the outer ones. Nesting is one way to keep selection apart from evaluation. A clean test set, used once after a single search on the development data, is another. Revising the search after looking at outer scores turns those scores into development feedback.
In plain words The inner loop chooses and the outer loop grades. Each outer fold plays the part of future data for a complete run of the search, so the outer score shows how well the habit of searching this way works, rather than how good the luckiest candidate looked.
Example The constructed data have inputs and outcomes . Both loops use leave-one-out folds, the candidates are the ridge penalties and , and ties go to the smaller penalty. Inside every outer training set the inner mean squared error is about to for and about to for , so all eight searches select . With the row at input held out, the refit on the other seven rows is , which predicts against the outcome , a squared error of . The eight outer predictions are about , , , , , , and , and the nested mean squared error is about . The search on all eight rows also selects , and the final model is . Because every search chose the same penalty, the nested estimate equals the best leave-one-out score on all eight rows. The two numbers separate when the winning setting depends on which rows the search sees.
Optimistic selection bias
Definition Optimistic selection bias is the tendency of the best of several noisy performance estimates to look better than the selected candidate actually performs, when the same estimates are used both to choose and to report. Suppose candidates have true errors , and their estimates are unbiased, , as for errors measured on validation rows that were not used to fit any of the candidates. The reported minimum is at most each , so with the selected candidate,
The reported minimum therefore underestimates the selected candidate’s true error on average, and the gap tends to grow with the number of candidates that have similar true errors and noisy estimates. For accuracy, where larger is better, the same holds with maxima and the inequalities reversed. A clean test set used once on the selected procedure, or nested cross-validation, keeps the reported estimate apart from the selection. Test or outer scores consulted again and again to revise the search pick up the same bias.
In plain words If you pick the best of many noisy scores and report it, you report the luck along with the skill. The more candidates you try, the more luck the winner tends to carry, and only rows that played no part in the choice can measure the winner fairly.
Example Two procedures have the same true error , and each estimate is or with probability , independently. These numbers are constructed. The smaller estimate is only when both are , so the reported best has expected value , while the chosen procedure’s true error is . With such procedures the expected best is , which is for and about for . Observing for one procedure and for the other does not show that the first is better.
Check yourself
- Ridge regression with an unpenalized intercept is tuned over on the training pairs and , with the validation pairs and and ties going to the smaller penalty. Find both fitted lines, their validation mean squared errors and the selected penalty. What does the validation pair at input contribute?
Show the answer
The means are and . The centered inputs are and and the centered outcomes and , so and .
With the slope is and the intercept , so the line is . With the slope is and the intercept , so the line is .
At input both lines predict , a squared error of each. At input the first predicts , a squared error of , and the second predicts , a squared error of . The validation mean squared errors are and , so the search selects .
The input is the training mean of , where every ridge line passes through , so that pair adds the same error to both candidates and cannot separate them. The choice rests on the single pair at input . The winner’s validation error is the smaller of two estimates, so as a measure of the chosen line’s error on new cases it is optimistic, and two validation rows are very little evidence in any case.
- A report states that the best inner cross-validation accuracy of a search was percent and that the three outer folds gave , and percent. Which number estimates the performance of the search procedure, and can the percent be reported as independent final performance? What changes if the outer folds held , and cases?
Show the answer
The outer scores estimate the performance of the search procedure, because each comes from rows that took no part in the search that produced its model. Their equally weighted mean is
about percent. The percent is the best of the inner comparisons. It chose the setting, so it is a selected development result carrying optimistic selection bias, and it cannot be reported as independent final performance.
With outer folds of , and cases, the folds have , and correct predictions, and the accuracy over all outer cases is
which is , or percent. It counts every case once, while the equal-weight mean of percent gives every fold the same weight. Either way, report the design, the fold sizes and the search budget with the number. Three dependent fold scores do not by themselves give a valid confidence interval.
- A data set has visits from patients, visits each, and the model will be used for new patients. The search is a grid of penalties and feature sets with inner folds, evaluated with outer folds. How many fits does the nested estimate take, and how many more does the final model take if its search uses folds on all the development data? How should the folds be formed, and what should the report record?
Show the answer
The grid has candidates. In each outer fold the inner search takes fits and the refit adds , so the four outer folds take fits. The final search on all development data takes another fits and refit, so the whole plan costs fits.
Every fold at both levels has to keep whole patients together, since the model will meet new patients. The outer folds hold whole patients, about or each, and the inner folds are formed from the patients of each outer training set. Visits of one patient split between an inner training set and its inner validation fold would let the search favor settings that recognize familiar patients.
The report records the grid values, the metric, the fold assignments or the seeds that produced them, the tie rule, the budget of fits, and any change made to the search after outer results were seen. A random search with draws from stated distributions would cost the same number of fits and would need the same record.