Deploy Machine Learning Model using Amazon SageMaker | How to Deploy ML Models on AWS | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] amazon sage maker is a cloud machine learning platform that was launched in november 2017. sagemaker enables developers to create train and deploy machine learning models in the cloud sagemaker also enables developers to deploy ml models on embedded systems and edge devices hi everyone this is kavya from eduwaker welcome to today's session on how to deploy a machine learning model on amazon sage maker before we begin today's session i'd like to address the agenda firstly we will understand what is amazon sage maker and then we'll create your very own amazon web services account then moving on we will create your first notebook instance then finally we will download and explore data set train your model deploy your model and then finally evaluate your model we'll conclude the session with the case study kindly take up this time to subscribe to us and do not forget to hit that bell icon to never miss an update from the edureka youtube channel also to learn more about new trending technologies visit our page the link to which is given in the description box below so without much ado let's get started what is amazon siege maker amazon sage maker is a cloud machine learning platform that enables developers to build train and deploy ml models very quickly it is basically a service that enables a developer to build and train machine learning models for predictive or analytical applications in the amazon web services public cloud now you know that data science and machine learning models are in demand right now so amazon sage maker is here to help you machine learning offers a variety of benefits for enterprises such as advanced analytics for customer data or even back end security threat detection but it can be hard for it professionals to deploy these models without prior expertise so here is where amazon sage maker comes amazon sage maker aims to address this challenge as it provides built-in and common machine learning algorithms along with other tools to simplify and accelerate the entire process amazon siege maker supports jupyter notebooks which are open source web applications that help developers share live code now for sage maker users these notebooks include drivers packages and libraries for common deep learning platforms and frameworks a developer can launch a pre-built notebook which aws supplies for a variety of applications and use cases then customize it according to the data sets and schema the developer wants to train developers can also obviously use custom built algorithms written in one of the supported ml frameworks or any code that has been packaged as a docker container image sagemaker can basically pull data from amazon simple storage service that we'll be addressing later on in the session and there is obviously no practical limit to the size of the data set here so to get started a developer logs into the sagemaker console and launches a notebook instance sagemaker here provides a variety of built-in algorithms such as linear regression image classification or the developer can import his or her own algorithm then when the developer is ready to train the model he or she specifies the location of the data in s3 and the preferred instance type and then eventually initiates the training process sagemaker uses automatic model tuning to find the set of parameters also called as hyper parameters that best optimizes the algorithm when the model is finally ready to deploy the service automatically operates and scales cloud infrastructure using a set of sagemaker instance types that include several with gpu accelerators and that have been optimized for ml workloads sagemaker deploys across multiple availability zones performs health checks applies security patches sets up auto scaling and establishes secure https endpoints to connect to an application a developer can then track and trigger alarms for changes in production performance by amazon cloud watch metrics now that we know what is amazon sage make on how the entire workflow really works in sage maker let's move on to the next part of the session and learn how to create your own aws account so i'm just going to go to my local web browser and i'm just going to google for aws i'm just going to click the first link which will redirect me to the official website of amazon web services and as you can see here there's a navigation on top that will allow me to check the different and various products that amazon you know offer to us there are amazon ec2 there's amazon simple storage service that is s3 we'll be using this later on in the session the different solutions there's pricing of all of the products that amazon provide to their customers there are also events that happen in aws that you can participate or be a part of and if you scroll down you can see the solutions that amazon provide in the form of icons and this is just a summary of all of what they really do so this is not really necessary for you to know right now we're just going to create an account so what i'm going to do is i'm just going to click on to sign into the console and as you can see that this will redirect me to a sign in page if you already have an account that's great you can just you know type in your username and you can type in your password and then just log into your account else you will have to create a new aws account you can see that option right in the bottom and once you click that you will be redirected to a page where you'll have to provide some credentials like your email address your password confirm password aws account name you will have a lot of steps that you'll have to go through you'll have to give in your card details your debit card or credit card details for this you can also read the rules and regulations of their free tire account they have some instances and products that they provide for some free amount so you will have to read all of that just get an overall idea of how amazon provides its services and what is free so you can really maximize the use of their free services right so once that is done you can just sign in to your existing aws account that's what i'm going to do so once that is done we'll meet on the other side so this is what you will be able to see when you log in you'll just be able to see your aws management console you can explore aws you can also build a solution you can learn how to build and you can see the recent services that you've been working on you can also view all of the services that are currently present in amazon web services and with all of that said let's go ahead and create your first notebook instance so as you can see on the top there is a navigation search bar here you can just look for amazon sage maker okay and then you just click on the first service that pops up and this will redirect you to your amazon sage maker on the left hand side you can see you have your amazon sage maker studio dashboard search images ground truth notebook processing training inference and edge manager augmented ai aws marketplace you have a lot of options that you can really see in amazon sage maker now you can either do this project on amazon sagemaker studio but we'll be creating this project on the jupiter notebook so we'll be creating our first instance you can just click on the drop down menu of notebook and then click on the notebook instances option and as you can already see there's an instance that's currently running which is named as edureka demo 16 okay and you can see there's a creation time its status and actions that you can currently take on it so now we'll go ahead and create our own notebook instance it's a pretty easy thing you can just name your notebook instance anything you'd like i'm just going to name it demo okay you can pick a notebook instance type you can also pick an elastic inference i'm just going to let the instance type be ml.t2.medium okay i'm just going to scroll down so the iam role is pretty interesting so it is basically an aws identity and access management entity with permissions to really make aws service requests now iam roles cannot make direct requests to aws services they're basically just meant to be assumed by authorized entities such as iam users applications or even you know aw services such as ec2 okay so what i'm going to do is i am going to create a new role here so here the iam role that you will create will provide access to the s3 buckets you specify so for all of you all who don't know what s3 buckets is an amazon s3 bucket is simply a public cloud storage resource that is available in aws which is simply termed as simple storage service s3 and object storage offering so basically it's very similar to you know file folders or store objects so s3 really just contains data and its descriptive metadata so basically just like a database okay and now you can specify your s3 buckets that you like so here i'm just going to pick any s3 bucket currently i have only one s3 bucket so i'm just going to click on that and i'm going to create role and you can see that there's a notification that says success you created an iem role once that is done you're all set you can go ahead and create your first notebook instance on amazon sagemaker so usually this takes some time before it creates right now the status is spending as you can already see that so once it's created it'll just say the status is in service so we'll just wait for it to get in service okay now that you can see the status says in service it'll take a while to get in service now there are two actions that you can perform you can see that the first one is open jupiter or you also have an option where you can open jupiter lab i'm just going to click open jupiter and you can see that the page is redirected now that you can see that you'll be redirected to your first empty notebook your jupyter notebook and here you can create your first file and since we'll be creating a new notebook with python 3. so i'm just going to click on that see that your first so you can see that your first file has been created and you can name this whatever you'd like to you can just name it demo or you could just name it first project let's name it first project and i'm just going to rename this and if you want to save the file you can just save and checkpoint if you want to add cells you can do that if you want to remove cells you can just click this scissor icon you can also copy particular cells that you'd like to copy and you can paste cells below too if you want to move from one cell to another you can use these arrow buttons to you know switch up and down if you want to you know select and run a particular block of code you can just press this button if you want to interrupt the kernel you can just press the pause button if you want to restart the kernel you can just press this refresh button you can also restart the kernel and rerun the whole notebook and you can also use this option for you know specifying what kind of block you want to execute if it's a code you can just click code and you can just print whatever you'd like i'm just going to print hello and once i execute it it will give me the output hello and if i want to add headings i can do that too and i'm just going to run this and you can just print this as a heading if you want to mark them down you can mark them down too and you you have many options you can really do a lot of things with this and you can also see there's a navigation bar here you can save your files you can open on your notebook you can open the previous files if you have any you can make a copy if you know how jupyter notebook really works this is going to be a piece of cake for you and also you can view you can insert and you can also make manipulations with the cells that are currently present in your notebook and there are a lot of options you'll really get a hang of it once you start using jupiter okay now that we have an idea of how to create your first notebook instance let's move on to the next part of the session so what we're going to do first is we're going to download explore and transform a particular data set so in this step you load the adult census data to your notebook instance using the shap that is the sharply additive explanations library and we are also going to review the data set transform it and then upload it to amazon s3 okay as you can already see i have everything in place so first what we're going to do is we are going to install the sharp library now if the current jupiter kernel does not have the sharp library install it by running the following conda command that is conda install hyphen c conda hyphen forge shop you there's also a way to download shop on jupyter lab and that's not necessary right now so we're not going to discuss it now if i go a little down to the next cell now if i scroll down a bit you can see that using the sharp library i have imported the adult census data set so you can see that import shaft as you can see i have two variables x and y and also x display y display and also i'm going to display all of the feature names so you can see that in the output we have 12 different feature names and that's printed for us age war class education number marital status occupation relationship race sex capital gain capital loss ask per week in country okay so the feature name is basically a list object and it should list the features of this data set and that's what the output shows us so now if i scroll down a little more you will just have to run the following script to display the statistical overview of the data set and histograms of the numeric features you can see that the figure size is 20 by 10 and it's just describing the entire data set that's all you have a proper repeat so you have a good overview of the data so this is great now if we scroll down a little more we are now splitting the data set into train validation and test data sets okay now using sklon we are splitting the data set into a training set and a test set and the data set is randomly sorted with the fixed random speed and 20 of it for test set now moving on to the next block of code you're splitting the training set to separate out a validation set that is 75 of the training set really becomes the final training set and the rest is the validation set okay now moving on to the next block of code we are using the pandas package i'm sure everyone knows the pandas package it's one of the most important package in python and here we're going to use it to explicitly align each data set by concatenating the numeric features with the true labels okay once that is done we are just going to check if the data set is split and structured as expected so i'm just going to print train and it's going to give me the training data set and i'm going to also print validation and it's going to give me an overview of the validation data set and finally i am going to also display the test data set and it's just going to give me an overview of that you can see the number of rows and columns too that is specified in the bottom of each of these outputs that's great now moving on to the 10th block of code we're going to convert the train and validation data sets to csv files this is really important so we're going to use csv format to store the data and the first column is expected to definitely be the output column okay now we're going to convert the train and validation data frame objects to csv files to match the input file format for the xg boost algorithm we're going to be using that algorithm we'll come to that later moving on to the 11th block of code we are finally going to upload the data sets to amazon s3 this is really important now using the sagemaker and boto3 we have to upload the training and validation data sets to the default amazon s3 bucket and the data sets in the s3 bucket will be used by a compute optimize sage maker instance on amazon ec2 for training now the following code what it does is it sets up the default s3 bucket uri for your current sagemaker session and it creates this new demo sage maker xgboost adult income prediction folder and then it uploads the training and validation data sets to the data subfolder okay i hope this is clear moving on we have to run the following aws cli to check if the csv files are successfully uploaded to the s3 bucket okay and it should return of this following output and you can see that the train csv file and the validation csv files are the outcome that's great and that's right so let's move on to the next part of the session so now what we're going to do is we're going to train our model okay now to choose the right algorithm for your particular data set you typically need to evaluate different models to really find the most suitable models for your data right now for simplicity the sagemaker xg boost algorithm that is a built-in algorithm is used throughout this tutorial without the pre-evaluation of models now if you want sagemaker to find an appropriate model for your tabular dataset it is recommended to use amazon sagemaker autopilot that automates a machine learning solution okay now let's switch back to our demo part now after you've finally figured out which model to use you can start constructing a sagemaker estimator for training now here currently we're using the xgboost built-in algorithm for the sagemaker generic estimator so the first thing you have to do is you will have to run a model training job so for that you will have to import you can see on the block 13 you will import the amazon siege maker python sdk and start by retrieving the basic information from your current sage maker session okay and this returns two following information the first one is the aws region the current aws region where the sage maker notebook instance is running currently you can see that it's running in u.s east 2 region and there's also another output that we have that is the role and this is the ima role used by the notebook instance that we created if you remember okay now i just checked the sage maker python sdk version by running this command that is sage maker dot underscore underscore version underscore underscore and this is because i wanted to know the version of sage maker if the sage maker version is less than 2.20 then you will have to update it okay you can update it by using exclamatory mark pip install hyphen q uppercase usage maker but if you have a version of sagemaker that is above 2.20 then you're good to go okay moving on to the next block of code now we're going to create an sg boost estimator using the sagemaker.estimator.estimator class now in the following example code the sg boost estimator is named xgb underscore model okay so we have used the name xgb model okay so i hope that is clear now to construct the sagemaker estimator you will have to specify some parameters okay you can see all of these parameters here we're going to discuss about each of these now the first one is the image underscore uri which specifies the training container image uri now in this example the sagemaker xgboost training container uri is specified by using the command sage maker dot image underscore uris dot retrieve okay and the role that we've specified here is the aws identity and access management role that sage maker uses to perform tasks on your behalf now for example reading training results call model artifacts from amazon s3 and writing you know training results to amazon s3 all of this is performed by the access management role okay now the train instance count and train instance type is the type and number of amazon ec2 ml compute instances to use for model training now for this training exercise you use an ml.m2.me instance okay and you can use anything you'd like to hold then moving on to the train volume size this is simply just the size in gb of the ebs storage volume to attach to the training instance and this must definitely be large enough to store training data if you use file mode okay file mode is on by default so you don't really have to worry about that okay moving on to the output path now the output path is simply the path to the s3 bucket where sage maker stores the model artifact and training results sage maker session on the other hand is the session object that manages interactions with sage maker api operations and other aws services that the training job uses rules finally is the last parameter and it specifies a list of sage maker debugger built-in rules now in this example we have used the create xg boost report which creates an xg boost report that provides insights into the training progress and results and we will check this report out later now if we move to the next part of the code we can see that we have set the hyper parameters for the xg boost algorithm by calling the set hyperparameters function by calling the set hyperparameters method of the estimator and there's a complete list of extreme boost hyperparameters you can also tune the hyperparameters using the sagemaker hyperparameter optimization features okay moving on to the next block of code so here you can see that we're using the training input class to configure a data input flow for training now the following code shows how to configure training input objects to use the training and validation data sets you uploaded to amazon s3 in the split the data set into train validation and test data set section okay now once we've configured that so in the next block of code we're finally going to train the model so here we're going to call the estimator's fit method with the training and validation data sets now by setting weight is equal to true the fit method displays progress logs and also waits until training is complete and once the training is done you can see that the output is completed training job completed training seconds and the billable seconds is given okay now in the next block of code we run the following code to specify the s3 bucket uri where the debugger training reports are generated and checks if the reports exist okay now in the next block of code you will have to download the debugger xg boost training and profiling reports to the current workspace then you'll have to run the following ipython script to get the file link of the xd boost training report okay now the following ipad script returns the file link of the debugger profiling report that really shows summaries and details of the ec2 instance resource utilization system bottleneck detection results and also the python operation profiling results so once you run this particular block of code you can just click below to view the profiler report and as i've already mentioned here you can just see all of the resource utilization the bottleneck detection results and all of the you know really just a summary and statistics of the report so here you have the rule summary where you can see the gpu memory increase the cpu bottleneck the max initialization time the low gpu utilization load balancing etc etc you have a lot of things that are happening here you can see the start time the end time the job duration that's 56 seconds okay let's move back now what we're going to do is we are going to get predictions deploy our model to amazon ec2 using amazon sagemaker okay now to host a model through amazon ec2 using amazon sage maker you will have to deploy the model that you trained in create and run a training job by calling the deploy method of the xgb model estimator if you remember we had declared the xgb model in the previous session okay now when you call the deploy model you must specify the number and type of ec2 ml instances that you want to use for hosting an endpoint okay so you can see that we've used three different parameters here the first one is the initial instance count that is the int the number of instances to deploy the model the second parameter is the instance type which is string the type of instances that you want to operate your deployed model and the last parameter is the serializer which is into and it serializes input data of various formats that is a numpy array list file or buffer to a csv formatted string and we can use this because the xg boost algorithm accepts input files in csv format now what the deploy method really does is it creates a deployable model and configures the sagemaker hosting services endpoint and eventually launches the endpoint to host the model now we will have to retrieve the name of endpoint that was generated by this deploy method okay so for that we will have to run the following code that is the xgb predictor dot endpoint underscore name now this should return the end point name of the xgb predictor and the format of the endpoint name is sagemaker xgboost and the date that is in years and the month the date the r and you know the seconds the minutes the seconds everything okay so the deployment of model was pretty easy right now let's move on to the next part of the session now that we have trained and deployed a model using amazon siege maker evaluate the model to ensure that it generates accurate predictions on new data okay now to evaluate the model and use it in production we have to invoke the endpoint with the test data set and check whether the inferences you get returns a target accuracy you want to achieve so in the 27 block of code you set up the following function to predict each line of the test data here you can see in the code the rows argument is to specify the number of lines to predict at a time then you can change the value of it to perform a batch inference that fully utilizes the instances hardware resources and in the next block of code you run the following code to make predictions of the test data sets and then you plot a histogram so for obviously plotting histograms we have to use matplotlib dot pi plot and it will display the histogram now in the next block of code the predicted values are float type to determine true or false based on the float values you need to set a cutoff value you can see that we've used the sky kit learn library to return the output confusion metrics and classification report with a cutoff of 0.5 once that is done the output will return the following confusion matrix okay now to find the best cutoff with the given test set compute the log loss function of the logistic regression now the log loss function is defined as the negative log likelihood of a logistic model that returns prediction probabilities for its ground truth levels now the code numerically and iteratively calculates the log loss values it returns a log loss versus cut off graph you can see that output here so this is the log loss curve okay now finally we find the minimum points of the error curve using the numpy augmen and min functions so this should easily return log loss is minimized at a cutoff of 0.53 and the log loss value at the minimum is 4.34 and so on now instead of computing and minimizing the log loss function you can estimate a cost function as an alternative for example if you want to train a model to perform a binary classification for a business problem such as a customer churn prediction problem you can simply set weights to the elements of confusion matrix and calculate the cost function accordingly i hope this demo section proved and helped you let's move on and check out the case study so in the amazon sage maker case study we'll be talking about grammarly now every day grammarly's algorithms help millions of people communicate more effectively by offering writing assistance on multiple platforms across devices and also through a combination of natural language processing and also advanced machine learning technologies amazon sage maker makes it possible for grammarly to develop their tensorflow models in a distributed training environment here workflows also integrate with amazon emr for pre-processing so they can get the data from amazon s3 filtered with emr and spark from a jupiter notebook and then eventually train in amazon sage maker with the same notebook sagemaker is also flexible for their production requirements they can run inferences on sagemaker itself or if they need just the model they can download it from s3 and run inferences for their mobile device implementation for ios and android customers so this is how interesting and how helpful amazon siege maker is in today's world with this we come to the end of today's session on how to deploy a machine learning model with the help of amazon sage maker if you have any queries leave them in the comment section below and we'll get back to you as soon as possible until next time thank you i hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to edureka channel to learn more happy learning you
Info
Channel: edureka!
Views: 56,778
Rating: undefined out of 5
Keywords: yt:cc=on, aws sagemaker, aws sagemaker tutorial for beginners, amazon sagemaker tutorial, deploy ml model on aws, amazon sagemaker studio, aws machine learning deployment, ml model deployment aws, Deploy Machine Learning Model using Amazon SageMaker, amazon sagemaker, deploy machine learning model aws, sagemaker tutorial, How to Deploy ML Models on AWS, aws machine learning tutorial, aws machine learning tutorial for beginners, machine learning, Edureka, Machine learning edureka
Id: 6cg5ERPQ2v8
Channel Id: undefined
Length: 30min 52sec (1852 seconds)
Published: Sun Mar 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.