{ "cells": [ { "cell_type": "markdown", "id": "e950fa8e", "metadata": { "papermill": { "duration": 0.00756, "end_time": "2022-04-18T00:12:33.638987", "exception": false, "start_time": "2022-04-18T00:12:33.631427", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "# Train a SKLearn Model using Script Mode\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "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. \n", "\n", "![This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/us-west-2/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "---" ] }, { "cell_type": "markdown", "id": "e950fa8e", "metadata": { "papermill": { "duration": 0.00756, "end_time": "2022-04-18T00:12:33.638987", "exception": false, "start_time": "2022-04-18T00:12:33.631427", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "\n", "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](https://sagemaker.readthedocs.io/en/stable/frameworks/sklearn/using_sklearn.html).\n", "\n", "## Runtime\n", "This notebook takes approximately 15 minutes to run.\n", "\n", "## Contents\n", "1. [Download data](#Download-data)\n", "1. [Prepare data](#Prepare-data)\n", "1. [Train model](#Train-model)\n", "1. [Deploy and test endpoint](#Deploy-and-test-endpoint)\n", "1. [Cleanup](#Cleanup)" ] }, { "cell_type": "markdown", "id": "a16db1a6", "metadata": { "papermill": { "duration": 0.007451, "end_time": "2022-04-18T00:12:33.653948", "exception": false, "start_time": "2022-04-18T00:12:33.646497", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "## Download data \n", "Download the [Iris Data Set](https://archive.ics.uci.edu/ml/datasets/iris), which is the data used to trained the model in this demo." ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "!pip install -U sagemaker" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 2, "id": "a670c242", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:33.673901Z", "iopub.status.busy": "2022-04-18T00:12:33.673111Z", "iopub.status.idle": "2022-04-18T00:12:34.541929Z", "shell.execute_reply": "2022-04-18T00:12:34.542348Z" }, "papermill": { "duration": 0.881064, "end_time": "2022-04-18T00:12:34.542493", "exception": false, "start_time": "2022-04-18T00:12:33.661429", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lensepal_widpetal_lenpetal_widclass
05.13.51.40.2Iris-setosa
14.93.01.40.2Iris-setosa
24.73.21.30.2Iris-setosa
34.63.11.50.2Iris-setosa
45.03.61.40.2Iris-setosa
\n", "
" ], "text/plain": [ " sepal_len sepal_wid petal_len petal_wid class\n", "0 5.1 3.5 1.4 0.2 Iris-setosa\n", "1 4.9 3.0 1.4 0.2 Iris-setosa\n", "2 4.7 3.2 1.3 0.2 Iris-setosa\n", "3 4.6 3.1 1.5 0.2 Iris-setosa\n", "4 5.0 3.6 1.4 0.2 Iris-setosa" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import boto3\n", "import pandas as pd\n", "import numpy as np\n", "\n", "s3 = boto3.client(\"s3\")\n", "s3.download_file(f\"sagemaker-sample-files\", \"datasets/tabular/iris/iris.data\", \"iris.data\")\n", "\n", "df = pd.read_csv(\n", " \"iris.data\", header=None, names=[\"sepal_len\", \"sepal_wid\", \"petal_len\", \"petal_wid\", \"class\"]\n", ")\n", "df.head()" ] }, { "cell_type": "markdown", "id": "7c03b3d2", "metadata": { "papermill": { "duration": 0.007833, "end_time": "2022-04-18T00:12:34.559513", "exception": false, "start_time": "2022-04-18T00:12:34.551680", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "## Prepare data\n", "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." ] }, { "cell_type": "code", "execution_count": 3, "id": "72748b04", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:34.581371Z", "iopub.status.busy": "2022-04-18T00:12:34.580273Z", "iopub.status.idle": "2022-04-18T00:12:34.591699Z", "shell.execute_reply": "2022-04-18T00:12:34.591328Z" }, "papermill": { "duration": 0.024375, "end_time": "2022-04-18T00:12:34.591802", "exception": false, "start_time": "2022-04-18T00:12:34.567427", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0: 'Iris-setosa', 1: 'Iris-versicolor', 2: 'Iris-virginica'}\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lensepal_widpetal_lenpetal_widclassclass_cat
05.13.51.40.2Iris-setosa0
14.93.01.40.2Iris-setosa0
24.73.21.30.2Iris-setosa0
34.63.11.50.2Iris-setosa0
45.03.61.40.2Iris-setosa0
\n", "
" ], "text/plain": [ " sepal_len sepal_wid petal_len petal_wid class class_cat\n", "0 5.1 3.5 1.4 0.2 Iris-setosa 0\n", "1 4.9 3.0 1.4 0.2 Iris-setosa 0\n", "2 4.7 3.2 1.3 0.2 Iris-setosa 0\n", "3 4.6 3.1 1.5 0.2 Iris-setosa 0\n", "4 5.0 3.6 1.4 0.2 Iris-setosa 0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Convert the three classes from strings to integers in {0,1,2}\n", "df[\"class_cat\"] = df[\"class\"].astype(\"category\").cat.codes\n", "categories_map = dict(enumerate(df[\"class\"].astype(\"category\").cat.categories))\n", "print(categories_map)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "fb5ea6cf", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:34.613358Z", "iopub.status.busy": "2022-04-18T00:12:34.612800Z", "iopub.status.idle": "2022-04-18T00:12:34.615760Z", "shell.execute_reply": "2022-04-18T00:12:34.615349Z" }, "papermill": { "duration": 0.015648, "end_time": "2022-04-18T00:12:34.615876", "exception": false, "start_time": "2022-04-18T00:12:34.600228", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120 train, 30 test\n" ] } ], "source": [ "# Split the data into 80-20 train-test split\n", "num_samples = df.shape[0]\n", "split = round(num_samples * 0.8)\n", "train = df.iloc[:split, :]\n", "test = df.iloc[split:, :]\n", "print(\"{} train, {} test\".format(split, num_samples - split))" ] }, { "cell_type": "code", "execution_count": 5, "id": "48770a6b", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:34.637252Z", "iopub.status.busy": "2022-04-18T00:12:34.636697Z", "iopub.status.idle": "2022-04-18T00:12:34.766181Z", "shell.execute_reply": "2022-04-18T00:12:34.766620Z" }, "papermill": { "duration": 0.142033, "end_time": "2022-04-18T00:12:34.766774", "exception": false, "start_time": "2022-04-18T00:12:34.624741", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "# Write train and test CSV files\n", "train.to_csv(\"train.csv\", index=False)\n", "test.to_csv(\"test.csv\", index=False)" ] }, { "cell_type": "code", "execution_count": 6, "id": "ba40dab3", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:34.789600Z", "iopub.status.busy": "2022-04-18T00:12:34.789024Z", "iopub.status.idle": "2022-04-18T00:12:35.222992Z", "shell.execute_reply": "2022-04-18T00:12:35.223387Z" }, "papermill": { "duration": 0.446718, "end_time": "2022-04-18T00:12:35.223525", "exception": false, "start_time": "2022-04-18T00:12:34.776807", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "# Create a sagemaker session to upload data to S3\n", "import sagemaker\n", "\n", "sagemaker_session = sagemaker.Session()\n", "\n", "# Upload data to default S3 bucket\n", "prefix = \"DEMO-sklearn-iris\"\n", "training_input_path = sagemaker_session.upload_data(\"train.csv\", key_prefix=prefix + \"/training\")" ] }, { "cell_type": "markdown", "id": "9d52c534", "metadata": { "papermill": { "duration": 0.009017, "end_time": "2022-04-18T00:12:35.241699", "exception": false, "start_time": "2022-04-18T00:12:35.232682", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "## Train model\n", "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." ] }, { "cell_type": "code", "execution_count": 7, "id": "f7cbdad2", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:35.268850Z", "iopub.status.busy": "2022-04-18T00:12:35.266829Z", "iopub.status.idle": "2022-04-18T00:12:35.665830Z", "shell.execute_reply": "2022-04-18T00:12:35.666190Z" }, "papermill": { "duration": 0.415648, "end_time": "2022-04-18T00:12:35.666345", "exception": false, "start_time": "2022-04-18T00:12:35.250697", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arn:aws:iam::000000000000:role/ProdBuildSystemStack-ReleaseBuildRoleFB326D49-QK8LUA2UI1IC\n" ] } ], "source": [ "# Use the current execution role for training. It needs access to S3\n", "role = sagemaker.get_execution_role()\n", "print(role)" ] }, { "cell_type": "markdown", "id": "10cdcfb6", "metadata": { "papermill": { "duration": 0.009195, "end_time": "2022-04-18T00:12:35.684895", "exception": false, "start_time": "2022-04-18T00:12:35.675700", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "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:\n", "1. 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 the `script_mode` parameter to `True`.\n", "1. The role which allows us access to the S3 bucket containing the train and test data set (i.e. `role`)\n", "1. 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`)\n", "1. Which version of scikit-learn to use (i.e. `framework_version`)\n", "1. Training hyperparameters (i.e. `hyperparameters`)\n", "\n", "After setting these parameters, the `fit` function is invoked to train the model." ] }, { "cell_type": "code", "execution_count": 8, "id": "ac14dcb7", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:12:35.707671Z", "iopub.status.busy": "2022-04-18T00:12:35.707161Z", "iopub.status.idle": "2022-04-18T00:15:47.797961Z", "shell.execute_reply": "2022-04-18T00:15:47.797435Z" }, "papermill": { "duration": 192.103985, "end_time": "2022-04-18T00:15:47.798072", "exception": false, "start_time": "2022-04-18T00:12:35.694087", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2022-04-18 00:12:36 Starting - Starting the training job...\n", "2022-04-18 00:13:05 Starting - Preparing the instances for trainingProfilerReport-1650240755: InProgress\n", "......\n", "2022-04-18 00:14:06 Downloading - Downloading input data...\n", "2022-04-18 00:14:34 Training - Downloading the training image.....\u001b[34m2022-04-18 00:15:09,496 sagemaker-containers INFO Imported framework sagemaker_sklearn_container.training\u001b[0m\n", "\u001b[34m2022-04-18 00:15:09,499 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed)\u001b[0m\n", "\u001b[34m2022-04-18 00:15:09,510 sagemaker_sklearn_container.training INFO Invoking user training script.\u001b[0m\n", "\u001b[34m2022-04-18 00:15:09,813 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed)\u001b[0m\n", "\u001b[34m2022-04-18 00:15:09,826 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed)\u001b[0m\n", "\u001b[34m2022-04-18 00:15:09,838 sagemaker-training-toolkit INFO No GPUs detected (normal if no gpus installed)\u001b[0m\n", "\u001b[34m2022-04-18 00:15:09,851 sagemaker-training-toolkit INFO Invoking user script\u001b[0m\n", "\u001b[34mTraining Env:\u001b[0m\n", "\u001b[34m{\n", " \"additional_framework_parameters\": {},\n", " \"channel_input_dirs\": {\n", " \"train\": \"/opt/ml/input/data/train\"\n", " },\n", " \"current_host\": \"algo-1\",\n", " \"framework_module\": \"sagemaker_sklearn_container.training:main\",\n", " \"hosts\": [\n", " \"algo-1\"\n", " ],\n", " \"hyperparameters\": {\n", " \"estimators\": 20\n", " },\n", " \"input_config_dir\": \"/opt/ml/input/config\",\n", " \"input_data_config\": {\n", " \"train\": {\n", " \"TrainingInputMode\": \"File\",\n", " \"S3DistributionType\": \"FullyReplicated\",\n", " \"RecordWrapperType\": \"None\"\n", " }\n", " },\n", " \"input_dir\": \"/opt/ml/input\",\n", " \"is_master\": true,\n", " \"job_name\": \"sagemaker-scikit-learn-2022-04-18-00-12-35-728\",\n", " \"log_level\": 20,\n", " \"master_hostname\": \"algo-1\",\n", " \"model_dir\": \"/opt/ml/model\",\n", " \"module_dir\": \"s3://sagemaker-us-west-2-000000000000/sagemaker-scikit-learn-2022-04-18-00-12-35-728/source/sourcedir.tar.gz\",\n", " \"module_name\": \"train\",\n", " \"network_interface_name\": \"eth0\",\n", " \"num_cpus\": 4,\n", " \"num_gpus\": 0,\n", " \"output_data_dir\": \"/opt/ml/output/data\",\n", " \"output_dir\": \"/opt/ml/output\",\n", " \"output_intermediate_dir\": \"/opt/ml/output/intermediate\",\n", " \"resource_config\": {\n", " \"current_host\": \"algo-1\",\n", " \"current_instance_type\": \"ml.c5.xlarge\",\n", " \"current_group_name\": \"homogeneousCluster\",\n", " \"hosts\": [\n", " \"algo-1\"\n", " ],\n", " \"instance_groups\": [\n", " {\n", " \"instance_group_name\": \"homogeneousCluster\",\n", " \"instance_type\": \"ml.c5.xlarge\",\n", " \"hosts\": [\n", " \"algo-1\"\n", " ]\n", " }\n", " ],\n", " \"network_interface_name\": \"eth0\"\n", " },\n", " \"user_entry_point\": \"train.py\"\u001b[0m\n", "\u001b[34m}\u001b[0m\n", "\u001b[34mEnvironment variables:\u001b[0m\n", "\u001b[34mSM_HOSTS=[\"algo-1\"]\u001b[0m\n", "\u001b[34mSM_NETWORK_INTERFACE_NAME=eth0\u001b[0m\n", "\u001b[34mSM_HPS={\"estimators\":20}\u001b[0m\n", "\u001b[34mSM_USER_ENTRY_POINT=train.py\u001b[0m\n", "\u001b[34mSM_FRAMEWORK_PARAMS={}\u001b[0m\n", "\u001b[34mSM_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\"}\u001b[0m\n", "\u001b[34mSM_INPUT_DATA_CONFIG={\"train\":{\"RecordWrapperType\":\"None\",\"S3DistributionType\":\"FullyReplicated\",\"TrainingInputMode\":\"File\"}}\u001b[0m\n", "\u001b[34mSM_OUTPUT_DATA_DIR=/opt/ml/output/data\u001b[0m\n", "\u001b[34mSM_CHANNELS=[\"train\"]\u001b[0m\n", "\u001b[34mSM_CURRENT_HOST=algo-1\u001b[0m\n", "\u001b[34mSM_MODULE_NAME=train\u001b[0m\n", "\u001b[34mSM_LOG_LEVEL=20\u001b[0m\n", "\u001b[34mSM_FRAMEWORK_MODULE=sagemaker_sklearn_container.training:main\u001b[0m\n", "\u001b[34mSM_INPUT_DIR=/opt/ml/input\u001b[0m\n", "\u001b[34mSM_INPUT_CONFIG_DIR=/opt/ml/input/config\u001b[0m\n", "\u001b[34mSM_OUTPUT_DIR=/opt/ml/output\u001b[0m\n", "\u001b[34mSM_NUM_CPUS=4\u001b[0m\n", "\u001b[34mSM_NUM_GPUS=0\u001b[0m\n", "\u001b[34mSM_MODEL_DIR=/opt/ml/model\u001b[0m\n", "\u001b[34mSM_MODULE_DIR=s3://sagemaker-us-west-2-000000000000/sagemaker-scikit-learn-2022-04-18-00-12-35-728/source/sourcedir.tar.gz\u001b[0m\n", "\u001b[34mSM_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\"}\u001b[0m\n", "\u001b[34mSM_USER_ARGS=[\"--estimators\",\"20\"]\u001b[0m\n", "\u001b[34mSM_OUTPUT_INTERMEDIATE_DIR=/opt/ml/output/intermediate\u001b[0m\n", "\u001b[34mSM_CHANNEL_TRAIN=/opt/ml/input/data/train\u001b[0m\n", "\u001b[34mSM_HP_ESTIMATORS=20\u001b[0m\n", "\u001b[34mPYTHONPATH=/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\u001b[0m\n", "\u001b[34mInvoking script with the following command:\u001b[0m\n", "\u001b[34m/miniconda3/bin/python train.py --estimators 20\u001b[0m\n", "\u001b[34m2022-04-18 00:15:11,397 sagemaker-containers INFO Reporting training SUCCESS\u001b[0m\n", "\n", "2022-04-18 00:15:34 Uploading - Uploading generated training model\n", "2022-04-18 00:15:34 Completed - Training job completed\n", "Training seconds: 82\n", "Billable seconds: 82\n" ] } ], "source": [ "# Docs: https://sagemaker.readthedocs.io/en/stable/frameworks/sklearn/sagemaker.sklearn.html\n", "\n", "from sagemaker.sklearn import SKLearn\n", "\n", "sk_estimator = SKLearn(\n", " entry_point=\"train.py\",\n", " role=role,\n", " instance_count=1,\n", " instance_type=\"ml.c5.xlarge\",\n", " py_version=\"py3\",\n", " framework_version=\"1.2-1\",\n", " script_mode=True,\n", " hyperparameters={\"estimators\": 20},\n", ")\n", "\n", "# Train the estimator\n", "sk_estimator.fit({\"train\": training_input_path})" ] }, { "cell_type": "markdown", "id": "3813b62c", "metadata": { "papermill": { "duration": 0.013288, "end_time": "2022-04-18T00:15:47.824920", "exception": false, "start_time": "2022-04-18T00:15:47.811632", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "## Deploy and test endpoint\n", "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." ] }, { "cell_type": "code", "execution_count": 9, "id": "06aace5c", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:15:47.857009Z", "iopub.status.busy": "2022-04-18T00:15:47.856519Z", "iopub.status.idle": "2022-04-18T00:18:49.237480Z", "shell.execute_reply": "2022-04-18T00:18:49.237860Z" }, "papermill": { "duration": 181.399805, "end_time": "2022-04-18T00:18:49.237997", "exception": false, "start_time": "2022-04-18T00:15:47.838192", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "------!" ] } ], "source": [ "import time\n", "\n", "sk_endpoint_name = \"sklearn-rf-model\" + time.strftime(\"%Y-%m-%d-%H-%M-%S\", time.gmtime())\n", "sk_predictor = sk_estimator.deploy(\n", " initial_instance_count=1, instance_type=\"ml.m5.large\", endpoint_name=sk_endpoint_name\n", ")" ] }, { "cell_type": "markdown", "id": "bbc747e1", "metadata": { "papermill": { "duration": 0.014884, "end_time": "2022-04-18T00:18:49.267735", "exception": false, "start_time": "2022-04-18T00:18:49.252851", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "After the endpoint has been completely deployed, it can be invoked using the [SageMaker Runtime Client](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker-runtime.html) (which is the method used in the code cell below) or [Scikit Learn Predictor](https://sagemaker.readthedocs.io/en/stable/frameworks/sklearn/sagemaker.sklearn.html#scikit-learn-predictor). If you plan to use the latter method, make sure to use a [Serializer](https://sagemaker.readthedocs.io/en/stable/api/inference/serializers.html) to serialize your data properly." ] }, { "cell_type": "code", "execution_count": 10, "id": "85491166", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:18:49.304390Z", "iopub.status.busy": "2022-04-18T00:18:49.303901Z", "iopub.status.idle": "2022-04-18T00:18:49.436952Z", "shell.execute_reply": "2022-04-18T00:18:49.436531Z" }, "papermill": { "duration": 0.154462, "end_time": "2022-04-18T00:18:49.437062", "exception": false, "start_time": "2022-04-18T00:18:49.282600", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predicted class category 1 (Iris-versicolor)\n" ] } ], "source": [ "import json\n", "\n", "client = sagemaker_session.sagemaker_runtime_client\n", "\n", "request_body = {\"Input\": [[9.0, 3571, 1976, 0.525]]}\n", "data = json.loads(json.dumps(request_body))\n", "payload = json.dumps(data)\n", "\n", "response = client.invoke_endpoint(\n", " EndpointName=sk_endpoint_name, ContentType=\"application/json\", Body=payload\n", ")\n", "\n", "result = json.loads(response[\"Body\"].read().decode())[\"Output\"]\n", "print(\"Predicted class category {} ({})\".format(result, categories_map[result]))" ] }, { "cell_type": "markdown", "id": "90f26921", "metadata": { "papermill": { "duration": 0.015271, "end_time": "2022-04-18T00:18:49.467415", "exception": false, "start_time": "2022-04-18T00:18:49.452144", "status": "completed" }, "pycharm": { "name": "#%% md\n" }, "tags": [] }, "source": [ "## Cleanup\n", "If the model and endpoint are no longer in use, they should be deleted to save costs and free up resources." ] }, { "cell_type": "code", "execution_count": 11, "id": "ec5a3a83", "metadata": { "execution": { "iopub.execute_input": "2022-04-18T00:18:49.502390Z", "iopub.status.busy": "2022-04-18T00:18:49.501867Z", "iopub.status.idle": "2022-04-18T00:18:49.887165Z", "shell.execute_reply": "2022-04-18T00:18:49.886712Z" }, "papermill": { "duration": 0.404624, "end_time": "2022-04-18T00:18:49.887279", "exception": false, "start_time": "2022-04-18T00:18:49.482655", "status": "completed" }, "pycharm": { "name": "#%%\n" }, "tags": [] }, "outputs": [], "source": [ "sk_predictor.delete_model()\n", "sk_predictor.delete_endpoint()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Notebook CI Test Results\n", "\n", "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.\n", "\n", "![This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/us-east-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/us-east-2/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/us-west-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/ca-central-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/sa-east-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/eu-west-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/eu-west-2/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/eu-west-3/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/eu-central-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/eu-north-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/ap-southeast-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/ap-southeast-2/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/ap-northeast-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/ap-northeast-2/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n", "\n", "![This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://prod.us-west-2.tcx-beacon.docs.aws.dev/sagemaker-nb/ap-south-1/sagemaker-script-mode|sklearn|sklearn_byom_outputs.ipynb)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.9" }, "papermill": { "default_parameters": {}, "duration": 377.636152, "end_time": "2022-04-18T00:18:50.419483", "environment_variables": {}, "exception": null, "input_path": "sklearn_byom.ipynb", "output_path": "/opt/ml/processing/output/sklearn_byom-2022-04-18-00-07-51.ipynb", "parameters": { "kms_key": "arn:aws:kms:us-west-2:000000000000:1234abcd-12ab-34cd-56ef-1234567890ab" }, "start_time": "2022-04-18T00:12:32.783331", "version": "2.3.4" } }, "nbformat": 4, "nbformat_minor": 5 }