Lesson 2 of 4 Hyperparameters, Tuning and Model Fit

Tuning Hyperparameters with SageMaker Automatic Model Tuning

This lesson explains SageMaker AI automatic model tuning (AMT), which runs many training jobs with different hyperparameter values and searches for the combination that optimises an objective metric.

Learning outcomes

After completing this lesson, you will be able to:

  • Configure a tuning job with an objective metric and ranges.
  • Choose between Bayesian, random, grid and Hyperband search.
  • Reduce tuning cost with warm start and early stopping.

Configuring a tuning job

A tuning job needs an estimator, an objective metric and whether to maximise or minimise it (for example, maximise validation:auc), parameter ranges and limits on the total number of training jobs and on parallel jobs. Ranges are continuous, integer or categorical, with a scaling type: linear, logarithmic (for values spanning orders of magnitude, such as a learning rate from 0.0001 to 0.1) or reverse logarithmic (for values close to 1, such as momentum).

True or false

Tuning job settings

Exam skills 2.2.3

Check how a SageMaker AI tuning job is configured.

Log in to try this activity Interactive activities are free with an account.

Search strategies

Bayesian optimisation (the default) builds a model of how hyperparameters affect the metric and chooses promising values based on earlier results; it usually needs the fewest jobs. Random search samples values independently, so it runs many jobs in parallel efficiently. Grid search tries every combination of categorical values. Hyperband starts many configurations and stops weak ones early based on intermediate results, which makes it efficient for large searches of iterative algorithms. Bayesian search learns only from completed jobs, so running fewer jobs in parallel gives it more information per round.

The following table compares the strategies.

StrategyHow it searchesBest for
Bayesian (default)Learns from previous resultsLimited budget, expensive jobs
RandomIndependent samplesMany parallel jobs
GridEvery categorical combinationSmall categorical spaces
HyperbandEarly-stops weak configurationsLarge searches of iterative training

The following diagram compares four parallel jobs over ten rounds with forty jobs at once.

Forty XGBoost tuning jobs run two ways. With four parallel jobs, Bayesian search runs ten rounds and chooses each round's values from the results of earlier rounds, while early stopping ends jobs unlikely to win. With all forty in parallel, the search finishes sooner but has nothing to learn from.
Which jobs stop early is illustrative.
Sort into groups

Choose the search strategy

Exam skills 2.2.3

Choose the AMT search strategy that fits each situation.

Log in to try this activity Interactive activities are free with an account.

Warm start and early stopping

Hyperparameter tuning can be expensive, and two features reduce its cost. Warm start uses one or more previous tuning jobs as a starting point, so knowledge is not thrown away when tuning continues or the data is updated. IdenticalDataAndAlgorithm continues tuning with the same data and algorithm, and TransferLearning reuses results after the data or algorithm has changed.

Training job early stopping, set to Auto, stops individual training jobs whose intermediate metric shows they are unlikely to beat the best job so far, so the tuning job does not pay for runs that will not win.

Scenario questions

Reuse earlier tuning

Exam skills 2.2.3

Choose the warm start type for a new tuning job.

Log in to try this activity Interactive activities are free with an account.
Worked example

Tuning XGBoost

A team tunes an XGBoost model with a budget of 40 training jobs.

from sagemaker.tuner import HyperparameterTuner, ContinuousParameter, IntegerParameter
ranges = {
    "eta": ContinuousParameter(0.01, 0.3, scaling_type="Logarithmic"),
    "max_depth": IntegerParameter(3, 10),
    "subsample": ContinuousParameter(0.5, 1.0),
    "min_child_weight": ContinuousParameter(1, 10),
}
tuner = HyperparameterTuner(xgb_estimator, objective_metric_name="validation:auc",
                            objective_type="Maximize", hyperparameter_ranges=ranges,
                            strategy="Bayesian", max_jobs=40, max_parallel_jobs=4,
                            early_stopping_type="Auto")
tuner.fit({"train": train_s3, "validation": val_s3})

Logarithmic scaling on eta samples small values as often as large ones, Bayesian search with four parallel jobs learns between rounds, and early stopping ends weak jobs. Running all 40 jobs in parallel would finish sooner but would give Bayesian search nothing to learn from.

Check yourself

Which scaling suits a hyperparameter whose values span orders of magnitude?

Show answer

Logarithmic scaling.

Exam Tips

Remember for the exam

  • Default and most budget-efficient: Bayesian optimisation.
  • Values spanning orders of magnitude: logarithmic scaling.
  • Large searches of iterative models: Hyperband.
  • Continue from previous tuning: warm start.
  • Fewer parallel jobs help Bayesian search learn.