Notebooks
A
Amazon Web Services
Scikit Learn Model Registry Batch Transform

Scikit Learn Model Registry Batch Transform

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

Develop, Train, Register and Batch Transform Scikit-Learn Random Forest


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


In this notebook we show how to use Amazon SageMaker to train a Scikit-learn Random Forest model, register it in Model Registry, and run a Batch Transform Job. More info on Scikit-Learn can be found here https://scikit-learn.org/stable/index.html. We use the California Housing dataset, present in Scikit-Learn: https://scikit-learn.org/stable/modules/generated/sklearn.datasets.fetch_california_housing.html. The California Housing dataset was originally published in:

Pace, R. Kelley, and Ronald Barry. "Sparse spatial auto regressions." Statistics & Probability Letters 33.3 (1997): 291-297.

Link to the paper: https://doi.org/10.1016/S0167-7152(96)00140-X

[ ]
[ ]
[ ]

Prepare data

We use the California housing dataset.

More info on the dataset:

This dataset was obtained from the StatLib repository. http://lib.stat.cmu.edu/datasets/

The target variable is the median house value for California districts.

This dataset was derived from the 1990 U.S. census, using one row per census block group. A block group is the smallest geographical unit for which the U.S. Census Bureau publishes sample data (a block group typically has a population of 600 to 3,000 people).

[ ]
[ ]
[ ]
[ ]
[ ]

Let's inspect the training dataset

[ ]
[ ]

Save training, testing and evaluation data as csv and upload to S3

[ ]

Upload training and evaluation data to S3, as SageMaker Training Job, and afterward, Batch Transform Job will take it from there.

[ ]
[ ]

Writing a Script Mode script

The below script contains both training and inference functionality and can run both in SageMaker Training hardware or locally (desktop, SageMaker notebook, on premise, etc.). Detailed guidance here https://sagemaker.readthedocs.io/en/stable/using_sklearn.html#preparing-the-scikit-learn-training-script

[ ]

Launching a SageMaker training job with the Python SDK

We will train two models: the first with 100 epochs, and the second with 300 epochs. The number of epochs has no specific meaning. We are interested in training two models, so we will be able to register each one of them into SageMaker Model Registry.

Launch the 1st training job

Once we've defined our estimator, we can specify the hyperparameters we'd like to tune and their possible values. This time we will train with 100 epochs.

[ ]
[ ]

Create a Model Package Group for the trained model to be registered

Create a new Model Package Group or use an existing one to register the model

[ ]

Register the model of the 1st training job in the Model Registry

Once the model is registered, you will see it in the Model Registry tab of the SageMaker Studio UI. The model is registered with the approval_status set to "Approved". By default, the model is registered with the approval_status set to PendingManualApproval. Users can then navigate to the Model Registry to manually approve the model based on any criteria set for model evaluation or this can be done via API.

[ ]

Create a transform job with the default configurations from the model of the 1st training job

[ ]
[ ]

Let's inspect the output of the Batch Transform job in S3. It should show the median income in block group.

[ ]
[ ]
[ ]
[ ]

Launch the 2nd training job

This time we will train with 300 epochs.

[ ]
[ ]

Register the model of 2nd training job in the Model Registry

[ ]

View Model Groups and Versions

You can view details of a specific model version by using either the AWS SDK for Python (Boto3) or by using Amazon SageMaker Studio. To view the details of a model version by using Boto3, call the list_model_packages method to view the model versions in a model group

[ ]

Let's fetch the latest model version from the Model Package Group

[ ]

View the latest Model Version details

Call describe_model_package to see the details of the model version. You pass in the ARN of a model version that you got in the output of the call to list_model_packages.

[ ]

Create a transform job with the default configurations from the model of the 2nd training job

[ ]
[ ]

Let's inspect the output locations of both Batch Transform jobs in S3. You can see they have different locations due to their separate Batch Transform jobs.

[ ]
[ ]

Conclusion

In this notebook you successfully downloaded the California housing dataset and trained a model using SageMaker Python SDK. Then you created a ModelPackageGroup, registered the Model Version in SageMaker Model Registry, and triggered a SageMaker Batch Transform Job to process the evaluation dataset from S3.

You trained another model, this time with 300 epochs, registered this Model Version in SageMaker Model Registry, viewed the model versions, and again, triggered a SageMaker Batch Transform Job to process the evaluation dataset from S3.

As next steps, you can try registering your own model in SageMaker Model Registry, and run a SageMaker Batch Transform Job on data you have on S3.

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