Extend Your Estimator for Batch Inference Job | SageMaker Notebook | AWS Machine Learning

Batch Inference Job for Extending Estimator in SageMaker Notebook

Question

You work as a machine learning specialist for a data mining department of a large bank.

Your department is responsible for leveraging the bank's huge data lake to gain insights and make predictions for your marketing and risk departments.

Your team's latest project, an XGBoost prediction model, is ready for production deployment.

However, you want to run some additional batch predictions using a batch inference job to make sure your model can handle the production prediction workload.

In your SageMaker notebook, how do you extend your estimator to read input data in batch from a specified S3 bucket and make predictions?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer: A.

Option A is correct.

You can extend your estimator to a transformer object, which is derived from the SageMaker Transformer class.

The batch transformer reads input data from a specified S3 bucket and makes predictions.

Option B is incorrect.

The Predictor object makes prediction requests to an Amazon SageMaker endpoint.

However, it is not the SageMaker API used to perform batch predictions.

Option C is incorrect.

The MultiDataModel object is used to deploy multiple models to the same endpoint, not to make batch predictions.

Option D is incorrect.

There is no BatchPredictor SageMaker API.

References:

Please see the Amazon SageMaker developer guide titled Deploy a Model in Amazon SageMaker (https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-deployment.html),

The Amazon SageMaker developer guide titled Step 5: Deploy the Model to Amazon EC2 (https://docs.aws.amazon.com/sagemaker/latest/dg/ex1-model-deployment.html#ex1-deploy-model), a.

The SageMaker API page titled Transformer (https://sagemaker.readthedocs.io/en/stable/api/inference/transformer.html),

The SageMaker API page titled Predictors (https://sagemaker.readthedocs.io/en/stable/api/inference/predictors.html),

The SageMaker API page titled MultiDataModel (https://sagemaker.readthedocs.io/en/stable/api/inference/multi_data_model.html),

The SageMaker API page titled Inference APIs (https://sagemaker.readthedocs.io/en/stable/api/inference/index.html)

To run batch predictions using an XGBoost prediction model in SageMaker, we need to extend the estimator to a BatchPredictor object. Here is a step-by-step explanation of how to do that:

  1. First, we need to create an instance of the XGBoost estimator with the hyperparameters and input data configuration that we want. For example:
python
from sagemaker import Estimator from sagemaker.amazon.amazon_estimator import get_image_uri container = get_image_uri(boto3.Session().region_name, 'xgboost') estimator = Estimator(container, role='arn:aws:iam::012345678901:role/SageMakerRole', train_instance_count=1, train_instance_type='ml.m5.large', output_path='s3://bucket-name/model-artifacts/', hyperparameters={ 'max_depth': '5', 'eta': '0.2', 'gamma': '4', 'min_child_weight': '6', 'subsample': '0.8', 'objective': 'binary:logistic', 'num_round': '100' })

This creates an instance of the XGBoost estimator with the specified hyperparameters and output path for storing the trained model artifacts.

  1. Next, we need to configure the batch inference job by creating an instance of the sagemaker.predictor.Predictor class, which is a base class for SageMaker predictors. For example:
python
from sagemaker.predictor import Predictor from sagemaker.serializers import CSVSerializer from sagemaker.deserializers import JSONDeserializer class MyPredictor(Predictor): def __init__(self, endpoint_name, sagemaker_session): super(MyPredictor, self).__init__(endpoint_name, sagemaker_session, serializer=CSVSerializer(), deserializer=JSONDeserializer())

This creates a custom MyPredictor class that extends the Predictor class and sets the serializer to CSV and deserializer to JSON. The endpoint_name parameter should be set to the name of the endpoint that will be created for the batch inference job.

  1. Once we have created the custom predictor class, we can use it to create a batch predictor object by calling the create_batch_predictor method of the estimator. For example:
python
batch_predictor = estimator.create_batch_predictor( 's3://bucket-name/input-data/', output_path='s3://bucket-name/batch-predictions/', job_name='my-batch-prediction-job', predictor_cls=MyPredictor )

This creates a batch predictor object that will read input data in batch from the specified S3 bucket, make predictions using the XGBoost model, and store the output predictions in the specified S3 bucket. The job_name parameter sets the name of the batch inference job.

  1. Finally, we can run the batch inference job by calling the predict method of the batch predictor object, passing in the path to the input data in S3. For example:
python
batch_predictor.predict('s3://bucket-name/input-data/test.csv')

This will start the batch inference job and return immediately. We can monitor the status of the job using the SageMaker console or the describe_batch_job method of the batch predictor object. Once the job is complete, we can retrieve the output predictions from the specified S3 bucket.

In summary, to extend an XGBoost estimator to read input data in batch from a specified S3 bucket and make predictions, we need to create a custom predictor class that extends the Predictor class, use it