-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial commit of the SageMaker notebooks. #32
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1972ebb
Initial commit of the SageMaker notebooks.
ahsimb 9ea4259
Merge branch 'main' into mibe-sagemaker-notebooks
ahsimb 31d597b
Addressing review comments
ahsimb 72a7247
Merge remote-tracking branch 'origin/mibe-sagemaker-notebooks' into m…
ahsimb a10eac2
Merge branch 'main' into mibe-sagemaker-notebooks
ahsimb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,318 @@ | ||
{ | ||
tkilias marked this conversation as resolved.
Show resolved
Hide resolved
tkilias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "65088350-a826-4103-b57f-26377bc967b8", | ||
"metadata": {}, | ||
"source": [ | ||
"# Model deployment\n", | ||
"\n", | ||
"In this notebook, we will deploy the binary classification model created in the [previous notebook](train_model.ipynb) to a real-time AWS SageMaker endpoint. We will then use the model to make predictions on a test dataset. Please refer to the SageMaker Extension <a href=\"https://github.com/exasol/sagemaker-extension/blob/main/doc/user_guide/user_guide.md#prediction-on-aws-sagemaker-endpoint\" target=\"_blank\" rel=\"noopener\">User Guide</a> for a detailed description of this process.\n", | ||
"\n", | ||
"<b>Important! Please make sure you perform the last step - deletion of the endpoint. Leaving the endpoint in the cloud will incur continuous charges by AWS.</b>\n", | ||
"\n", | ||
"We will be running SQL queries using <a href=\"https://jupysql.ploomber.io/en/latest/quick-start.html\" target=\"_blank\" rel=\"noopener\"> JupySQL</a> SQL Magic.\n", | ||
"\n", | ||
"## Prerequisites\n", | ||
"\n", | ||
"Prior to using this notebook the following steps need to be completed:\n", | ||
"1. [Configure the sandbox](../sandbox_config.ipynb).\n", | ||
"2. [Initialize the SageMaker Extension](sme_init.ipynb).\n", | ||
"3. [Load the MAGIC Gamma Telescope data](../data/data_telescope.ipynb).\n", | ||
"4. [Train the model using SageMaker Autopilot](sme_train_model.ipynb).\n", | ||
"\n", | ||
"## Setup\n", | ||
"\n", | ||
"### Access configuration" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "22d0b288-a0ba-4a8a-981f-fc105bc01038", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%run ../access_store_ui.ipynb\n", | ||
"display(get_access_store_ui('../'))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "edba16c9-cd48-4075-8f7d-d6179dc011a8", | ||
"metadata": {}, | ||
"source": [ | ||
"### Other variables" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "66f3ee9a-5887-4516-b785-7f3791ec63e5", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"EXTERNAL_HOST = f\"{sb_config.EXTERNAL_HOST_NAME}:{sb_config.HOST_PORT}\"\n", | ||
"\n", | ||
"WEBSOCKET_URL = f\"exa+websocket://{sb_config.USER}:{sb_config.PASSWORD}\" \\\n", | ||
" f\"@{EXTERNAL_HOST}/{sb_config.SCHEMA}?SSLCertificate=SSL_VERIFY_NONE\"\n", | ||
"\n", | ||
"S3_BUCKET_URI=f\"s3://{sb_config.AWS_BUCKET}\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bdce84ae-7702-4f3e-b196-a7affcc01182", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's bring up JupySQL and connect to the database via SQLAlchemy. Please refer to the documentation in the sqlalchemy-exasol for details on how to connect to the database using Exasol SQLAlchemy driver." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "53a337ec-4f54-4d60-bb13-cb0c1221221c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sqlalchemy import create_engine\n", | ||
"\n", | ||
"engine = create_engine(WEBSOCKET_URL)\n", | ||
"\n", | ||
"%load_ext sql\n", | ||
"%sql engine" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "4399dbb0-d2f0-471a-96d6-a02cdd7744ff", | ||
"metadata": {}, | ||
"source": [ | ||
"## Deploy model to a SageMaker endpoint\n", | ||
"\n", | ||
"The script below deploys the best candidate model of the trained Autopilot job to an endpoint with a specified name. The deployment SQL command additionally generates the prediction UDF script with the same name. This UDF can be used for making predictions in an SQL statement.\n", | ||
"\n", | ||
"<img src=\"./sme_deployment.png\"/>\n", | ||
"<center>Model deployment</center>\n", | ||
"\n", | ||
"Let's define some variables." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f69e00ed-2cb2-4b7c-8eb0-a1a79f95e552", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Endpoint name, also the name of the generated UDF script.\n", | ||
"ENDPOINT_NAME = \"APSPredictor\"\n", | ||
"\n", | ||
"# The EC2 instance type of the endpoint to deploy the Autopilot model to.\n", | ||
"INSTANCE_TYPE = \"ml.m5.large\"\n", | ||
"\n", | ||
"# The initial number of instances to run the endpoint on.\n", | ||
"INSTANCE_COUNT = 1\n", | ||
"\n", | ||
"# Name of the table with the test data\n", | ||
"TEST_TABLE_NAME = \"TELESCOPE_TEST\"\n", | ||
"\n", | ||
"# Name of the column in the test table which is the prediction target.\n", | ||
"TARGET_COLUMN = \"CLASS\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "028a6784-26bc-4028-9009-32725c716e06", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%sql\n", | ||
"EXECUTE SCRIPT {{sb_config.SCHEMA}}.\"SME_DEPLOY_SAGEMAKER_AUTOPILOT_ENDPOINT\"(\n", | ||
" '{{sb_config.JOB_NAME}}', \n", | ||
" '{{ENDPOINT_NAME}}', \n", | ||
" '{{sb_config.SCHEMA}}',\n", | ||
" '{{INSTANCE_TYPE}}', \n", | ||
" {{INSTANCE_COUNT}}, \n", | ||
" '{{sb_config.AWS_CONN}}', \n", | ||
" '{{sb_config.AWS_REGION}}'\n", | ||
");" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "42578a73-8049-4751-9d6f-eca60579f6e1", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's check if the script has been created. We should be able to see an entry with the same name as the endpoint in the list of UDF scripts." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9bde0b81-eb3c-4cde-8cdc-4384d976386d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%sql\n", | ||
"SELECT SCRIPT_NAME, SCRIPT_TYPE \n", | ||
"FROM SYS.EXA_ALL_SCRIPTS\n", | ||
"WHERE SCRIPT_SCHEMA='{{sb_config.SCHEMA}}' AND SCRIPT_TYPE = 'UDF'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "26afbc93-e6c1-45f0-84e9-3f036ca9eabb", | ||
"metadata": {}, | ||
"source": [ | ||
"## Make predictions via SageMaker endpoint\n", | ||
"\n", | ||
"Let's use the generated UDF script for making predictions on the test data.\n", | ||
"\n", | ||
"First, we need to get a list of features to be passed to the UDF." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b939a986-3ddb-4e0a-a465-ec5c3167ced6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%sql column_names <<\n", | ||
"SELECT COLUMN_NAME\n", | ||
"FROM SYS.EXA_ALL_COLUMNS\n", | ||
"WHERE COLUMN_SCHEMA = '{{sb_config.SCHEMA}}' AND COLUMN_TABLE='{{TEST_TABLE_NAME}}' AND COLUMN_NAME <> UPPER('{{TARGET_COLUMN}}');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "639757ff-ecc0-46ad-8e96-a936af5770a2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"column_names = ', '.join(f'[{name[0]}]' for name in column_names)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "966b1c84-6078-4923-ab79-4e214690261a", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's predict classes for the first 10 rows of the test table, just to see how the output of the UDF looks like. Remember that the first column in the input is reserved for the sample ID. Here we can just set it to zero." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "61ba6f6c-6244-46a9-b3ee-4196c2b7b9ca", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%sql\n", | ||
"SELECT \"{{sb_config.SCHEMA}}\".\"{{ENDPOINT_NAME}}\"(0, {{column_names}})\n", | ||
"FROM \"{{sb_config.SCHEMA}}\".\"{{TEST_TABLE_NAME}}\"\n", | ||
"LIMIT 10" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8370e769-b10b-4375-9b3f-ec0d13b372f1", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we will compute the confusion matrix for the test data." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "422ece50-3706-4bcb-a2be-1736dbacd67f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%sql\n", | ||
"WITH TEST_DATA AS\n", | ||
"(\n", | ||
" -- We take data from the test table and add the row number calling it SAMPLE_ID.\n", | ||
" SELECT ROW_NUMBER() OVER () AS SAMPLE_ID, {{column_names}}, [{{TARGET_COLUMN}}] FROM \"{{sb_config.SCHEMA}}\".\"{{TEST_TABLE_NAME}}\"\n", | ||
")\n", | ||
"WITH MODEL_OUTPUT AS\n", | ||
"(\n", | ||
" -- Make predictions. We will pass the SAMPLE_ID that sould be returned back unchanged.\n", | ||
" SELECT \"{{sb_config.SCHEMA}}\".\"{{ENDPOINT_NAME}}\"(SAMPLE_ID, {{column_names}})\n", | ||
" FROM TEST_DATA\n", | ||
")\n", | ||
"-- Finally, compute the confusion matrix.\n", | ||
"SELECT predictions, [{{TARGET_COLUMN}}], COUNT(*) as count\n", | ||
"FROM MODEL_OUTPUT INNER JOIN TEST_DATA ON MODEL_OUTPUT.SAMPLE_ID = TEST_DATA.SAMPLE_ID\n", | ||
"GROUP BY 1, 2;" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ab428be0-c90c-440c-ab78-05816998e222", | ||
"metadata": {}, | ||
"source": [ | ||
"## Delete endpoint\n", | ||
"\n", | ||
"It is important to delete the endpoint once we finished working with it, to avoid unnecessary charges. The following script does that." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a12eebc0-f74c-4556-9be0-264d6d225abe", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%sql\n", | ||
"EXECUTE SCRIPT SME_DELETE_SAGEMAKER_AUTOPILOT_ENDPOINT(\n", | ||
" '{{ENDPOINT_NAME}}', \n", | ||
" '{{sb_config.AWS_CONN}}', \n", | ||
" '{{sb_config.AWS_REGION}}'\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "2cf063c3-7afa-4853-9e61-915fcfc17d21", | ||
"metadata": {}, | ||
"source": [ | ||
"## Conclusion\n", | ||
"\n", | ||
"In this set of notebooks, we went through the steps required to train, deploy and use models based on the SageMaker Autopilot with the help of the Exasol SageMaker-Extension. The advantages the SageMaker-Extension provides include simple and fast uploading of training data into S3 buckets and getting predictions with SQL queries." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7e8d885b-eeb7-4849-a064-9d980bc1243e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"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.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reduce the instance type more, maybe to a ml.t3.medium?
Reply via ReviewNB
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am yet to learn what it means, so will go with your suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://aws.amazon.com/sagemaker/pricing/