Skip to content

Latest commit

 

History

History
180 lines (127 loc) · 11.4 KB

012.md

File metadata and controls

180 lines (127 loc) · 11.4 KB

Introduction to the Keras Tuner

原文:https://tensorflow.google.cn/tutorials/keras/keras_tuner

Overview

The Keras Tuner is a library that helps you pick the optimal set of hyperparameters for your TensorFlow program. The process of selecting the right set of hyperparameters for your machine learning (ML) application is called hyperparameter tuning or hypertuning.

Hyperparameters are the variables that govern the training process and the topology of an ML model. These variables remain constant over the training process and directly impact the performance of your ML program. Hyperparameters are of two types:

  1. Model hyperparameters which influence model selection such as the number and width of hidden layers
  2. Algorithm hyperparameters which influence the speed and quality of the learning algorithm such as the learning rate for Stochastic Gradient Descent (SGD) and the number of nearest neighbors for a k Nearest Neighbors (KNN) classifier

In this tutorial, you will use the Keras Tuner to perform hypertuning for an image classification application.

Setup

import tensorflow as tf
from tensorflow import keras

import IPython 

Install and import the Keras Tuner.

!pip install -q -U keras-tuner
import kerastuner as kt 
WARNING: You are using pip version 20.2.2; however, version 20.2.3 is available.
You should consider upgrading via the '/tmpfs/src/tf_docs_env/bin/python -m pip install --upgrade pip' command.

Download and prepare the dataset

In this tutorial, you will use the Keras Tuner to find the best hyperparameters for a machine learning model that classifies images of clothing from the Fashion MNIST dataset.

Load the data.

(img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data() 
# Normalize pixel values between 0 and 1
img_train = img_train.astype('float32') / 255.0
img_test = img_test.astype('float32') / 255.0 

Define the model

When you build a model for hypertuning, you also define the hyperparameter search space in addition to the model architecture. The model you set up for hypertuning is called a hypermodel.

You can define a hypermodel through two approaches:

  • By using a model builder function
  • By subclassing the HyperModel class of the Keras Tuner API

You can also use two pre-defined HyperModel classes - HyperXception and HyperResNet for computer vision applications.

In this tutorial, you use a model builder function to define the image classification model. The model builder function returns a compiled model and uses hyperparameters you define inline to hypertune the model.

def model_builder(hp):
  model = keras.Sequential()
  model.add(keras.layers.Flatten(input_shape=(28, 28)))

  # Tune the number of units in the first Dense layer
  # Choose an optimal value between 32-512
  hp_units = hp.Int('units', min_value = 32, max_value = 512, step = 32)
  model.add(keras.layers.Dense(units = hp_units, activation = 'relu'))
  model.add(keras.layers.Dense(10))

  # Tune the learning rate for the optimizer 
  # Choose an optimal value from 0.01, 0.001, or 0.0001
  hp_learning_rate = hp.Choice('learning_rate', values = [1e-2, 1e-3, 1e-4]) 

  model.compile(optimizer = keras.optimizers.Adam(learning_rate = hp_learning_rate),
                loss = keras.losses.SparseCategoricalCrossentropy(from_logits = True), 
                metrics = ['accuracy'])

  return model 

Instantiate the tuner and perform hypertuning

Instantiate the tuner to perform the hypertuning. The Keras Tuner has four tuners available - RandomSearch, Hyperband, BayesianOptimization, and Sklearn. In this tutorial, you use the Hyperband tuner.

To instantiate the Hyperband tuner, you must specify the hypermodel, the objective to optimize and the maximum number of epochs to train (max_epochs).

tuner = kt.Hyperband(model_builder,
                     objective = 'val_accuracy', 
                     max_epochs = 10,
                     factor = 3,
                     directory = 'my_dir',
                     project_name = 'intro_to_kt') 

The Hyperband tuning algorithm uses adaptive resource allocation and early-stopping to quickly converge on a high-performing model. This is done using a sports championship style bracket. The algorithm trains a large number of models for a few epochs and carries forward only the top-performing half of models to the next round. Hyperband determines the number of models to train in a bracket by computing 1 + logfactor(max_epochs) and rounding it up to the nearest integer.

Before running the hyperparameter search, define a callback to clear the training outputs at the end of every training step.

class ClearTrainingOutput(tf.keras.callbacks.Callback):
  def on_train_end(*args, **kwargs):
    IPython.display.clear_output(wait = True) 

Run the hyperparameter search. The arguments for the search method are the same as those used for tf.keras.model.fit in addition to the callback above.

tuner.search(img_train, label_train, epochs = 10, validation_data = (img_test, label_test), callbacks = [ClearTrainingOutput()])

# Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]

print(f"""
The hyperparameter search is complete. The optimal number of units in the first densely-connected
layer is {best_hps.get('units')} and the optimal learning rate for the optimizer
is {best_hps.get('learning_rate')}.
""") 

<iframe src="/tutorials/keras/keras_tuner_a519a78fea1911af04b2b9f7741c7a9753570cdd628b143c45b62cc7e1dd9962.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_ba2b44510f9e52684b49b0a894a6561d8e3fe1f44b674eb91418c93636c5e160.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_c2c5323108f4a7f5343e68d7709c1634a11ab4e4f369de802dd6d70997cc3c6f.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_f2075d216cc02c6b0dc3c4178af7fa1943023cb5865c03f9f49d5ead7e18701b.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_9ba94851ba50c73e2db777d333047fc2c1874bebed4fbfea5a7380298e700a65.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_50ba5b64a7fe929f83ea8725a872fdc9dedf3ef0ce7b7405a67a068666fabd73.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_168a4b88269c92f3ed9acf9a80c2b878f4fd9ca3ead0b2cdb205c4130bd83094.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_47b0a34966f43feebd04e333a60e16b04a27545aa60bf5520a59d6d7548fb9d7.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_e3825583c6f56f151f2465e7f385806859a0162d75c21b38045cdc1c9440c8e4.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_2e55df2b3a5831eeb4af6388d0d5dcb4bc9072b2679abd631ce3b38072257a12.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_6a5cdf58938bdd56963be72d5e8fe1cac4b1945785df35af1116dc123d320e58.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_ff7616aec6013d63a1d3c5dc5d4d5f8713577dac721174196098b2213945656a.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe><iframe src="/tutorials/keras/keras_tuner_e0f11c6203af33c31efc4fcbd8ab0f615a85620bf28f4672f398a6c8860a3a39.frame" class="framebox inherit-locale " allowfullscreen="" is-upgraded=""></iframe>

Epoch 3/4
 911/1875 [=============>................] - ETA: 1s - loss: 0.5757 - accuracy: 0.8040

To finish this tutorial, retrain the model with the optimal hyperparameters from the search.

# Build the model with the optimal hyperparameters and train it on the data
model = tuner.hypermodel.build(best_hps)
model.fit(img_train, label_train, epochs = 10, validation_data = (img_test, label_test)) 
Epoch 1/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.4787 - accuracy: 0.8303 - val_loss: 0.4199 - val_accuracy: 0.8509
Epoch 2/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3600 - accuracy: 0.8684 - val_loss: 0.3902 - val_accuracy: 0.8570
Epoch 3/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3253 - accuracy: 0.8794 - val_loss: 0.3670 - val_accuracy: 0.8689
Epoch 4/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3038 - accuracy: 0.8874 - val_loss: 0.3714 - val_accuracy: 0.8684
Epoch 5/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2842 - accuracy: 0.8939 - val_loss: 0.3527 - val_accuracy: 0.8758
Epoch 6/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2678 - accuracy: 0.9005 - val_loss: 0.3334 - val_accuracy: 0.8785
Epoch 7/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2533 - accuracy: 0.9055 - val_loss: 0.3277 - val_accuracy: 0.8834
Epoch 8/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2445 - accuracy: 0.9089 - val_loss: 0.3487 - val_accuracy: 0.8768
Epoch 9/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2352 - accuracy: 0.9116 - val_loss: 0.3352 - val_accuracy: 0.8843
Epoch 10/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.2260 - accuracy: 0.9145 - val_loss: 0.3457 - val_accuracy: 0.8814

<tensorflow.python.keras.callbacks.History at 0x7f1f802512b0>

The my_dir/intro_to_kt directory contains detailed logs and checkpoints for every trial (model configuration) run during the hyperparameter search. If you re-run the hyperparameter search, the Keras Tuner uses the existing state from these logs to resume the search. To disable this behavior, pass an additional overwrite = True argument while instantiating the tuner.

Summary

In this tutorial, you learned how to use the Keras Tuner to tune hyperparameters for a model. To learn more about the Keras Tuner, check out these additional resources:

Also check out the HParams Dashboard in TensorBoard to interactively tune your model hyperparameters.