{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Lesson5-AWSML-Operationalization.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [ "c_Id55m6Jsbu", "bq4VmHjPpMOR", "Wy7wioth10_L", "9LCNxWRJ3mZd", "iBzRF3DI3moU", "_qj08RlD4A-H" ], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "metadata": { "id": "j8TnIYSn1rvg", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Lesson 5: Operationalize Machine Learning on AWS\n", "\n", "[Watch Lesson 5: Operationalize Machine Learning on AWS Video](https://learning.oreilly.com/videos/aws-certified-machine/9780135556597/9780135556597-ACML_01_05_00)" ] }, { "metadata": { "id": "c_Id55m6Jsbu", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## Pragmatic AI Labs\n", "\n" ] }, { "metadata": { "id": "e5p96AqpSDZa", "colab_type": "text" }, "cell_type": "markdown", "source": [ "![alt text](https://paiml.com/images/logo_with_slogan_white_background.png)\n", "\n", "This notebook was produced by [Pragmatic AI Labs](https://paiml.com/). You can continue learning about these topics by:\n", "\n", "* Buying a copy of [Pragmatic AI: An Introduction to Cloud-Based Machine Learning](http://www.informit.com/store/pragmatic-ai-an-introduction-to-cloud-based-machine-9780134863863) from Informit.\n", "* Buying a copy of [Pragmatic AI: An Introduction to Cloud-Based Machine Learning](https://www.amazon.com/Pragmatic-AI-Introduction-Cloud-Based-Learning/dp/0134863860) from Amazon\n", "* Reading an online copy of [Pragmatic AI:Pragmatic AI: An Introduction to Cloud-Based Machine Learning](https://www.safaribooksonline.com/library/view/pragmatic-ai-an/9780134863924/)\n", "* Watching video [Essential Machine Learning and AI with Python and Jupyter Notebook-Video-SafariOnline](https://www.safaribooksonline.com/videos/essential-machine-learning/9780135261118) on Safari Books Online.\n", "* Watching video [AWS Certified Machine Learning-Speciality](https://learning.oreilly.com/videos/aws-certified-machine/9780135556597)\n", "* Purchasing video [Essential Machine Learning and AI with Python and Jupyter Notebook- Purchase Video](http://www.informit.com/store/essential-machine-learning-and-ai-with-python-and-jupyter-9780135261095)\n", "* Viewing more content at [noahgift.com](https://noahgift.com/)\n" ] }, { "metadata": { "id": "bq4VmHjPpMOR", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## Load AWS API Keys" ] }, { "metadata": { "id": "aWrzIk7WpRoh", "colab_type": "text" }, "cell_type": "markdown", "source": [ "Put keys in local or remote GDrive: \n", "\n", "`cp ~/.aws/credentials /Users/myname/Google\\ Drive/awsml/`" ] }, { "metadata": { "id": "hPWO_zyRopXN", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Mount GDrive\n" ] }, { "metadata": { "id": "XI73HZNLobp4", "colab_type": "code", "outputId": "66f597a9-6c94-4ffd-b323-21869a4eb988", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "from google.colab import drive\n", "drive.mount('/content/gdrive', force_remount=True)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Mounted at /content/gdrive\n" ], "name": "stdout" } ] }, { "metadata": { "id": "UNyzZwgmoxwm", "colab_type": "code", "outputId": "80a98691-3926-4666-82c7-f27f606fbfaf", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "import os;os.listdir(\"/content/gdrive/My Drive/awsml\")" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['kaggle.json', 'credentials', 'config']" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "metadata": { "id": "fYu0ekUlqPk6", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Install Boto" ] }, { "metadata": { "id": "dJDDrUkWrYRY", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "!pip -q install boto3\n" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "FpJhrpSQsK5E", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Create API Config" ] }, { "metadata": { "id": "QxRwGOZtsN0-", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "!mkdir -p ~/.aws &&\\\n", " cp /content/gdrive/My\\ Drive/awsml/credentials ~/.aws/credentials " ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "Kj977UW3rph_", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Test Comprehend API Call" ] }, { "metadata": { "id": "P-A8Cia-raT0", "colab_type": "code", "outputId": "4290a9a8-667f-4b06-edf3-aa4c2ae121f2", "colab": { "base_uri": "https://localhost:8080/", "height": 238 } }, "cell_type": "code", "source": [ "import boto3\n", "comprehend = boto3.client(service_name='comprehend', region_name=\"us-east-1\")\n", "text = \"There is smoke in San Francisco\"\n", "comprehend.detect_sentiment(Text=text, LanguageCode='en')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'ResponseMetadata': {'HTTPHeaders': {'connection': 'keep-alive',\n", " 'content-length': '160',\n", " 'content-type': 'application/x-amz-json-1.1',\n", " 'date': 'Thu, 22 Nov 2018 00:21:54 GMT',\n", " 'x-amzn-requestid': '9d69a0a9-edec-11e8-8560-532dc7aa62ea'},\n", " 'HTTPStatusCode': 200,\n", " 'RequestId': '9d69a0a9-edec-11e8-8560-532dc7aa62ea',\n", " 'RetryAttempts': 0},\n", " 'Sentiment': 'NEUTRAL',\n", " 'SentimentScore': {'Mixed': 0.008628507144749165,\n", " 'Negative': 0.1037612184882164,\n", " 'Neutral': 0.8582549691200256,\n", " 'Positive': 0.0293553676456213}}" ] }, "metadata": { "tags": [] }, "execution_count": 50 } ] }, { "metadata": { "id": "Wy7wioth10_L", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.1 Understand ML Operations" ] }, { "metadata": { "id": "XqVIECd13mW0", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Key Concepts" ] }, { "metadata": { "id": "OWz-nzDsq4xs", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n", "\n", "* Monitoring\n", "* Security\n", "* Retraining Models\n", "* A/B Testing\n", "* TCO (Total Cost of Ownership)\n", "\n", "\n" ] }, { "metadata": { "id": "Jh1781q32Rs3", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### MLOPS\n" ] }, { "metadata": { "id": "-pInLXNU2Tg8", "colab_type": "text" }, "cell_type": "markdown", "source": [ "* Are you using a simple enough model?\n", "* Are you using the Data Lake or wired directly into production SQL DB?\n", "* Do you have alerts setup for prediction threshold failures?\n", "* Environments? Dev, Stage, Prod" ] }, { "metadata": { "id": "9LCNxWRJ3mZd", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.2 Use Containerization with Machine Learning and Deep Learning" ] }, { "metadata": { "id": "iStgEd4g3meD", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Key Concepts" ] }, { "metadata": { "id": "QtWvU6pVrLE4", "colab_type": "text" }, "cell_type": "markdown", "source": [ "![docker_workflows](https://user-images.githubusercontent.com/58792/49868900-9b415400-fdc3-11e8-807f-375bbe0a4684.png)" ] }, { "metadata": { "id": "bQR81GSkTxqV", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Amazon ECS (Elastic Container Service)" ] }, { "metadata": { "id": "_DJtXn8uPMkq", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### [Demo] ECS" ] }, { "metadata": { "id": "-Y7Im88zPX7g", "colab_type": "text" }, "cell_type": "markdown", "source": [ "\n", "\n", "* Create a repo\n", "* List item\n", "\n" ] }, { "metadata": { "id": "v-Uk0cIfT9T5", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Amazon EKS (Kubernetes on AWS)" ] }, { "metadata": { "id": "XuA5Jz1nVJMh", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] }, { "metadata": { "id": "iBzRF3DI3moU", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.3 Implement continuous deployment and delivery for Machine Learning" ] }, { "metadata": { "id": "QrpNUFiw4A7M", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Key Concepts" ] }, { "metadata": { "id": "uOve6XahrhJt", "colab_type": "text" }, "cell_type": "markdown", "source": [ "![codebuild](https://user-images.githubusercontent.com/58792/49869955-da24d900-fdc6-11e8-925f-767fb7fb522f.png)\n" ] }, { "metadata": { "id": "XbuXpZ9dRFk0", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### [Demo] Code Build" ] }, { "metadata": { "id": "A63Gu5sgRMDH", "colab_type": "text" }, "cell_type": "markdown", "source": [ "* buildspec.yml\n", "* console\n", "* build job\n", "* sync to s3\n", "* ECS integration" ] }, { "metadata": { "id": "_qj08RlD4A-H", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.4 A/B Testing production deployments" ] }, { "metadata": { "id": "ULLTKu844BAr", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Key Concepts\n" ] }, { "metadata": { "id": "fLowy9Rmr0RD", "colab_type": "text" }, "cell_type": "markdown", "source": [ "* Sagemaker A/B Testing Capabilities\n", "* Deciding on ratio of delivery to ML Model" ] }, { "metadata": { "id": "OUmtwRLMRSY_", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### [Demo] Sagemaker A/B" ] }, { "metadata": { "id": "80fnx7574BC6", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.5 Troubleshoot Production Deployment" ] }, { "metadata": { "id": "Tt6BwBEV4qB3", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Key Concepts" ] }, { "metadata": { "id": "NrCH2JlYsD-9", "colab_type": "text" }, "cell_type": "markdown", "source": [ "* Using Cloudwatch\n", "* Searching Cloudwatch Logs\n", "* Alerting on key events\n", "* Using Auto-Scale Capabilities\n", "* Enterprise AWS Support" ] }, { "metadata": { "id": "SHNU5WSN1xh-", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### [Demo]Cloudwatch Features" ] }, { "metadata": { "id": "S4dTBcuc13Gi", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] }, { "metadata": { "id": "XQZB4upQ4qE4", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.6 Production Security" ] }, { "metadata": { "id": "JGs2E08_4qHK", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Key Concepts" ] }, { "metadata": { "id": "tRVvIzZxslgF", "colab_type": "text" }, "cell_type": "markdown", "source": [ "* Understanding KMS system (Encryption)\n", "* IAM Roles for Sagemaker\n", "* IAM Roles for VPC\n" ] }, { "metadata": { "id": "gNHxYVJJR28e", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### [Demo] Sagemaker Security Features" ] }, { "metadata": { "id": "2TirycC3R5qk", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] }, { "metadata": { "id": "w7l8bRAa45Ny", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 5.7 Cost and Efficiency of ML Systems" ] }, { "metadata": { "id": "nLczj-xj5O1R", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Key Concepts" ] }, { "metadata": { "id": "pdghodT5stIj", "colab_type": "text" }, "cell_type": "markdown", "source": [ "* Understanding Spot Instances (show spot code)\n", "* Understanding Proper use of CPU vs GPU Resources\n", "* Scale up and Scale Down\n", "* Improve Time to Market\n", "* Choosing wisely from AI API vs \"Do it Yourself\"\n" ] }, { "metadata": { "id": "45Dcpc9sITVX", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### [Demo] Spot Instances on AWS\n", "\n" ] }, { "metadata": { "id": "oR5-MrnYIY9c", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] } ] }