Notebooks
A
Amazon Web Services
Hpo Tensorflow2 Mnist Outputs

Hpo Tensorflow2 Mnist Outputs

data-scienceinferencearchivedtensorflow2_mnistamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningsagemakerjupyter-notebooktrainingmlops

Hyperparameter Tuning with the SageMaker TensorFlow Container


This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.

This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable


This tutorial focuses on how to create a convolutional neural network model to train the MNIST dataset using the SageMaker TensorFlow container. It leverages hyperparameter tuning to run multiple training jobs with different hyperparameter combinations, to find the one with the best model training result.

Runtime

This notebook takes approximately 10 minutes to run.

Contents

  1. Set Up the Environment
  2. Data
  3. Run a TensorFlow Training Job
  4. Set Up Channels for Training and Testing Data
  5. Run a Hyperparameter Tuning Job
  6. Deploy the Best Model
  7. Evaluate
  8. Cleanup

Set Up the Environment

Set up a few things before starting the workflow:

  1. A boto3 session object to manage interactions with the Amazon SageMaker APIs.
  2. An execution role which is passed to SageMaker to access your AWS resources.
[2]

Data

Download the MNIST data from a public S3 bucket and save it in a temporary directory.

[3]

Run a TensorFlow Training Job

A TensorFlow training job is defined by using the TensorFlow estimator class. It lets you run your training script on SageMaker infrastructure in a containerized environment. For more information on how to instantiate it, see the example Train an MNIST model with TensorFlow.

[4]

Set Up Channels for Training and Testing Data

Upload the MNIST data to the default bucket of your AWS account and pass the S3 URI as the channels of training and testing data for the TensorFlow estimator class.

[5]

Run a Hyperparameter Tuning Job

Now that you have set up the training job and the input data channels, you are ready to train the model with hyperparameter search.

Set up the hyperparameter tuning job with the following steps:

  • Define the ranges of hyperparameters we plan to tune. In this example, we tune the learning rate.
  • Define the objective metric for the tuning job to optimize.
  • Create a hyperparameter tuner with the above setting, as well as tuning resource configurations.

For a typical ML model, there are three kinds of hyperparamters:

  • Categorical parameters need to take one value from a discrete set. We define this by passing the list of possible values to CategoricalParameter(list)
  • Continuous parameters can take any real number value between the minimum and maximum value, defined by ContinuousParameter(min, max)
  • Integer parameters can take any integer value between the minimum and maximum value, defined by IntegerParameter(min, max)

Learning rate is a continuous variable, so we define its range by ContinuousParameter.

[6]

Next we specify the objective metric that we'd like to tune and its definition, which includes the regular expression (regex) needed to extract that metric from the CloudWatch logs of the training job. In this particular case, our script emits average loss value and we use it as the objective metric. We set the objective_type to Minimize, so that hyperparameter tuning seeks to minimize the objective metric when searching for the best hyperparameter value.

[7]

Now, you'll create a HyperparameterTuner object. It takes the following parameters:

  • The TensorFlow estimator you previously created.
  • Your hyperparameter ranges.
  • Objective metric name and definition.
  • Tuning resource configurations such as the number of training jobs to run in total, and how many training jobs to run in parallel.
[8]
..................................................................................................................................................................................................................................................................................................................................................................................................................................!

Deploy the Best Model

After training with hyperparameter optimization, you can deploy the best-performing model (by the objective metric you defined) to a SageMaker endpoint. For more information about deploying a model to a SageMaker endpoint, see the example Deploy a Trained TensorFlow V2 Model.

[9]

2022-04-18 00:54:36 Starting - Preparing the instances for training
2022-04-18 00:54:36 Downloading - Downloading input data
2022-04-18 00:54:36 Training - Training image download completed. Training in progress.
2022-04-18 00:54:36 Uploading - Uploading generated training model
2022-04-18 00:54:36 Completed - Training job completed
update_endpoint is a no-op in sagemaker>=2.
See: https://sagemaker.readthedocs.io/en/stable/v2.html for details.

----!

Evaluate

Now, you can evaluate the best-performing model by invoking the endpoint with the MNIST test set. The test data needs to be readily consumable by the model, so we arrange them into the correct shape that is accepted by a TensorFlow model. We also normalize them so that the pixel values have mean 0 and standard deviation 1, since this is the convention used to train the model.

[10]
Output
[11]
Predictions:  6 4 8 6 1 7 3 3 6 9 6 5 7 1 6 3

Cleanup

If you do not plan to continue using the endpoint, delete it to free up resources.

[12]

Notebook CI Test Results

This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.

This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable