Sklearn Byom Outputs
Train a SKLearn Model using Script Mode
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.
The aim of this notebook is to demonstrate how to train and deploy a scikit-learn model in Amazon SageMaker. The method used is called Script Mode, in which we write a script to train our model and submit it to the SageMaker Python SDK. For more information, feel free to read Using Scikit-learn with the SageMaker Python SDK.
Runtime
This notebook takes approximately 15 minutes to run.
Contents
Download data
Download the Iris Data Set, which is the data used to trained the model in this demo.
Prepare data
Next, we prepare the data for training by first converting the labels from string to integers. Then we split the data into a train dataset (80% of the data) and test dataset (the remaining 20% of the data) before saving them into CSV files. Then, these files are uploaded to S3 where the SageMaker SDK can access and use them to train the model.
{0: 'Iris-setosa', 1: 'Iris-versicolor', 2: 'Iris-virginica'}
120 train, 30 test
Train model
The model is trained using the SageMaker SDK's Estimator class. Firstly, get the execution role for training. This role allows us to access the S3 bucket in the last step, where the train and test data set is located.
arn:aws:iam::000000000000:role/ProdBuildSystemStack-ReleaseBuildRoleFB326D49-QK8LUA2UI1IC
Then, it is time to define the SageMaker SDK Estimator class. We use an Estimator class specifically desgined to train scikit-learn models called SKLearn. In this estimator, we define the following parameters:
- The script that we want to use to train the model (i.e.
entry_point). This is the heart of the Script Mode method. Additionally, set thescript_modeparameter toTrue. - The role which allows us access to the S3 bucket containing the train and test data set (i.e.
role) - How many instances we want to use in training (i.e.
instance_count) and what type of instance we want to use in training (i.e.instance_type) - Which version of scikit-learn to use (i.e.
framework_version) - Training hyperparameters (i.e.
hyperparameters)
After setting these parameters, the fit function is invoked to train the model.
2022-04-18 00:12:36 Starting - Starting the training job... 2022-04-18 00:13:05 Starting - Preparing the instances for trainingProfilerReport-1650240755: InProgress ...... 2022-04-18 00:14:06 Downloading - Downloading input data... 2022-04-18 00:14:34 Training - Downloading the training image.....2022-04-18 00:15:09,496 sagemaker-containers INFO Imported framework sagemaker_sklearn_container.training 2022-04-18 00:15:09,499 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed) 2022-04-18 00:15:09,510 sagemaker_sklearn_container.training INFO Invoking user training script. 2022-04-18 00:15:09,813 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed) 2022-04-18 00:15:09,826 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed) 2022-04-18 00:15:09,838 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed) 2022-04-18 00:15:09,851 sagemaker-training-toolkit INFO Invoking user script Training Env: { "additional_framework_parameters": {}, "channel_input_dirs": { "train": "/opt/ml/input/data/train" }, "current_host": "algo-1", "framework_module": "sagemaker_sklearn_container.training:main", "hosts": [ "algo-1" ], "hyperparameters": { "estimators": 20 }, "input_config_dir": "/opt/ml/input/config", "input_data_config": { "train": { "TrainingInputMode": "File", "S3DistributionType": "FullyReplicated", "RecordWrapperType": "None" } }, "input_dir": "/opt/ml/input", "is_master": true, "job_name": "sagemaker-scikit-learn-2022-04-18-00-12-35-728", "log_level": 20, "master_hostname": "algo-1", "model_dir": "/opt/ml/model", "module_dir": "s3://sagemaker-us-west-2-000000000000/sagemaker-scikit-learn-2022-04-18-00-12-35-728/source/sourcedir.tar.gz", "module_name": "train", "network_interface_name": "eth0", "num_cpus": 4, "num_gpus": 0, "output_data_dir": "/opt/ml/output/data", "output_dir": "/opt/ml/output", "output_intermediate_dir": "/opt/ml/output/intermediate", "resource_config": { "current_host": "algo-1", "current_instance_type": "ml.c5.xlarge", "current_group_name": "homogeneousCluster", "hosts": [ "algo-1" ], "instance_groups": [ { "instance_group_name": "homogeneousCluster", "instance_type": "ml.c5.xlarge", "hosts": [ "algo-1" ] } ], "network_interface_name": "eth0" }, "user_entry_point": "train.py" } Environment variables: SM_HOSTS=["algo-1"] SM_NETWORK_INTERFACE_NAME=eth0 SM_HPS={"estimators":20} SM_USER_ENTRY_POINT=train.py SM_FRAMEWORK_PARAMS={} SM_RESOURCE_CONFIG={"current_group_name":"homogeneousCluster","current_host":"algo-1","current_instance_type":"ml.c5.xlarge","hosts":["algo-1"],"instance_groups":[{"hosts":["algo-1"],"instance_group_name":"homogeneousCluster","instance_type":"ml.c5.xlarge"}],"network_interface_name":"eth0"} SM_INPUT_DATA_CONFIG={"train":{"RecordWrapperType":"None","S3DistributionType":"FullyReplicated","TrainingInputMode":"File"}} SM_OUTPUT_DATA_DIR=/opt/ml/output/data SM_CHANNELS=["train"] SM_CURRENT_HOST=algo-1 SM_MODULE_NAME=train SM_LOG_LEVEL=20 SM_FRAMEWORK_MODULE=sagemaker_sklearn_container.training:main SM_INPUT_DIR=/opt/ml/input SM_INPUT_CONFIG_DIR=/opt/ml/input/config SM_OUTPUT_DIR=/opt/ml/output SM_NUM_CPUS=4 SM_NUM_GPUS=0 SM_MODEL_DIR=/opt/ml/model SM_MODULE_DIR=s3://sagemaker-us-west-2-000000000000/sagemaker-scikit-learn-2022-04-18-00-12-35-728/source/sourcedir.tar.gz SM_TRAINING_ENV={"additional_framework_parameters":{},"channel_input_dirs":{"train":"/opt/ml/input/data/train"},"current_host":"algo-1","framework_module":"sagemaker_sklearn_container.training:main","hosts":["algo-1"],"hyperparameters":{"estimators":20},"input_config_dir":"/opt/ml/input/config","input_data_config":{"train":{"RecordWrapperType":"None","S3DistributionType":"FullyReplicated","TrainingInputMode":"File"}},"input_dir":"/opt/ml/input","is_master":true,"job_name":"sagemaker-scikit-learn-2022-04-18-00-12-35-728","log_level":20,"master_hostname":"algo-1","model_dir":"/opt/ml/model","module_dir":"s3://sagemaker-us-west-2-000000000000/sagemaker-scikit-learn-2022-04-18-00-12-35-728/source/sourcedir.tar.gz","module_name":"train","network_interface_name":"eth0","num_cpus":4,"num_gpus":0,"output_data_dir":"/opt/ml/output/data","output_dir":"/opt/ml/output","output_intermediate_dir":"/opt/ml/output/intermediate","resource_config":{"current_group_name":"homogeneousCluster","current_host":"algo-1","current_instance_type":"ml.c5.xlarge","hosts":["algo-1"],"instance_groups":[{"hosts":["algo-1"],"instance_group_name":"homogeneousCluster","instance_type":"ml.c5.xlarge"}],"network_interface_name":"eth0"},"user_entry_point":"train.py"} SM_USER_ARGS=["--estimators","20"] SM_OUTPUT_INTERMEDIATE_DIR=/opt/ml/output/intermediate SM_CHANNEL_TRAIN=/opt/ml/input/data/train SM_HP_ESTIMATORS=20 PYTHONPATH=/opt/ml/code:/miniconda3/bin:/miniconda3/lib/python37.zip:/miniconda3/lib/python3.7:/miniconda3/lib/python3.7/lib-dynload:/miniconda3/lib/python3.7/site-packages Invoking script with the following command: /miniconda3/bin/python train.py --estimators 20 2022-04-18 00:15:11,397 sagemaker-containers INFO Reporting training SUCCESS 2022-04-18 00:15:34 Uploading - Uploading generated training model 2022-04-18 00:15:34 Completed - Training job completed Training seconds: 82 Billable seconds: 82
Deploy and test endpoint
After training the model, it is time to deploy it as an endpoint. To do so, we invoke the deploy function within the scikit-learn estimator. As shown in the code below, one can define the number of instances (i.e. initial_instance_count) and instance type (i.e. instance_type) used to deploy the model.
------!
After the endpoint has been completely deployed, it can be invoked using the SageMaker Runtime Client (which is the method used in the code cell below) or Scikit Learn Predictor. If you plan to use the latter method, make sure to use a Serializer to serialize your data properly.
Predicted class category 1 (Iris-versicolor)
Cleanup
If the model and endpoint are no longer in use, they should be deleted to save costs and free up resources.
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.