Pyspark Mnist Xgboost
SageMaker PySpark XGBoost MNIST Example
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.
Introduction
This notebook will show how to classify handwritten digits using the XGBoost algorithm on Amazon SageMaker through the SageMaker PySpark library. We will train on Amazon SageMaker using XGBoost on the MNIST dataset, host the trained model on Amazon SageMaker, and then make predictions against that hosted model.
Unlike the other notebooks that demonstrate XGBoost on Amazon SageMaker, this notebook uses a SparkSession to manipulate data, and uses the SageMaker Spark library to interact with SageMaker with Spark Estimators and Transformers.
You can visit SageMaker Spark's GitHub repository at https://github.com/aws/sagemaker-spark to learn more about SageMaker Spark.
You can visit XGBoost's GitHub repository at https://github.com/dmlc/xgboost to learn more about XGBoost
This notebook was created and tested on an ml.m4.xlarge notebook instance.
Setup
First, we import the necessary modules and create the SparkSession with the SageMaker Spark dependencies.
Loading the Data
Now, we load the MNIST dataset into a Spark Dataframe, which dataset is available in LibSVM format at
s3://sagemaker-sample-data-[region]/spark/mnist/train/
where [region] is replaced with a supported AWS region, such as us-east-1.
In order to train and make inferences our input DataFrame must have a column of Doubles (named "label" by default) and a column of Vectors of Doubles (named "features" by default).
Spark's LibSVM DataFrameReader loads a DataFrame already suitable for training and inference.
Here, we load into a DataFrame in the SparkSession running on the local Notebook Instance, but you can connect your Notebook Instance to a remote Spark cluster for heavier workloads. Starting from EMR 5.11.0, SageMaker Spark is pre-installed on EMR Spark clusters. For more on connecting your SageMaker Notebook Instance to a remote EMR cluster, please see this blog post.
Training and Hosting a Model
Now we create an XGBoostSageMakerEstimator, which uses the XGBoost Amazon SageMaker Algorithm to train on our input data, and uses the XGBoost Amazon SageMaker model image to host our model.
Calling fit() on this estimator will train our model on Amazon SageMaker, and then create an Amazon SageMaker Endpoint to host our model.
We can then use the SageMakerModel returned by this call to fit() to transform Dataframes using our hosted model.
The following cell runs a training job and creates an endpoint to host the resulting model, so this cell can take up to twenty minutes to complete.
Inference
Now we transform our DataFrame.
To do this, we serialize each row's "features" Vector of Doubles into LibSVM format for inference against the Amazon SageMaker Endpoint. We deserialize the CSV responses from the XGBoost model back into our DataFrame. This serialization and deserialization is handled automatically by the transform() method:
How well did the algorithm perform? Let us display the digits corresponding to each of the labels and manually inspect the results:
Since we don't need to make any more inferences, now we delete the endpoint:
More on SageMaker Spark
The SageMaker Spark Github repository has more about SageMaker Spark, including how to use SageMaker Spark with your own algorithms on Amazon SageMaker: https://github.com/aws/sagemaker-spark
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.