Using R with Amazon SageMaker
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:
ggplot2. You use RStudio to run the code. RStudio is an integrated development environment (IDE) for working with R. It can be licensed either commercially or under AGPLv3.
Launching AWS CloudFormation
Launching this stack creates the following resources:
- A public virtual private cloud (VPC)
- An Amazon EC2 instance (t2.medium)
- A security group allowing SSH access only
- An AWS Identity and Access Management (IAM) role for Amazon EC2 with Amazon SageMaker permissions
- An IAM service role for Amazon SageMaker
- An installation of RStudio
- The required R packages
- An installation of the Amazon SageMaker Python SDK
After you launch the stack, follow these steps to configure and connect to RStudio:
- On the Select Template page, choose Next.
- On the Specify Details page, choose your key pair for KeyName.
- On the 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 has reached CREATE_COMPLETE status, choose the Outputs
- Copy and paste the SSH string in Value into a terminal window.
The SSH command forwards port 8787 to your computer while connecting to the new instance. Once connected, open a browser window and type localhost:8787 in the address bar:
You might see connect failed messages in your terminal while RStudio and the required R packages are installing. The installation process takes approximately 15 minutes.
Sign in with the following credentials:
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.
Session class, as shown in the following image.
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:
default_bucket function creates a unique Amazon S3 bucket with the following name:
sagemaker-<aws-region-name>-<aws account number>.
Specify the IAM role’s ARN to allow Amazon SageMaker to access the Amazon S3 bucket:
The AWS CloudFormation stack automatically created an IAM role called
sagemaker-service-role with the required policies. The
expand_role function retrieves the ARN for this role because Amazon SageMaker requires it.
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
height for each value of
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
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
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:
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:
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.