In [ ]:
knitr::opts_chunk$set(cache = TRUE)
AzureML provides an interface to publish web services on Microsoft Azure Machine Learning (Azure ML) from your local R environment. The main functions in the package cover the following topics:
This short tutorial will show you how to use the AzureML
package to operationalize R models in Azure as webservices. Our package version is 0.2.13, which you can install from an up-to-date CRAN mirror. Please be careful, as package version < 0.2 will not work as described here, as there were some fundamental changes in the latter releases.
In [ ]:
getOption("repos")
# on centOS you'll need xml
# system("sudo yum install libxml2-devel")
install.packages("AzureML")
packageVersion("AzureML")
Getting started with the AzureML package requires a Microsoft Azure Machine Learning account. These are available for free from studio.azureml.net. Please make an account, don't use a guest account.
A complete introduction on using the AzureML
package is provided by the package's vignette.
Firstly, we need to get our AzureML credentials. Using the left-hand menu, click on the Settings tab and copy your Workspace ID.
Next, click on the Authorization Tokens tab at the top of the settings page and copy your Primary Authentication key.
In order to connect your R client to your AzureML workshop, we will need to define a workspace object using the workspace
function inside of your R session that points to an Azure service.
In [ ]:
wsid <- "your_id"
pri_token <- "your_private_token"
library(AzureML)
ws <- workspace(
id = wsid,
auth = pri_token
)
In order to use AzureML
package we need to expose our credentials to the package. For example, we could make this pointer directly using the workspace
function:
In [ ]:
library(AzureML)
ws <- workspace(
id = "your workspace ID",
auth = "your authorization token"
)
In [ ]:
head(datasets(ws))
In [ ]:
e <- experiments(ws)
head(e)
The experiments
function creates an R data.frame
containing all the experiments you have in your AzureML account, along with some useful metadata about each experiment.
The same information is also available inside the ws
object under the slot experiments
, which is continuously updated whenever called:
In [ ]:
identical(ws$experiments, e)
In [ ]:
powers <- function(x, y) {
x^y
}
And then we create our webservice:
In [ ]:
api <- publishWebService(
ws,
fun = powers,
name = "azure-power-ex",
inputSchema = list(
x = "numeric",
y = "numeric"
),
outputSchema = list(
ans = "numeric"
)
)
In [ ]:
df <- data.frame(
x = rep(2, 10),
y = 1:10
)
s <- services(ws, name = "azure-power-ex")
s <- tail(s, 1) # use the last published function, in case of duplicate function names
ep <- endpoints(ws, s)
consume(ep, df)
In the third module of our course, we learned how to create MRS using the RevoScaleR
package.
In order to deploy them on Azure, we need to first convert them into an object that Azure can understand. At the current time, Azure can only publish webservices using open-source R, so we need to convert our MRS model objects into their open-source counterparts. Fortunately, every MRS model object has a method that'll convert it to an open-source variant with the same coefficeints.
For the purpose of this part of the tutorial, I will assume you have ran the third module and created the gradient boosted trees model for default prediction, default_model_gbm
.
In [ ]:
default_model_gbm <- readRDS("gbm.rds")
summary(default_model_gbm)
To convert it to to a gbm
object from the base gbm
package, we will use the obvious method: as.gbm
:
In [ ]:
gbm_default <- as.gbm(default_model_gbm)
summary(gbm_default)
We will use the first five rows of the test dataset as our sample data:
In [ ]:
mort_split <- readRDS("mort_split.rds")
test <- head(mort_split$validate)
mypredict <- function(newdata)
{
require(gbm)
predict(gbm_default, newdata, n.trees = 10)
}
print(mypredict(test))
Now that we have create a scoring wrapper function, let's publish it to Azure:
In [ ]:
ep <- publishWebService(ws = ws,
fun = mypredict,
name = "AzureML-rpart-ex",
inputSchema = test)
# Consume test data, comparing with result above
print(consume(ep, test))
In [ ]:
deleteWebService(ws, name = "azure-power-ex")
deleteWebService(ws, name = "AzureML-rpart-ex")