AWS Machine Learning Blog
Using R with Amazon SageMaker
July, 2022: This post was reviewed and updated for relevancy and accuracy, with an updated AWS CloudFormation Template.
December 2020: Post updated with changes required for Amazon SageMaker SDK v2
This blog post describes how to train, deploy, and retrieve predictions from a machine learning (ML) model using Amazon SageMaker and R. The model predicts abalone age as measured by the number of rings in the shell. The reticulate package will be used as an R interface to Amazon SageMaker Python SDK to make API calls to Amazon SageMaker. The reticulate package translates between R and Python objects, and Amazon SageMaker provides a serverless data science environment to train and deploy ML models at scale.
To follow along with this blog post, you should have a basic understanding of R and be familiar with the following tidyverse packages: dplyr
, readr
, stringr
, and ggplot2
. You use RStudio to run the code. RStudio is an integrated development environment (IDE) for working with R. We will be using fully managed RStudio on Amazon SageMaker.
Launching AWS CloudFormation
Please refer to Get started with RStudio on Amazon SageMaker which details the steps you take to create a SageMaker domain with RStudio. Use the following AWS CloudFormation stack to create a domain with a user profile:
Launching this stack creates the following resources:
- A SageMaker domain with RStudio
- A SageMaker RStudio user profile
- An IAM service role for SageMaker RStudio domain execution
- An IAM service role for SageMaker RStudio user profile execution
After you launch the stack, follow these steps to configure and connect to RStudio:
- On the Select template page, choose Next.
- On the Specify stack details page, in the Stack name section, enter a name.
- On the Specify stack details page, in the Execution Role Arn, leave blank unless you already have the required role created.
- On the Specify stack details page, in the Vpc Id parameter, select your Vpc.
- On the Specify stack details page, in the Subnet Id(s) parameter, select your subnets.
- On the Specify stack details page, in the App Network Access Type parameter, select either PublicInternetOnly or VpcOnly.
- On the Specify stack details page, in the Security Group(s) parameter, select your security groups.
- On the Specify stack details page, in the Domain Execution Role Arn, leave blank unless you already have the required role created.
- Leave the remaining required parameters as is.
- There are also optional parameters which we will not use:
- Customer managed CMK
- RStudio connect URL
- RStudio package manager URL
- 3 RStudio custom images
- On the bottom of the Specify stack details page, choose Next.
- On the Configure stack options page, choose Next.
- On the Review page, select the I acknowledge that AWS CloudFormation might create IAM resources with custom names check box and choose Next.
Once the stack status is CREATE_COMPLETE, navigate to the Amazon SageMaker Control Panel and launch the RStudio app for rstudio-user.
On the RStudio Workbench launcher page start a new R session using the RSession Base image.
Reticulating the Amazon SageMaker Python SDK
First, load the reticulate
library and import the sagemaker
Python module. Once the module is loaded, use the $
notation in R instead of the .
notation in Python to view the available classes.
Use the Session
class, as shown in the following image.
The Session
class provides operations for working with the following boto3 resources with Amazon SageMaker:
To view the objects available to the Session
class, use the $
notation, as shown in the following image.
Creating and accessing the data storage
Let’s create an Amazon Simple Storage Service (Amazon S3) bucket for your data. You will need the IAM role that allows Amazon SageMaker to access the bucket.
Specify the Amazon S3 bucket to store the training data, the model’s binary file, and output from the training job:
Note:
- You do not need to install Miniconda. Type n when you are prompted.
- The
default_bucket
function creates a unique Amazon S3 bucket with the following name:sagemaker-<aws-region-name>-<aws account number>
.
Downloading and processing the dataset
The model uses the abalone dataset from the UCI Machine Learning Repository. First, download the data and start the exploratory data analysis. Use tidyverse packages to read the data, plot the data, and transform the data into ML format for Amazon SageMaker:
The preceding image shows that sex
is a factor data type but is currently a character data type (F is female, M is male, and I is infant). Change sex
to a factor and view the statistical summary of the dataset:
The summary shows that the minimum value for height
is 0:
Visually explore which abalones have height equal to 0 by plotting the relationship between rings
and height
for each value of sex
:
The plot shows multiple outliers: two infant abalones with a height of 0 and a few female and male abalones with greater heights than the rest. Let’s filter out the two infant abalones with a height of 0.
Preparing the dataset for model training
The model needs three datasets: one each for training, testing, and validation. First, convert sex
into a dummy variable and move the target, rings
, to the first column. Amazon SageMaker algorithms require the target to be in the first column of the dataset.
This code produces a dataset like the following:
Next, sample 70% of the data for training the ML algorithm. Split the remaining 30% into two halves, one for testing and one for validation:
Upload the training and validation data to Amazon S3 so that you can train the model. First, write the training and validation datasets to the local filesystem in .csv format:
Second, upload the two datasets to the Amazon S3 bucket into the data key:
Finally, define the Amazon S3 input types for the Amazon SageMaker algorithm:
Training the model
Amazon SageMaker algorithms are available via a Docker container. To train an XGBoost model, specify the training containers in Amazon Elastic Container Registry (Amazon ECR) for the AWS Region.
Define an Amazon SageMaker Estimator, which can train any supplied algorithm that has been containerized with Docker. When creating the Estimator, use the following arguments:
- image_uri – The container image to use for training
- role – The Amazon SageMaker service role that you created
- instance_count – The number of Amazon EC2 instances to use for training
- instance_type – The type of Amazon EC2 instance to use for training
- volume_size – The size in GB of the Amazon Elastic Block Store (Amazon EBS) volume to use for storing input data during training
- max_run – The timeout in seconds for training
- input_mode – The input mode that the algorithm supports
- output_path – The Amazon S3 location for saving the training results (model artifacts and output files)
- output_kms_key – The AWS Key Management Service (AWS KMS) key for encrypting the training output
- base_job_name – The prefix for the name of the training job
- sagemaker_session – The Session object that manages interactions with Amazon SageMaker API
Note
The equivalent to None
in Python is NULL
in R.
Specify the XGBoost hyperparameters and fit the model. Set the number of rounds for training to 100 which is the default value when using the XGBoost library outside of Amazon SageMaker. Also specify the input data and a job name based on the current time stamp:
Once training has finished, Amazon SageMaker copies the model binary (a gzip tarball) to the specified Amazon S3 output location. Get the full Amazon S3 path with this command:
Deploying the model
Amazon SageMaker lets you deploy your model by providing an endpoint that consumers can invoke by a secure and simple API call using an HTTPS request.
Let’s deploy our trained model to a ml.t2.medium
instance. For more information, see Amazon SageMaker ML Instance Types.
Generating predictions with the model
Use the test data to generate predictions. Pass comma-separated text to be serialized into JSON format by specifying CSVSerializer
for the endpoint:
Remove the target column and convert the first 500 observations to a matrix with no column names:
Note
500 observations was chosen because it doesn’t exceed the endpoint limitation.
Generate predictions from the endpoint and convert the returned comma-separated string:
Column-bind the predicted rings to the test data:
The predicted ages (number of shell rings) look like this:
Deleting the endpoint
When you’re done with the model, delete the endpoint to avoid incurring deployment costs:
Conclusion
In this blog post, you learned how to build and deploy an ML model by using Amazon SageMaker with R. Typically, you execute this workflow with Python, but we showed how you could also do it with R.
About the Author
Ryan Garner is a Data Scientist with AWS Professional Services. He is passionate about helping AWS customers use R to solve their Data Science and Machine Learning problems.