Skip to content

How to train a XGBoost regression model on Amazon SageMaker, host inference on a serverless function in AWS Lambda and optionally expose as an API with Amazon API Gateway

License

Notifications You must be signed in to change notification settings

aws-samples/amazon-sagemaker-xgboost-regression-model-hosting-on-aws-lambda-and-amazon-api-gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Train a XGBoost regression model on Amazon SageMaker, host inference on a serverless function in AWS Lambda and optionally expose as an API with Amazon API Gateway

This repository contains a sample to train a regression model in Amazon SageMaker using SageMaker's built-in XGBoost algorithm on the California Housing dataset and host the inference as a serverless function in AWS Lambda and optionally expose as an API with Amazon API Gateway.

Overview

Amazon SageMaker is a fully managed end-to-end Machine Learning (ML) service. With SageMaker, you have the option of using the built-in algorithms or you can bring your own algorithms and frameworks to train your models. After training, you can deploy the models in one of two ways for inference - persistent endpoint or batch transform.

With a persistent inference endpoint, you get a fully-managed real-time HTTPS endpoint hosted on either CPU or GPU based EC2 instances. It supports features like auto scaling, data capture, model monitoring and also provides cost-effective GPU support using Amazon Elastic Inference. It also supports hosting multiple models using multi-model endpoints that provide A/B testing capability. You can monitor the endpoint using Amazon CloudWatch. In addition to all these, you can use Amazon SageMaker Pipelines which provides a purpose-built, easy-to-use Continuous Integration and Continuous Delivery (CI/CD) service for Machine Learning.

There are use cases where you may want to host the ML model on a real-time inference endpoint that is cost-effective and do not require all the capabilities provided by the SageMaker persistent inference endpoint. These may involve,

  • simple models
  • models whose sizes are lesser than 200 MB
  • models that are invoked sparsely and do not need inference instances running all the time
  • models that do not need to be re-trained and re-deployed frequently
  • models that do not need GPUs for inference

In these cases, you can take the trained ML model and host it as a serverless function on AWS Lambda and optionally expose it as an API by front-ending it with a HTTP/REST API hosted on Amazon API Gateway. This will be cost-effective as compared to having inference instances running all the time and still provide a fully-managed and scalable solution.

This example contains a Jupyter Notebook that demonstrates this solution by using SageMaker's built-in XGBoost algorithm to train a regression model on the California Housing dataset. It loads the trained model as a Python3 pickle object in a container to be hosted on an AWS Lambda function. Finally, it provides instructions for exposing it as an API by front-ending it with a HTTP/REST API hosted on Amazon API Gateway.

Warning: The Python3 pickle module is not secure. Only unpickle data you trust. Keep this in mind if you decide to get the trained ML model file from somewhere instead of building your own model.

Note:

  • This notebook should only be run from within a SageMaker notebook instance as it references SageMaker native APIs.
  • If you already have a trained model that can be loaded as a Python3 pickle object, then you can skip the training step in this notebook and directly upload the model file to S3 and update the code in this notebook's cells accordingly.
  • Although you can host a Python3 function directly on AWS Lambda, choosing the container option to package the code and dependencies is the best fit for this use case as the ML model file along with its dependencies will easily exceed the maximum deployment package size of 50 MB for zipped or 250 MB for unzipped files.
  • In this notebook, the ML model generated in the training step has not been tuned as that is not the intent of this demo.

Repository structure

This repository contains

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

About

How to train a XGBoost regression model on Amazon SageMaker, host inference on a serverless function in AWS Lambda and optionally expose as an API with Amazon API Gateway

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published