Package: sagemaker-tidymodels
sagemaker-tidymodelssagemaker-tidymodelsis anAWS
Sagemakerframework for training and
deploy machine learning models written in R.This framework lets you do cloud-based training and deployment withtidymodels, using the same code you would
write locally.InstallationYou can install the framework with fromPyPi:pipinstallsagemaker-tidymodelsThe docker image is available ondockerhub:dockerpulltmastny/sagemaker-tidymodelsUsageThesagemaker-tidymodelsPython package provides simple wrappers
around theEstimatorand Modelsagemakerclasses.The main difference is theentry_pointparameter, where you can supply
an R script. This R script should process the raw data, train the model,
and save the final fit.fromsagemaker_tidymodelsimportTidymodels,get_roletidymodels=Tidymodels(entry_point="tests/train.R",train_instance_type="local",role=get_role(),image_name="tmastny/sagemaker-tidymodels:latest",)s3_data="s3://sagemaker-sample-data-us-east-2/processing/census/census-income.csv"tidymodels.fit({"train":s3_data})train.Ris a normal R script, with a few necessary additions so it can
run on the command line effectively.#!/usr/bin/env Rscriptlibrary(tidymodels)if(sys.nframe()==0){input_path<-file.path(Sys.getenv("SM_CHANNEL_TRAIN"),"census-income.csv")df<-read.csv(input_path,stringsAsFactors=TRUE)pipeline<-workflow()%>%add_formula(income~age)%>%add_model(logistic_reg()%>%set_engine("glm"))model<-pipeline%>%fit(data=df)output_path<-file.path(Sys.getenv("SM_MODEL_DIR"),"model.RDS")saveRDS(model,output_path)}The first line should be the shebang#!/usr/bin/env Rscript, so it
can be ran as./train.Ras required by the framework. Make sure to
runchmod +x train.Rso it’s an executable.All the training logic should be wrapped by the followingifstatement. This seems a little mysterious, but it makes sure that
the training logic doesn’t accidentally run when we’ve deployed our
model for predictions.if(sys.nframe()==0){# training logic goes here!}Sagemaker is very specific about input and output locations. The
input data path is found in an environment variable that can be read
usingSys.getenv('SM_CHANNEL_TRAIN'). Likewise, the output model
path can be found withSys.getenv('SM_MODEL_DIR').From there, you can deploy the model as normal!predictor=model.deploy(initial_instance_count=1,instance_type="local")predictor.predict("28\n")Advanced UsageThe docker container has some additional features that may be useful.Custom Model ServingThe model serving defaults are defined indocker/server/default_fn.R.
If you’d like to customize how the model is served, you can overwrite
these defaults by defining these functions in yourentry_pointscript.The valid options aremodel_fn,input_fn,predict_fn, andoutput_fn. In our scripttrain.R, the defaultpredict_fnmeans we
get class predictors, either- 50000.or50000+..If we wanted to output the probability of belonging to either class, we
could include our ownpredict_fnintrain.R:# add to `train.R`predict_fn<-function(model,new_data){predict(model,new_data,type="prob")}This is also why the training script needs to be wrapped by theifstatement.Identical Local and Cloud ScriptsIntrain.R, the logic you use to train is exactly the same you would
write locally. However, you can’t run the script locally as is, becausesagemakerdefines the environment variablesSM_CHANNEL_TRAINandSM_MODEL_DIR(as well asmany
othersyou might want to use).A nice way to set some defaults so the script can run locally and in
sagemaker is by usingr-optparse.For example:library(optparse)option_list<-list(make_option(c("-i","--input"),default=Sys.getenv("SM_CHANNEL_TRAIN")),make_option(c("-o","--output"),default=Sys.getenv("SM_MODEL_DIR")))args<-parse_args(OptionParser(option_list=option_list))This lets us useargs$inputandargs$outputto refer to the input
data path and output model path. Then when running locally, we can
define inputs and outputsRscripttests/train.R-idata/census-income.csv-omodels/and it works just as it would insagemaker.