Path: blob/master/site/en-snapshot/tensorboard/hyperparameter_tuning_with_hparams.ipynb
25115 views
Copyright 2019 The TensorFlow Authors.
Hyperparameter Tuning with the HParams Dashboard
When building machine learning models, you need to choose various hyperparameters, such as the dropout rate in a layer or the learning rate. These decisions impact model metrics, such as accuracy. Therefore, an important step in the machine learning workflow is to identify the best hyperparameters for your problem, which often involves experimentation. This process is known as "Hyperparameter Optimization" or "Hyperparameter Tuning".
The HParams dashboard in TensorBoard provides several tools to help with this process of identifying the best experiment or most promising sets of hyperparameters.
This tutorial will focus on the following steps:
Experiment setup and HParams summary
Adapt TensorFlow runs to log hyperparameters and metrics
Start runs and log them all under one parent directory
Visualize the results in TensorBoard's HParams dashboard
Note: The HParams summary APIs and dashboard UI are in a preview stage and will change over time.
Start by installing TF 2.0 and loading the TensorBoard notebook extension:
Import TensorFlow and the TensorBoard HParams plugin:
Download the FashionMNIST dataset and scale it:
1. Experiment setup and the HParams experiment summary
Experiment with three hyperparameters in the model:
Number of units in the first dense layer
Dropout rate in the dropout layer
Optimizer
List the values to try, and log an experiment configuration to TensorBoard. This step is optional: you can provide domain information to enable more precise filtering of hyperparameters in the UI, and you can specify which metrics should be displayed.
If you choose to skip this step, you can use a string literal wherever you would otherwise use an HParam
value: e.g., hparams['dropout']
instead of hparams[HP_DROPOUT]
.
2. Adapt TensorFlow runs to log hyperparameters and metrics
The model will be quite simple: two dense layers with a dropout layer between them. The training code will look familiar, although the hyperparameters are no longer hardcoded. Instead, the hyperparameters are provided in an hparams
dictionary and used throughout the training function:
For each run, log an hparams summary with the hyperparameters and final accuracy:
When training Keras models, you can use callbacks instead of writing these directly:
3. Start runs and log them all under one parent directory
You can now try multiple experiments, training each one with a different set of hyperparameters.
For simplicity, use a grid search: try all combinations of the discrete parameters and just the lower and upper bounds of the real-valued parameter. For more complex scenarios, it might be more effective to choose each hyperparameter value randomly (this is called a random search). There are more advanced methods that can be used.
Run a few experiments, which will take a few minutes:
4. Visualize the results in TensorBoard's HParams plugin
The HParams dashboard can now be opened. Start TensorBoard and click on "HParams" at the top.
The left pane of the dashboard provides filtering capabilities that are active across all the views in the HParams dashboard:
Filter which hyperparameters/metrics are shown in the dashboard
Filter which hyperparameter/metrics values are shown in the dashboard
Filter on run status (running, success, ...)
Sort by hyperparameter/metric in the table view
Number of session groups to show (useful for performance when there are many experiments)
The HParams dashboard has three different views, with various useful information:
The Table View lists the runs, their hyperparameters, and their metrics.
The Parallel Coordinates View shows each run as a line going through an axis for each hyperparemeter and metric. Click and drag the mouse on any axis to mark a region which will highlight only the runs that pass through it. This can be useful for identifying which groups of hyperparameters are most important. The axes themselves can be re-ordered by dragging them.
The Scatter Plot View shows plots comparing each hyperparameter/metric with each metric. This can help identify correlations. Click and drag to select a region in a specific plot and highlight those sessions across the other plots.
A table row, a parallel coordinates line, and a scatter plot market can be clicked to see a plot of the metrics as a function of training steps for that session (although in this tutorial only one step is used for each run).
To further explore the capabilities of the HParams dashboard, download a set of pregenerated logs with more experiments:
View these logs in TensorBoard:
You can try out the different views in the HParams dashboard.
For example, by going to the parallel coordinates view and clicking and dragging on the accuracy axis, you can select the runs with the highest accuracy. As these runs pass through 'adam' in the optimizer axis, you can conclude that 'adam' performed better than 'sgd' on these experiments.